1

I want to display a product label if a cart rule has been applied to a product but the label is being displayed for all products even those who don't qualify for the cart rules.

Here is the code I am testing:

$rules =  Mage::getModel('salesrule/rule')->getCollection();?>
        //echo count($rules);

    <?php foreach ($rules as $rule) :?> 
        <?php   $hasPromo=$rule->getConditions()->validate($_product) ;
            if ($hasPromo) 
            {
                $Tag = $rule->getDescription(); 
                ?>
                <div class="libelle-promo"><?php echo $Tag; ?></div>
                <?php 
            }
            else { ?>
            <div class="libelle-promo" style="display: none;"></div>
            <?php } 
          ?>
        <?php endforeach;?>

2 Answers 2

0
<?php
        $currentcustomerGroupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
        $proid = $_product->getId(); // your prduct ID

        $resource = Mage::getSingleton('core/resource');
        $connection = $resource->getConnection('core_read');

        $ruleid = $connection->fetchOne("SELECT `rule_id` FROM `catalogrule_product` WHERE `product_id` ='$proid' 
            AND `customer_group_id` = '$currentcustomerGroupId'");

        $rulename = $connection->fetchOne("SELECT `name` FROM `catalogrule` WHERE `rule_id` = '$ruleid' AND 'is_active' = 1 ");


        echo $rulename;

    ?>
1
  • use this code wherever like view page, list, checkout, etc.,
    – Raghu
    Jul 15, 2017 at 12:32
0

Thank you very much for your reply .But i want to display the cart shopping rule not the catalog rule this is why I used applied_rule_ids in sales_quote_item but the label is displayed only if on product is added to to cart . `$tableName = $resource->getTableName('sales_flat_quote_item');

//echo 'table  : '.$tableName .'<br>';
            $productID=$_product->getId();
            //echo 'productID : '. $productID .'<br>' ;
            $query = 'SELECT applied_rule_ids FROM ' . $tableName . ' WHERE store_id = '
            .$storeId . ' AND ' . 'product_id = '. $productID . ' AND ' . 'applied_rule_ids is NOT NULL' ;
            $rows  = $readConnection->fetchAll($query); 
            $rules = Mage::getResourceModel('salesrule/rule_collection');
            foreach ($rows as $row) {
                foreach ($row as $singleRow) {
                    foreach ($rules as $rule) {
                        if ($rule->getIsActive()) {
                            if ($rule->getRuleId()==$singleRow) { 
                                $Tag = $rule->getDescription(); ?>
                                <div class="libelle-promo"><?php echo $Tag; ?></div>
                                <?php } 
                                else { ?>
                                <div class="libelle-promo" style="display: none;"></div>
                                <?php }
                            } 
                        }
                    } 
                }`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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