Override decimal places in the cart
WooCommerce uses a single global decimal setting (WooCommerce → Settings → General → Number of decimals) that applies to all price displays across your store. However, when Advanced Dynamic Pricing for WooCommerce calculates percentage-based or complex discounts, the resulting prices can have more decimal places than your global setting allows — causing prices in the cart or checkout to appear rounded in a way that doesn’t accurately reflect the discount applied.
This article provides ready-to-use snippets to override the number of decimal places only on specific cart-related pages, without affecting the rest of your store.
Code Sample 1 — Cart & Checkout Pages
Add the following snippet to your theme’s functions.php or a custom plugin:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function adp_set_decimals(){ return 3; } add_action("woocommerce_before_cart", function(){ add_filter("wc_get_price_decimals","adp_set_decimals",123); }); add_action("woocommerce_review_order_before_cart_contents", function(){ add_filter("wc_get_price_decimals","adp_set_decimals",123); }); add_action("woocommerce_after_cart", function(){ remove_filter("wc_get_price_decimals","adp_set_decimals",123); }); add_action("woocommerce_review_order_after_order_total", function(){ remove_filter("wc_get_price_decimals","adp_set_decimals",123); }); |
Code Sample 2 — Mini-Cart
If you also need the override to apply to the mini-cart (the cart widget in the header/sidebar), use this separate snippet alongside the one above:
|
1 2 3 4 5 6 7 8 9 |
function adp_set_decimals(){ return 3; } add_action("woocommerce_before_mini_cart", function(){ add_filter("wc_get_price_decimals","adp_set_decimals",123); }); add_action("woocommerce_after_mini_cart", function(){ remove_filter("wc_get_price_decimals","adp_set_decimals",123); }); |
Note: If you are using both snippets, make sure the adp_set_decimals() function is only declared once in your code. Define it once at the top and reference it in both filter calls. Declaring the same function name twice will cause a PHP fatal error.
Code Explained (for Developers)
The helper function
| Element | Description |
|---|---|
adp_set_decimals() | A simple callback function that returns the desired number of decimal places. Change 3 to any integer to set your preferred precision (e.g. 2 for two decimals, 4 for four). |
Cart & Checkout — Hook breakdown
| Hook | Type | Purpose |
|---|---|---|
woocommerce_before_cart | add_action | Fires just before the cart table is rendered. Used to activate the decimal override at the start of the cart page. |
woocommerce_review_order_before_cart_contents | add_action | Fires before the order review table on the checkout page. Activates the override for checkout price display as well. |
woocommerce_after_cart | add_action | Fires after the cart table is rendered. Used to remove the override so it doesn’t affect the rest of the page. |
woocommerce_review_order_after_order_total | add_action | Fires after the order total row on checkout. Removes the override cleanly after the checkout summary is rendered. |
Mini-Cart — Hook breakdown
| Hook | Type | Purpose |
|---|---|---|
woocommerce_before_mini_cart | add_action | Fires just before the mini-cart widget content is rendered. Activates the decimal override. |
woocommerce_after_mini_cart | add_action | Fires after the mini-cart content is rendered. Removes the override immediately. |
The filter itself
| Element | Description |
|---|---|
wc_get_price_decimals | A WooCommerce filter that controls how many decimal places are used when formatting prices. Normally it reads from your WooCommerce general settings. |
Priority 123 | A deliberately high priority value to ensure this filter runs after any other filters — including those set by WooCommerce core or other plugins — and is not overridden by them. The same priority value must be used in both the add_filter() and remove_filter() calls, otherwise the filter will not be removed correctly. |
Key concept: The filter is intentionally added and removed in pairs using matching action hooks (before/after). This is called scoped filtering — it ensures the decimal override is active only during the rendering of specific sections, and does not leak into other parts of the page such as related products, upsells, or footer widgets.
How to Apply This Code
- Open Appearance → Theme File Editor in your WordPress admin, or open the Code Snippets plugin.
- Paste the relevant snippet(s) into your theme’s
functions.phpor create a dedicated snippet. - If using both snippets, ensure
adp_set_decimals()is defined only once. - Save and visit your Cart and Checkout pages to verify that prices now display with the correct number of decimal places.
- Check the mini-cart widget if you applied the second snippet.
Editing
functions.phpdirectly can be overwritten during theme updates. A child theme or the Code Snippets plugin is the safer long-term approach.
Customizing the Number of Decimals
The return 3; value in adp_set_decimals() is fully customizable. Here are some common use cases:
| Return value | Use case |
|---|---|
2 | Standard two-decimal currency display (e.g. $12.50) |
3 | High-precision pricing for percentage discounts (e.g. $12.505) |
4 | Very high precision for B2B or wholesale pricing |
0 | No decimals, for stores using whole-number pricing |
When Should You Use This?
- Dynamically calculated discount prices are being rounded incorrectly in the cart or checkout.
- You run a B2B or wholesale store where precise pricing (3+ decimal places) is required for line-item accuracy.
- Your store uses per-unit pricing for large quantities where small decimal differences compound significantly.
- You want decimal precision in the cart without changing the global WooCommerce decimal setting that affects product pages and other areas.