【问题标题】:Show single product prices with and without VAT in WooCommerce在 WooCommerce 中显示含和不含增值税的单一产品价格
【发布时间】:2020-11-21 22:41:01
【问题描述】:

我试图在 WooCommerce 商店的每个单一产品页面上显示含增值税和不含增值税的价格。

感谢WooCommerce_available_variation 钩子,我已经使用下面的脚本为可变产品完成了它:

add_filter( 'woocommerce_available_variation', 'my_variation', 10, 3);
function my_variation( $data, $product, $variation ) {
    $data['price_html'] = "<bdi><span class='inc-vat-price'></span>" . woocommerce_price($variation->get_price_including_tax()) . "</bdi><br>";
    $data['price_html'] .= "<bdi><span class='ex-vat-price'>Tax Free - </span>". woocommerce_price($variation->get_price_excluding_tax()) . "</bdi>" ;
    return $data;
}

相反,对于单一产品,我没有找到任何解决方案。

通过修改 WooCommerce 中 Single Product 的 Price.php 文件,在变量 products 中也重复了不含增值税的价格。在这种情况下,我使用了代码:

$product->get_price_excluding_tax()

有什么办法可以解决这个问题?

【问题讨论】:

    标签: php wordpress woocommerce price tax


    【解决方案1】:

    WooCommerce_available_variation的实际代码已经过时了有一段时间了:

    • 函数woocommerce_price()wc_price()函数替换
    • 方法get_price_including_tax()替换为wc_get_price_including_tax()函数
    • 方法get_price_excluding_tax()替换为wc_get_price_excluding_tax()函数

    现在您可以改为为简单产品、可变产品和产品变体提供独特的功能 html 格式的价格,它还可以处理销售价格,显示含税和不含税的价格。

    因此,以下内容将替换您的代码,并适用于简单产品、可变产品和产品变体,以自定义格式显示包括和不包括价格(就像您在代码上所做的那样)仅在单个产品页面上

    add_filter('woocommerce_get_price_html', 'display_prices_incl_and_excl_taxes', 100, 2 );
    function display_prices_incl_and_excl_taxes( $price_html, $product ) {
        global $woocommerce_loop;
    
        // On single product pages only (and not on any product loop)
        if( isset($woocommerce_loop['total']) && $woocommerce_loop['total'] == 0
        && isset($woocommerce_loop['total']) && empty($woocommerce_loop['name']) ) {
    
            // For simple products and products variations
            if( $product->is_type('simple') || $product->is_type('variation') ) {
                // On sale products
                if( $product->is_on_sale() ) {
                    $regular_price_incl_tax = wc_get_price_including_tax( $product, array( 'price' => $product->get_regular_price() ) );
                    $price_incl_tax_html    = wc_format_sale_price( $regular_price_incl_tax, wc_get_price_including_tax( $product ) );
                    $regular_price_excl_tax = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_regular_price() ) );
                    $price_excl_tax_html    = wc_format_sale_price( $regular_price_excl_tax, wc_get_price_excluding_tax( $product ) );
                }
                // Not on sale
                else {
                    $price_incl_tax_html = wc_price( wc_get_price_including_tax( $product ) );
                    $price_excl_tax_html = wc_price( wc_get_price_excluding_tax( $product ) );
                }
            }
            // variable pproducts
            elseif( $product->is_type('variable') ) {
                $prices = $product->get_variation_prices( true );
    
                if ( ! empty( $prices['price'] ) ) {
                    $act_keys = array_keys($prices['price']);
                    $reg_keys = array_keys($prices['regular_price']);
    
                    $min_price_incl_tax = wc_get_price_including_tax( wc_get_product(reset($act_keys)));
                    $max_price_incl_tax = wc_get_price_including_tax( wc_get_product(end($act_keys)));
    
                    $min_price_excl_tax = wc_get_price_excluding_tax( wc_get_product(reset($act_keys)));
                    $max_price_excl_tax = wc_get_price_excluding_tax( wc_get_product(end($act_keys)));
    
                    $min_reg_price_jncl_tax = wc_get_price_including_tax( wc_get_product(reset($reg_keys)));
                    $max_reg_price_incl_tax = wc_get_price_including_tax( wc_get_product(end($reg_keys)));
    
                    $min_reg_price_excl_tax = wc_get_price_excluding_tax( wc_get_product(reset($reg_keys)));
                    $max_reg_price_excl_tax = wc_get_price_excluding_tax( wc_get_product(end($reg_keys)));
    
                    if ( $min_price_excl_tax !== $max_price_excl_tax ) {
                        $price_incl_tax_html = wc_format_price_range( $min_price_incl_tax, $max_reg_price_incl_tax );
                        $price_excl_tax_html = wc_format_price_range( $min_price_excl_tax, $max_reg_price_excl_tax );
                    }
                    elseif ( $product->is_on_sale() && $min_reg_price_excl_tax === $max_reg_price_excl_tax ) {
                        $price_incl_tax_html = wc_format_sale_price( wc_price( $max_reg_price_incl_tax ), wc_price( $min_price_incl_tax ) );
                        $price_excl_tax_html = wc_format_sale_price( wc_price( $max_reg_price_excl_tax ), wc_price( $min_price_excl_tax ) );
                    }
                    else {
                        $price_incl_tax_html = wc_price( $min_price_incl_tax );
                        $price_excl_tax_html = wc_price( $min_price_excl_tax );
                    }
                }
            }
            if ( isset($price_incl_tax_html) && isset($price_excl_tax_html) ) {
                $price_html  = '<bdi><span class="inc-vat-price"></span>' . $price_incl_tax_html . '<bdi><br>';
                $price_html .= '<bdi><span class="ex-vat-price">'. __("Tax Free") . ' - </span>' . $price_excl_tax_html . '<bdi><br>';
                $price_html .= $product->get_price_suffix();
            }
        }
        return $price_html;
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。


    此代码不会处理可变产品价格范围。

    【讨论】:

    • LoicTheAztec 如果我们想颠倒显示顺序,排除在前,包括在它旁边,你只是突出显示,所以我尝试这样做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 2020-11-17
    • 2018-07-03
    • 2019-05-22
    • 1970-01-01
    • 2022-01-13
    相关资源
    最近更新 更多