Advanced Dynamic Pricing For WooCommerce

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):

 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

ElementDescription
wdp_custom_override_centsA 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_priceThe current overridden price value. null by default — returning null tells the plugin to use its own calculated price without modification.
$priceThe 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.
$contextA string describing where the price is being calculated (e.g. 'product', 'cart'). Can be used to limit rounding to specific display contexts if needed.
$productThe WC_Product object for the current product, providing access to all product data including category IDs.
10, 4Hook priority 10 (standard); 4 means the callback accepts four arguments — all four parameters listed above must be declared.

Exclude block

ElementDescription
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(...) > 0If at least one category matches, the product is in an excluded category and the function exits early without applying rounding.

Include block

ElementDescription
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.05Subtracts 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:

  1. Go to Products → Categories in your WordPress admin.
  2. Hover over a category name in the list.
  3. 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:

GoalFormulaExample input → output
Round to nearest whole numberround( $price, 0 )$9.67$10.00
Always round up to whole numberceil( $price )$9.10$10.00
Always round down to whole numberfloor( $price )$9.90$9.00
Charm pricing (end in .95)round( $price - 0.05, 1 )$10.00$9.95
Round to nearest .50round( $price * 2 ) / 2$9.30$9.50
Round to nearest .99floor( $price ) + 0.99$9.30$9.99

How to Apply This Code

  1. Open Appearance → Theme File Editor in your WordPress admin, or open the Code Snippets plugin.
  2. Paste the snippet into your theme’s functions.php or create a new dedicated snippet.
  3. Replace array( 1, 2, 3 ) in the exclude block with the category IDs you want to skip.
  4. Replace array( 3, 4, 5 ) in the include block with the category IDs you want to apply rounding to.
  5. Adjust the rounding formula if needed (see the table above).
  6. Save and navigate to a product page in one of your included categories to verify the price is rounded correctly.

Editing functions.php directly 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.

Have questions? Please submit a support request. We're always happy to help!

Advanced Dynamic Pricing
for WooCommerce
PRO

From $60
Buy
✓ 30-day money-back guarantee