3

I need to show all coupons codes in cms page. For this i have created a custom phtml in templete and in this template i have wrote the following code :

<?php
$rulesCollection = Mage::getModel('salesrule/rule')->getCollection();
foreach ($rulesCollection as $rule) {
    $coupon = $rule->getCoupons();
}

$rules = Mage::getResourceModel('salesrule/rule_collection')->load();
foreach ($rules as $rule) {
    $conditions = $rule->getConditionsSerialized();
    var_dump($conditions);
}
?>

and add a block in cms page :

{{block type="core/template" name="email_coupon" template="email/coupon.phtml"}}

But nothing is showing please help me .

  • what is the issue in code ,please explain – Amit Bera Apr 15 '15 at 13:02
3

instead of load whole collection i suggest you use sql query with help of my custom function

  public function getAllActiveCoupon(){

        $coupontable = 'salesrule_coupon';
        $today = Mage::getModel('core/date')->date('Y-m-d');
        $select = $this->_read->select()
                ->from('salesrule', array('name', 'description'))
                 ->joinLeft(
                        array('sc' => $coupontable),
                        "(sc.rule_id = salesrule.rule_id) AND (sc.expiration_date IS NULL OR sc.expiration_date >= '{$today}')",
                        array('code'))

                ->where('is_active = ?', 1)
                ->where('from_date IS NULL OR from_date <= ?', $today)
                ->where('to_date IS NULL OR to_date >= ?', $today)
                ->where('is_rss = ?', 1);

        $result = $this->_read->fetchAll($select); //fetch all active couopon
        return $result;       
    }

EDIT

in view file

$coupons = $this->getActiveCoupon();
 <?php
        if (!empty($coupons)) :
            foreach ($coupons as $coupon):
                ?>
                <li>
                    <h2><?php echo $coupon['name']; ?></h2>
                    <p><?php echo $coupon['description']; ?> </p>
                </li>
<?php endforeach; ?>    
<?php endif; ?>    

you can change condition depends on requirement

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.