【问题标题】:Woocommerce show price incl and excl tax in product catalogWoocommerce 在产品目录中显示含税和不含税价格
【发布时间】:2020-06-24 16:08:31
【问题描述】:

我想在我的 Woocommerce 商店的目录页面中显示每个产品下的产品价格包括和不含税。

它已经在工作了,但是对于我只有一个变体的可变产品,它没有显示任何东西。它也适用于单一产品。

我也确实收到了通知:

注意:WC_Product::get_price_includes_tax 是版本 3.0 veraltet! Benutze stattdessen wc_get_price_including_tax.

注意:WC_Product::get_price_exclude_tax ist seit 3.0 veraltet! Benutze stattdessen wc_get_price_ exclude_tax。

但如果我这样做了,它就根本不起作用了。

add_action( 'woocommerce_after_shop_loop_item_title', 'preise_notice', 10 );
 
function preise_notice() {
 global $product;

    if ( $price_html_incl_tax = $product->get_price_including_tax() )
    if ( $price_html_excl_tax = $product->get_price_excluding_tax() )   {
        
        echo '<div class="product-prices-excl-vat"><a>ab ' . wc_price($price_html_excl_tax) . ' netto</a></div>';
        echo '<div class="product-prices-incl-vat"><a>(' . wc_price($price_html_incl_tax) . ' inkl. 19% MwSt.)</a></div>';
    }
}

【问题讨论】:

    标签: woocommerce


    【解决方案1】:

    wc_get_price_including_taxwc_get_price_excluding_tax 函数需要 $product 作为参数。所以你必须像这样传递它:

    wc_get_price_including_tax( $product )

    另外,获取产品的税率而不是硬编码似乎是个好主意。也许将来您将拥有不具有 19% 税率的产品。我还在wc_price 函数中包含了货币参数,因此价格将以商店的货币显示。

    您可以使用以下 sn-p 获取产品的税率并打印含税和不含税的价格:

    add_action( 'woocommerce_after_shop_loop_item_title', 'add_product_price_incl_and_excl_tax', 10 );
    function add_product_price_incl_and_excl_tax() {
    
        global $product;
        $tax_rate = '';
        $tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
    
        //Check the product tax rate
        if ( !empty( $tax_rates ) ) {
            $tax_rate = reset($tax_rates);
            $tax_rate = sprintf( ' inkl. %.0f%% MwSt.', $tax_rate['rate'] );
         }
    
        //Print product prices including tax and tax percentage, and excluding tax
        printf( '<div class="product-prices-excl-vat">ab %s netto</div>', wc_price( wc_get_price_excluding_tax( $product ), array( 'currency' => get_woocommerce_currency() ) ) );
        printf( '<div class="product-prices-incl-vat">%s%s</div>', wc_price( wc_get_price_including_tax( $product ), array( 'currency' => get_woocommerce_currency() ) ), $tax_rate );
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 2014-06-07
      • 2018-07-03
      • 2022-01-13
      • 2019-05-22
      • 2018-06-05
      相关资源
      最近更新 更多