简体   繁体   中英

Calculations on product price without tax

I'm using this WooCommerce function woocommerce_price($product->get_price_excluding_tax()); to display the price without TAX.

But I want to make some calculations before with this price, for example, to divide it ...

With echo woocommerce_price($product->get_price_excluding_tax()); it displays the price with currency, for example €13.80 . So I remove the € symbol from the string with this code:

$taxed_price = woocommerce_price($product->get_price_excluding_tax());
$taxed_price = str_replace("€","",$taxed_price);
echo $taxed_price = str_replace('"', "", $taxed_price);
echo $excl_tax_price = $taxed_price/2;

But my calculation on $taxed_price/2 doesn't work, it don't get divided.

Can anybody help me?

Thanks.

First woocommerce_price() is a deprecated , instead you should use wc_price() function.

This function allow to format the price with a currency symbol.

You just have to use it after your calculations, to avoid using str_replace() php function to remove that formatted currency symbol and get the correct calculations, this way:

// Making your calculation (dividing)
$excl_tax_price = $product->get_price_excluding_tax()/2;

// Displaying this calculation (non formatted, no currency symbol):
echo $excl_tax_price;

// Displaying this formatted calculation (with currency symbol):
echo wc_price($excl_tax_price);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM