Display collection of Shopping cart rules and products categories associated to each rule

Display collection of Shopping cart rules and products categories associated to each rule

September 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Display collection of Shopping cart rules and products categories associated to each rule

Display Collection of Shopping Cart Rules and Associated Products Categories

In Magento, retrieving shopping cart rules and their associated products is a common requirement for displaying promotions on category list pages. Let’s explore how to loop through all the shopping cart rules and retrieve the products/categories linked to each rule.

Understanding Magento Rule Conditions

Magento uses serialized data structures to store rule conditions. The salesrule/rule_collection model contains rules with conditions stored in the rule_conditions_serialized field. This field holds an array of condition objects, each representing a specific rule criterion like product category or price range.

When you retrieve a rule collection using Mage::getResourceModel('salesrule/rule_collection')->load(), it returns an object containing active rules along with their conditions serialized into a string format. To work with these conditions, you need to unserialize them and loop through the resulting array of condition objects.

Unserializing Rule Conditions

Let’s start by retrieving all active shopping cart rules and unserializing their conditions:

$rules = Mage::getResourceModel('salesrule/rule_collection')->load();

foreach ($rules as $rule) {
    if ($rule->getIsActive()) {
        // Unserialize the rule conditions
        $conditions = unserialize($rule->getConditionsSerialized());

        // Loop through each condition object
        foreach ($conditions as $condition) {
            // Process each condition here
            var_dump($condition);
        }
    }
}

In this example, $conditions will be an array of serialized condition objects. You can loop through these objects to extract the desired information.

Extracting Product SKUs from Rule Conditions

The condition object for product-based rules contains the attribute, operator, and value fields. The value field holds a comma-separated string of product SKUs when the rule is based on specific products. However, you’ve already discovered that directly accessing var_dump($conditions) doesn’t reveal the SKU values.

To extract SKUs from the condition objects, you need to traverse the nested arrays within $conditions. Here’s an updated approach using the salesrule/rule_collection model:

$rules = Mage::getResourceModel('salesrule/rule_collection')->load();

foreach ($rules as $rule) {
    if ($rule->getIsActive()) {
        // Reuse loaded rule to get conditions in array format
        $conditions = $rule->getConditions()->asArray();
        
        foreach( $conditions['conditions'] as $_conditions ):
            foreach( $_conditions['conditions'] as $_condition ):
                if ($_condition['attribute'] == 'sku') {
                    $string = explode(',', $_condition['value']);
                    for ($i=0; $i<count($string); $i++) {
                        $skus[] = trim($string[$i]);
                    }
                }
            endforeach;
        endforeach;
    }
}

return $skus;

This code retrieves the rule conditions as an array and then loops through nested arrays to find condition objects with the attribute set to ‘sku’. It extracts SKUs from the value field, trims whitespace, and adds them to the $skus array.

Conclusion

Retrieving shopping cart rules and their associated product categories involves unserializing rule conditions and traversing the resulting nested arrays. By using the correct Magento models and methods, you can efficiently loop through active rules, extract condition objects, and process the necessary information like SKUs or category IDs.

Remember to consider performance implications when dealing with large datasets. Optimize your code by limiting the number of loops or using more efficient data structures where possible.

For further guidance on Magento development best practices, check out our comprehensive tutorials and guides at Laravel Company.

Checkout Catalog Pricing

Our network