6

I need to check if a product has a catalog price rule and if it has, it needs to display a specific div.

So I need something like:

<?php if catalog price rule : ?>
<div>
Text
</div>
<?php endif; ?>

How can I achieve that?

6

If you don't have many rules or many products you can try this:

$website = Mage::app()->getWebsite();
$product = $this->getProduct();
$rules = Mage::getModel('catalogrule/rule')->getCollection()
    ->addWebsiteFilter($website) //filter rules for current site
    ->addIsActiveFilter(1); //filter active rules

foreach ($rules as $rule) {
    $affectedProductIds = $rule->getMatchingProductIds();
    if (in_array($product->getId(), $affectedProductIds)) {
        //product has a price rule set for it
    }
}

The quick and dirty idea would be to search for a record in the table catalogrule_product.
All products that are affected by catalog rules are referenced in that table.

$resource = Mage::getSingleton('core/resource');
$connection = $resource->getConnection('core_read');
$tableName = $resource->getTableName('catalogrule_product');
$count = $connection->fetchOne('SELECT COUNT(1) as c FROM '.$tableName.' WHERE product_id = '. $product->getId());

if ($count == 1) { 
    //you have a rule for the product
}

But this is not the best practice.

8
  • Thanks! But we do have a lot of products, 45.000+. So I do not know if that is a good idea, isn't?
    – JGeer
    Nov 9 '15 at 16:18
  • the second approach should work.
    – Marius
    Nov 9 '15 at 16:39
  • i will use second option... it better for speed..
    – Amit Bera
    Nov 9 '15 at 16:59
  • I tried the second option, but that does not work. Do I need to edit something inside your code?
    – JGeer
    Nov 9 '15 at 18:03
  • See how the variable $count looks like. It may be an array or something else.
    – Marius
    Nov 9 '15 at 18:22
4

Old question, but for others looking, this seems to be an efficient method:

$product = $this->getProduct();
Mage::getModel('catalogrule/rule')->loadProductRules($product);
var_dump($product->getMatchedRules());

See Mage_CatalogRule_Model_Rule for reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.