1

I'm trying to use this code I found here Get Shopping cart rule coupon code (with alittle modifications) to get shopping cart rules:

<?php
$current_sku = $_product->getSku();
$rules       = Mage::getResourceModel('salesrule/rule_collection')->load();
foreach ($rules as $rule) {
    if ($rule->getIsActive()) {
        $rule = Mage::getModel('salesrule/rule')->load($rule->getId()); 
        $conditions = $rule->getConditions()->asArray();
        foreach( $conditions['conditions'] as $_conditions ):
            foreach( $_conditions['conditions'] as $_condition ):
                $string = explode(',', $_condition['value']);
                for ($i=0; $i<count($string); $i++) {
                    $sku = trim($string[$i]); ?>
<?php if ($sku === $current_sku): ?>
<div style="float:right;height:0">
<img src="<?php echo $this->getSkinUrl('images/free-gift.png',array('_secure'=>true)) ?>" style="max-width:140px;width:100%;height:auto;max-height:140px;position:relative;bottom:60px;left:10px" />
</div>
<?php endif; ?>
  <?php } endforeach;
        endforeach;
    }
}
 ?>

I'm using it on the Product Details page. The code works but I'm getting the following error on line 83 i.e. $conditions = $rule->getConditions()->asArray();

Warning: Invalid argument supplied for foreach()

It seems after searching Google I need to somehow check if the Array() has a value but I'm not sure how. Or maybe it suppose to always have a value?

Any help would be great.

0

Try this code:

<?php
$rules       = Mage::getResourceModel('salesrule/rule_collection')->load(); 
    foreach ($rules as $rule) {
        if ($rule->getIsActive()) {
            $rule = Mage::getModel('salesrule/rule')->load($rule->getId()); 
            $conditions = $rule->getConditions()->asArray();
            $conditions = (isset($conditions['conditions']) && $conditions['conditions']) ? $conditions['conditions'] : array();
            if(!empty($conditions)) {
                foreach($conditions as $_conditions ):
                    $_conditions = (isset($_conditions['conditions']) && $_conditions['conditions']) ? $_conditions['conditions'] : array();
                    if(!empty($_conditions)) { 
                        foreach($_conditions as $_condition ):
                        $string = explode(',', $_condition['value']);
                            for ($i=0; $i<count($string); $i++) {
                                $sku = trim($string[$i]); ?>
                                <?php if ($sku === $current_sku): ?>
                                <div style="float:right;height:0">
                                <img src="<?php echo $this->getSkinUrl('images/free-gift.png',array('_secure'=>true)) ?>" style="max-width:140px;width:100%;height:auto;max-height:140px;position:relative;bottom:60px;left:10px" />
                                </div>
                                <?php endif; ?>
                                  <?php 
                              } 
                        endforeach; 
                    }
                endforeach;
            }
        }
    }
?>
  • No it didn't work. It stop the error but the image also stop showing. – Freejoy Dec 26 '17 at 5:37

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.