Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide price excluding tax when price field is empty in Woocommerce

I have a setup. For products, I display prices with VAT (info added in the suffix). However, I must include a net price under this price (only in single-product-view). In addition, I have products that are without a price and with them using the Call For Price plugin to display a button to send a price inquiry.

I display the net price adding:

<?php echo woocommerce_price ($ product-> get_price_excluding_tax ()); ?>

added in the template /woocommerce/single-product/price.php.

The problem is that in the case of a product without a price, I still see this value without tax (net) with the amount of 0.00. This one with VAT is deleted automatically.

I try also:

<?php if(woocommerce_price ($product-> get_price_excluding_tax ()) != '0.00') // If price is not equal to 0.00
{ echo woocommerce_price ($product-> get_price_excluding_tax ()); }
?>

and

<?php if( !empty(woocommerce_price ($product-> get_price_excluding_tax ()))) { echo woocommerce_price ($product-> get_price_excluding_tax ()); } ?>

Still 0,00 is visible.

How could I remove this net price in products that do not have the price indicated? Any ideas?

like image 878
Pat Avatar asked Dec 19 '25 04:12

Pat


1 Answers

Since Woocommerce version 3, woocommerce_price() function is outdated and deprecated. It has been replaced by wc_price() formatting function.

Same thing for get_price_excluding_tax() method. It has been replaced by this function (where $product is an instance of the WC_Product object):

 wc_get_price_excluding_tax( $product );

So the correct code should be:

<?php 
    $price_excl_tax = wc_get_price_excluding_tax( $product );
    echo $price_excl_tax > 0 || ! empty( $price_excl_tax ) ? wc_price( $price_excl_tax ) . __(' netto') : '';
?>

Tested and works


Before Woocommerce version 3, you will use:

<?php 
    $price_excl_tax = $product->get_price_excluding_tax();
    echo $price_excl_tax > 0 || ! empty( $price_excl_tax ) ? woocommerce_price( $price_excl_tax ) . __(' netto') : '';
?>
like image 121
LoicTheAztec Avatar answered Dec 21 '25 18:12

LoicTheAztec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!