Advanced Dynamic Pricing For WooCommerce

Check the active rules for the product

The adp_functions()->getActiveRulesForProduct() helper function is the key to unlocking this insight. This guide provides a ready-to-use code snippet that retrieves all active pricing rules for a product. You will learn how to install the code, understand how it works, and apply it to your store.

The Complete Code Snippet

Copy and paste the following PHP code into your site to display a message on the product page if no pricing rules are applied:

Replace "Your text" with the message you want to display, such as "No active rules apply to this product" or a custom sales note.

How the Code Works

This snippet attaches a function to the woocommerce_single_product_summary action, which places the output in the product summary section of the single product page. The logic is structured to handle simple, variable, and grouped products correctly by using the plugin’s helper function.

Step 1: Access the Global Product Object

The code starts by declaring global $product;, which gives access to the current WooCommerce product object.

Step 2: Define a Helper to Collect Rules

A closure $calculateProduct is created to retrieve rules for a given product. It does this by leveraging adp_functions()->getActiveRulesForProduct().

ParameterDescription
ProductThe product or variation object to evaluate.
Quantity (Qty)The quantity passed to the function (here always 1).
useEmptyCartDefaults to false. When true, the function uses an empty cart for calculations, which is useful for rule evaluation without affecting the actual cart.

For Simple Products

If the product is an instance of WC_Product_Simple, the code directly calls adp_functions()->getActiveRulesForProduct() and adds each rule to a temporary array.

For Variable Products

For WC_Product_Variable, the code loops through each available variation. It retrieves the variations as objects using get_available_variations("obj"). For each variation, the same helper function collects active rules, storing them in a unique array keyed by rule ID to avoid duplicates.

Step 3: Handle Grouped Products

If the product is a grouped product (WC_Product_Grouped), the code fetches its visible child products. It then recursively calls $calculateProduct on each child to collect all rules that apply to any item in the group.

Step 4: Display a Custom Message When No Rules Are Found

After collecting the rules for the appropriate product type, the code checks count($rules). If the count is zero, it outputs your custom message using echo. This allows you to inform customers or staff when no discounts are active.

Using the Helper Function Directly

You can also use adp_functions()->getActiveRulesForProduct() in your own custom code. The function signature is:

  • $productId (int|WC_Product): The product ID or object.
  • $qty (int): The quantity to consider for rule eligibility.
  • $useEmptyCart (bool): When true, the plugin simulates an empty cart for calculation. This is useful for previews or standalone rule checks.

The function returns an array of applied rule objects, each of which is an instance of WDP_Rule. These objects contain methods like getId()getName(), and getRuleType() for further inspection.

Customisation Options

The code is designed for flexibility. You can modify it to suit your needs.

Change the Message or Its Position

The message is currently echoed inside the product summary. To place it elsewhere, change the hook or adjust the output.

Output a List of Active Rules

Instead of a generic message, you can display the names of all active rules.

Combine with Custom CSS

Add a CSS class to the message and style it in your theme.

Then in your theme’s stylesheet:

Conditionally Exclude Product Types

If you want to skip the check for certain product types, add a condition at the beginning of the hook.

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