Woocommerce在产品目录中显示价格含税和不含税

时间:2020-06-24 16:08:31

标签: woocommerce

我想在我的Woocommerce商店的目录页面中显示每种产品下的产品价格(含税)。

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

我也收到通知:

注意:WC_Product :: get_price_ included_tax ist seit版本3.0版本! Benutze州wc_get_price_includestax。

注意:WC_Product :: get_price_ included_tax ist seit版本3.0版本!贝努兹(Benetze stattdessen)wc_get_price_ included_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>';
    }
}

1 个答案:

答案 0 :(得分:0)

wc_get_price_including_taxwc_get_price_excluding_tax函数期望$product作为参数。因此,您必须像这样通过它:

wc_get_price_including_tax( $product )

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

您可以使用以下代码段获取产品的税率并打印包含和不含税的价格:

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 );

}