Magento: Need to get product collection on catalog price rule applied

Magento: Need to get product collection on catalog price rule applied

July 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Need to get product collection on catalog price rule applied

Retrieving Products When Catalog Price Rule is Applied in Magento

Magento’s catalog price rules allow you to apply discounts based on complex conditions like product categories, customer groups, or special days. If you’ve created such a rule but don’t know how to fetch the corresponding product collection, this guide will walk you through the process.

Understanding Catalog Price Rules

Catalog price rules are defined in app/code/core/Mage/CatalogRule/Model/Rule.php. Each rule has properties like name, description, website ID, customer group ID, priority, and conditions for when it should apply.

The rule’s conditions are applied to products using Mage_Core_Model_Resource_Db_Collection_Abstract::addAttributeToFilter(). This function uses Zend_Db_Expr to create complex SQL queries that filter the product collection based on the rule’s conditions.

Fetching Products with Applied Catalog Price Rules

To fetch a product collection where the catalog price rule is applied, you need to use Magento’s rule application model. Here’s how:

public function getProductsWithAppliedRule()
{
    $ruleCollection = Mage::getModel('catalogrule/rule')->getCollection();
    foreach ($ruleCollection as $rule) {
        $productCollection = Mage::getResourceModel('catalog/product_collection');
        $fromDate = new Zend_Date($rule->getFromDate());
        $toDate = new Zend_Date($rule->getToDate());

        // Apply the rule to the product collection
        Mage::app()->setCurrentStore(Mage::app()->getWebsite()->getDefaultStoreId());
        Mage_CatalogRule_Model_Rule::applyAllRulesToCollection($productCollection);
    }
}

In this code, we first fetch all catalog rules using Mage::getModel('catalogrule/rule')->getCollection(). We then loop through each rule and create a fresh product collection.

Next, we set the current store to Magento’s default store (Mage::app()->getWebsite()->getDefaultStoreId()) before applying the rule. This ensures that the prices are calculated in the context of the correct store.

Finally, we use Mage_CatalogRule_Model_Rule::applyAllRulesToCollection($productCollection) to apply all rules to our product collection. This method updates the price column for each product based on the applied rules.

The result is a product collection where each product’s final price has been adjusted according to the applied catalog price rules.

Conclusion

Retrieving products with applied catalog price rules in Magento requires understanding how these rules are applied and using the built-in rule application model. The key steps involve fetching all rules, creating a fresh product collection for each rule, setting the current store context, and applying all rules to the collection.

Remember that catalog price rules can be complex, so always ensure your collections are filtered correctly based on the specific conditions defined in your rules.

For more advanced tutorials on Magento development, consider visiting Laravel Company’s Magento Development Blog. We regularly publish detailed guides and best practices for working with Magento.

Catalog Database Customers Pricing

Our network