0
votes

I want to show the product price including and exclunding tax under each product in the catalog page of my Woocommerce shop.

Its already working, but it is not showing anything for variable products where I have only one variation. On single products it is also working.

Also I do get the notification:

Notice: WC_Product::get_price_including_tax ist seit Version 3.0 veraltet! Benutze stattdessen wc_get_price_including_tax.

Notice: WC_Product::get_price_excluding_tax ist seit Version 3.0 veraltet! Benutze stattdessen wc_get_price_excluding_tax.

But if I do so, it is not working anymore at all.

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

1 Answers

0
votes

The wc_get_price_including_tax and wc_get_price_excluding_tax functions expect $product as an argument. So you will have to pass it like this:

wc_get_price_including_tax( $product )

Also it seems like a good idea to get the product's tax rate instead of hard coding it in. Maybe in the future you will have products that do not have a 19% tax rate. I also included the currency argument to the wc_price function so the price will be shown in the shop's currency.

You can use the following snippet that will get the product's tax rate and prints the prices including and excluding tax:

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

}