Advanced Dynamic Pricing For WooCommerce

Example Code for Calculating Percentage Value in Sale Badge

The Advanced Dynamic Pricing for WooCommerce plugin allows you to create sophisticated discount rules based on user roles, purchase quantities, cart totals, and many other conditions. However, the default WooCommerce sale badge only displays a generic “Sale!” label, even when a dynamic discount is applied. This generic badge does not communicate the actual value the customer is receiving, which reduces the persuasive power of your pricing strategy.

Displaying the exact percentage saved (e.g., “-15%”) is a well‑known conversion booster. It makes the discount tangible and creates urgency. The code example provided in this guide replaces the standard sale badge with a dynamic percentage badge that works for both standard WooCommerce sales and discounts created by the Advanced Dynamic Pricing plugin. It correctly handles simple products, variable products, external products, and product variations, ensuring that all your discounts are visually communicated effectively.

Note: This guide is based on the official developer documentation for the Advanced Dynamic Pricing for WooCommerce plugin. The code works with both the free and Pro versions, though the Pro version includes additional rule types that are also supported by the snippet.


The Complete Code Snippet

The following PHP snippet attaches a custom function to the woocommerce_sale_flash filter. It calculates the discount percentage based on the product type, the active pricing rules, and the standard WooCommerce sale price. If a discount is found, it returns a formatted percentage badge; otherwise, it falls back to the original sale flash text.

How the Code Works

The function processes the product and calculates the discount percentage based on the product type. It uses the woocommerce_sale_flash filter, which controls the sale badge content. The code is structured to handle three product types: variable, simple, external, and variations.

For Variable Products

The function first uses adp_functions()->calculateProduct($product, 1) to get a processed product object. This helper method, documented in the plugin’s developer guides, calculates pricing for a given product based on active rules.

  • If dynamic pricing rules are applied, the code retrieves the lowest price variation (getLowestPriceProduct()). It then accesses the calculated discounted price (getCalculatedPrice()) and the original price before the discount (getOriginalPrice()).
  • If no dynamic rules apply but the product is on sale via the standard WooCommerce sale price, the code iterates through all variations, calculates the percentage discount for each, and stores the highest one.

The areRulesApplied() method ensures that the discount percentage is only calculated when a dynamic rule is actually affecting the price.

For Simple, External Products and Variations

The logic is similar but streamlined because these product types have a single price.

  • If dynamic pricing rules are applied, the code retrieves the original price and the calculated discounted price from the processed product object.
  • If no dynamic rules apply but the product is on sale, it calculates the percentage using the standard WooCommerce sale price.

Understanding the adp_functions()->calculateProduct() Helper

This helper is the key to the integration. It is available in both the free and Pro versions of the plugin. The function takes a product object (or ID) and a quantity as parameters, and it returns a ProcessedProductSimple or ProcessedVariableProduct object. This object provides several useful methods:

MethodDescription
getOriginalPrice()Returns the product’s original price before any discounts are applied.
getCalculatedPrice()Returns the price after the active pricing rules have been applied.
areRulesApplied()Returns true if at least one pricing rule affects this product.
getLowestPriceProduct() (variable products)Returns the variation object with the lowest calculated price.

Using these methods allows you to accurately determine the discount amount even when multiple overlapping rules are active.

Output Format

If a discount percentage is calculated, the function returns the string ' - ' . $percentage . '%', which is added to the existing sale flash content. If no discount is found, it returns the original $flash value (typically “Sale!”).


Step‑by‑Step Implementation

Follow these steps to add the custom code to your WooCommerce store.

Step 1: Choose where to add the code

You have two safe options for adding custom PHP code:

  • Option A (Recommended): Use the free Code Snippets plugin.
    Install the plugin from Plugins → Add New by searching for “Code Snippets”. This is the safest method because it allows you to add, activate, and deactivate snippets without ever editing your theme files.
  • Option B: Add to your child theme’s functions.php file.
    If you are comfortable editing theme files, add the code to your active child theme’s functions.php file. Never add custom code directly to a parent theme, because it will be lost when the theme is updated.

Step 2: Insert the snippet

  • If using Code Snippets:
  • If editing functions.php:

Step 3: Test the functionality

  1. Create a test product and apply a discount rule using the Advanced Dynamic Pricing plugin (e.g., 15% off for a specific user role).
  2. Log in as a user who qualifies for the discount.
  3. View the product on the frontend of your site.
  4. The sale badge should display the calculated percentage, for example “-15%”.

If the badge shows the correct percentage, the implementation is successful.


Customisation Options

The code snippet can be easily adapted to different display requirements.

Change the badge text format

Modify the return string to change how the badge appears.

You can also add an icon or HTML for better visual impact:

Show “Up to X%” for variable products

If you prefer to show the maximum possible discount for variable products (e.g., “Up to 20% off”), modify the variable product section:

Include the amount saved

To display the actual monetary amount saved instead of (or in addition to) the percentage:

Conditionally show the badge only for certain products

Add a conditional check to limit the badge to specific product categories:

Display the badge on the cart page

The woocommerce_sale_flash filter typically only works on product pages and archives. To display a discount indicator in the cart, you would need to use a different hook (e.g., woocommerce_cart_item_name or a custom filter).

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