Rounding up prices for the specified categories
Advanced Dynamic Pricing for WooCommerce provides a dedicated filter hook — wdp_custom_override_cents — that allows developers to apply custom price rounding logic to dynamically calculated prices on a per-category basis. This is useful when you need precise control over how discounted prices are displayed: rounding some categories to a specific cent value while leaving others completely untouched.
This article walks through a ready-to-use snippet that demonstrates both excluding specific categories from rounding and including others with a custom rounding formula.
Code Sample
Add the following snippet to your theme’s functions.php or a custom plugin (we recommend using the Code Snippets plugin):
|
1 2 3 4 5 6 7 8 9 10 11 |
add_filter( "wdp_custom_override_cents", function ( $custom_price, $price, $context, $product ) { # exclude if ( count( array_intersect( array( 1, 2, 3 ), $product->get_category_ids() ) ) > 0 ) { return $custom_price; } # include if ( count( array_intersect( array( 3, 4, 5 ), $product->get_category_ids() ) ) > 0 ) { $custom_price = round( $price - 0.05, 1 ); } return $custom_price; }, 10, 4 ); |
Before using this snippet, replace the category ID arrays with your own. See How to Find Your Category IDs below.
Code Explained (for Developers)
Hook registration
| Element | Description |
|---|---|
wdp_custom_override_cents | A custom filter hook provided by Advanced Dynamic Pricing. It fires during the plugin’s internal price calculation and allows you to override the final cent value of a dynamically computed price. |
$custom_price | The current overridden price value. null by default — returning null tells the plugin to use its own calculated price without modification. |
$price | The dynamically calculated price produced by the plugin’s active rules, before any cent override is applied. This is the value you should base your rounding formula on. |
$context | A string describing where the price is being calculated (e.g. 'product', 'cart'). Can be used to limit rounding to specific display contexts if needed. |
$product | The WC_Product object for the current product, providing access to all product data including category IDs. |
10, 4 | Hook priority 10 (standard); 4 means the callback accepts four arguments — all four parameters listed above must be declared. |
Exclude block
|
1 2 3 |
if ( count( array_intersect( array( 1, 2, 3 ), $product->get_category_ids() ) ) > 0 ) { return $custom_price; } |
| Element | Description |
|---|---|
array( 1, 2, 3 ) | The list of excluded category IDs. Products belonging to any of these categories will be skipped — the function returns $custom_price unchanged (which is null by default, meaning no override). Replace these with your own category IDs. |
$product->get_category_ids() | Returns an array of all category IDs assigned to the current product, including parent categories. |
array_intersect() | Compares the two arrays and returns any IDs that appear in both — i.e. the categories this product shares with your exclusion list. |
count(...) > 0 | If at least one category matches, the product is in an excluded category and the function exits early without applying rounding. |
Include block
|
1 2 3 |
if ( count( array_intersect( array( 3, 4, 5 ), $product->get_category_ids() ) ) > 0 ) { $custom_price = round( $price - 0.05, 1 ); } |
| Element | Description |
|---|---|
array( 3, 4, 5 ) | The list of included category IDs. Products in these categories will have the custom rounding formula applied. Replace these with your own category IDs. |
$price - 0.05 | Subtracts 0.05 from the calculated price before rounding. This is a charm pricing technique — it nudges prices like $10.00 down to $9.95 rather than up to $10.00, which is more appealing to shoppers. Adjust or remove this offset to suit your needs. |
round( ..., 1 ) | Rounds the result to 1 decimal place (e.g. $9.95, $14.75). Change 1 to 0 for whole-number rounding, or 2 for standard two-decimal rounding. |
Key concept: The exclude block always runs first. If a product belongs to both an excluded and an included category, the exclusion takes priority because the function returns early before the include block is ever reached. This is intentional — use it to handle category overlap in multi-level category trees.
How to Find Your Category IDs
To replace the placeholder IDs (1, 2, 3 and 3, 4, 5) with your real category IDs:
- Go to Products → Categories in your WordPress admin.
- Hover over a category name in the list.
- Look at the URL shown in the bottom of your browser. It will contain
tag_ID=XX— that number is the category ID.
Alternatively, click to edit a category and check the URL in the address bar for the same tag_ID parameter.
Rounding Formula Reference
You can replace round( $price - 0.05, 1 ) with any of the following depending on your pricing strategy:
| Goal | Formula | Example input → output |
|---|---|---|
| Round to nearest whole number | round( $price, 0 ) | $9.67 → $10.00 |
| Always round up to whole number | ceil( $price ) | $9.10 → $10.00 |
| Always round down to whole number | floor( $price ) | $9.90 → $9.00 |
| Charm pricing (end in .95) | round( $price - 0.05, 1 ) | $10.00 → $9.95 |
| Round to nearest .50 | round( $price * 2 ) / 2 | $9.30 → $9.50 |
| Round to nearest .99 | floor( $price ) + 0.99 | $9.30 → $9.99 |
How to Apply This Code
- Open Appearance → Theme File Editor in your WordPress admin, or open the Code Snippets plugin.
- Paste the snippet into your theme’s
functions.phpor create a new dedicated snippet. - Replace
array( 1, 2, 3 )in the exclude block with the category IDs you want to skip. - Replace
array( 3, 4, 5 )in the include block with the category IDs you want to apply rounding to. - Adjust the rounding formula if needed (see the table above).
- Save and navigate to a product page in one of your included categories to verify the price is rounded correctly.
Editing
functions.phpdirectly can be overwritten during theme updates. A child theme or the Code Snippets plugin is the safer long-term approach.
When Should You Use This?
Your theme or marketplace feed requires prices to conform to a specific decimal format per product category.
You apply percentage-based discounts and the resulting prices end in awkward decimal values like $12.333 or $7.666.
You want charm pricing (e.g. ending in .95 or .99) applied automatically to discounted products in specific categories.
Your store has mixed category types — some requiring precise decimal pricing (e.g. B2B wholesale) and others needing clean consumer-friendly prices.
You want category-level control over price display without creating separate discount rules for each category.