Hi,
I'm using woocommerce v 2.0.13.
This is what I would like to accomplish: having 3 decimal places for product prices (set from woocommerce settings- cataloge - pricing options) and yet having just 2 decimal for Total price and Subtotal price on cart, checkout, review order and emails.
Thanks
Liam Bailey answers:
Hi there,
The easiest way to do this is to override the template files that are used for those pages and change the decimal count there. Basically, you can override any template in the templates folder of the Woocommerce plugin directory, You simply create a Woocommerce directory in your theme directory and then any template you want to override you move that template into your Woocommerce folder in your theme. Note: if the template file is in a nested folder you need to bring all the folder structure into your theme's Woocommerce folder.
It wont be hard for you to work out from the folder structure which templates you need to override and trial and error will do the rest.
If you would rather use filters you can find them by going through the aforementioned template files in the woocommerce directory, for example you can change the cart value output with woocommerce_cart_item_price_html. Let me know if you need any further advice.
David90639 comments:
Thanks for your reply Liam. I found the templates files and I tried on cart/totals.php
The following was the code I used but work it retruns 0.00. I think its because 'get_cart_subtotal' is passing the whole string of the price including currency name and currency symbol with the float price value itself:
$subtotal = $woocommerce->cart->get_cart_subtotal();
echo number_format($subtotal, 2, '.', '');
Regarding the filters I'm not sure if I know how to work with them. I would appreciate if you could help me more regarding the issue and if there is any function which can overwrite the subtotal and total prices everywhere in woocommerce and format them to have 2 decimal places.
Thank you,
Liam Bailey comments:
If you want it changing "everywhere in woocommerce" why can you not just change it in settings? If you want to use code to change it absolutely everywhere then use update_option to programatically change the setting. That setting is the only place you can change the decimals for every price display in one place. If you can't change the setting then it is a painstaking process of finding where your price displays are in the template files and either finding a filter there or overriding the template.
The problem is that each price output uses woocommerce_price or a function that uses woocommerce_price to build the price, and woocommerce_price uses the number of decimals put in the settings, so you either have to write a new function like woocommerce_changed_price and then override the template files with files calling your function instead of woocommerce_price, either that or find filters that work directly on the output after woocommerce_price, and then use the code you would have put in woocommerce_changed_price to rebuild the value with the right amount of decimals.
I am happy to do this for you, if you want me to please increase the price to $30 and please give me ftp details.
Liam Bailey comments:
For example in totals.php $woocommerce->cart->get_cart_subtotal(); uses woocommerce_price to build a fully formatted price. So this should work:
$subtotal = $woocommerce->cart->get_cart_subtotal();
echo number_format(floatval($subtotal), 2, '.', '');
Put the value back to float, and then output with your right number of decimals, but you need to add your currency so:
$subtotal = $woocommerce->cart->get_cart_subtotal();
echo get_woocommerce_currency_symbol() . number_format(floatval($subtotal), 2, '.', '');
David90639 comments:
Liam, as I have mentioned in my original question. I need to have two kind of price formatting: 3 decimal for product prices and 2 decimal for subtotal and total prices. I do not have two different options on the woocommerce settings for the number of decimals.
David90639 comments:
Liam I have tried your code but its not working. It returns $0.00.
echo get_woocommerce_currency_symbol() . number_format(floatval($subtotal), 2, '.', '');