Pat
Pat

Reputation: 717

Hide additional excluding tax price if product is not taxable in Woocommerce

I need to disable displaying the price excluding tax when I set the product without tax. I made a modification in price.php file to add price without VAT.

<p class="netto">
    <?php echo woocommerce_price($product->get_price_excluding_tax()); ?> netto
</p>

If price is not set this "netto price" is still visible in product page.

How can I disable it? Some hooks?

Upvotes: 4

Views: 1074

Answers (3)

LoicTheAztec
LoicTheAztec

Reputation: 253784

First woocommerce_price() function and get_price_excluding_tax() method are deprecated and outdated… They are replaced by wc_price() and wc_get_price_excluding_tax() functions.

Instead of overriding woocommerce template loop/pride.php you could use the following code to achieve what you want with this hooked custom function:

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

    if( $product->get_tax_status() != 'taxable' ){
        $price_excl_vat = wc_get_price_excluding_tax($product);
        echo'<p class="netto">'.wc_price($price_excl_vat).' '. __('netto').'</p>';
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.

The additional "netto" price will be hidden when the product "Tax status" is set to "None" on product archive pages.

Upvotes: 2

user8829426
user8829426

Reputation:

I haven't tested it yet but I expect something like this should be working:

add_action('woocommerce_before_shop_loop_item','custom_remove_loop_price');
function custom_remove_loop_price(){
    global $product;
    if(!$product->price){
        remove_action('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_price',10);
    }
}

Upvotes: 1

Rahul Prajapati
Rahul Prajapati

Reputation: 446

<p class="netto"></p><?php ($product->get_price_excluding_tax() != null) ? echo woocommerce_price($product->get_price_excluding_tax()) : echo 0; ?> netto</p>

Upvotes: 0

Related Questions