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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
add_action("woocommerce_single_product_summary", function () { global $product; $calculateProduct = function ($product) { $rules = []; if ($product instanceof WC_Product_Variable) { foreach ( $product->get_available_variations("obj") as $variation ) { foreach (adp_functions()->getActiveRulesForProduct($variation, 1) as $rule) { $rules[$rule->getId()] = $rule; } } } elseif ($product instanceof WC_Product_Simple) { foreach (adp_functions()->getActiveRulesForProduct($product, 1) as $rule) { $rules[$rule->getId()] = $rule; } } return $rules; }; $rules = []; if ($product instanceof WC_Product_Grouped) { $children = array_filter( array_map( 'wc_get_product', $product->get_children() ), 'wc_products_array_filter_visible_grouped' ); foreach ($children as $childProduct) { $rules = $calculateProduct($childProduct); } } else { $rules = $calculateProduct($product); } if (count($rules) === 0) { echo "Your text"; } }); |
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().
| Parameter | Description |
|---|---|
| Product | The product or variation object to evaluate. |
| Quantity (Qty) | The quantity passed to the function (here always 1). |
| useEmptyCart | Defaults 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:
|
1 |
adp_functions()->getActiveRulesForProduct( $productId, $qty = 1, $useEmptyCart = false ); |
$productId(int|WC_Product): The product ID or object.$qty(int): The quantity to consider for rule eligibility.$useEmptyCart(bool): Whentrue, 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.
|
1 2 3 4 5 6 7 |
// Example: Show message before the add-to-cart button add_action( 'woocommerce_before_add_to_cart_button', function() { // ... your rule-checking logic ... if ( count($rules) === 0 ) { echo '<div class="no-discount-notice">Your custom message</div>'; } } ); |
Output a List of Active Rules
Instead of a generic message, you can display the names of all active rules.
|
1 2 3 4 5 6 7 |
if ( count($rules) > 0 ) { echo '<ul class="active-discounts">'; foreach ( $rules as $rule ) { echo '<li>' . esc_html( $rule->getName() ) . '</li>'; } echo '</ul>'; } |
Combine with Custom CSS
Add a CSS class to the message and style it in your theme.
|
1 |
echo '<p class="adp-no-rule-message">Your custom message</p>'; |
Then in your theme’s stylesheet:
|
1 2 3 4 5 |
.adp-no-rule-message { color: #999; font-style: italic; margin-top: 10px; } |
Conditionally Exclude Product Types
If you want to skip the check for certain product types, add a condition at the beginning of the hook.
|
1 2 3 4 5 6 7 |
add_action("woocommerce_single_product_summary", function () { global $product; if ( $product->is_type( 'external' ) ) { return; } // ... rest of the code }); |