Magento API Get All Promotions

Hello,

Recently in one of my project we were building mobile app where we have backend as Magento. In this app we have to show all the promotions added in Magento as shopping cart rules. Also there was a requirement to get all the coupons. There is no API for that in default Magento API so I decided to write an API. Here is how to do this.

Mage::app()->setCurrentStore($_storeId);
   
    $rules = Mage::getResourceModel(‘salesrule/rule_collection’)->load();

$comboOffers = array();
$coupons = array();
foreach ($rules as $rule) {
    if ($rule->getIsActive() && $rule->coupon_type == 1) {//No coupons only offers.
        $rule = Mage::getModel(‘salesrule/rule’)->load($rule->getId());
        $comboOffers[] = array(
        “rule_id” => $rule->getId(),
        “name” => $rule->getName(),
        “description” => $rule->getDescription()
        );
        }else{
        $rule = Mage::getModel(‘salesrule/rule’)->load($rule->getId());
        $coupons[] = array(
        “rule_id” => $rule->getId(),
        “name” => $rule->getName(),
        “description” => $rule->getDescription(),
        “coupon_code”=> $rule->getCouponCode()
        );
        }
    }

As you can see in above code we are first getting collection of all the sales rule and then loop trough it and separate offers and coupon. In case of offers like buy 1 get 1 free and percent discount there is no coupon code so coupon_type is set to 1 which means “No Coupon”.  While for others there are coupons. So just checking by that we populated two different arrays and return it in form of JSON.

$MainArray[‘offers’] = $comboOffers;
$MainArray[‘coupons’] = $coupons;
$MainArray[‘success’] = true;
echo json_encode($MainArray);

That’s it and you will have all the coupons and array of offers available in response. You can use this JSON data to display on screen.

This entry was posted in Magento, Magento Mobile, Magento Mobile Site, Magento Mobile Store, Magento Mobile Theme on by .

About Hiren Dave

Hiren Dave is a software engineer, blogger and consultant with over 9 years of professional experience. He writes code in number of languages. Hiren has a passion for developing web apps, mobile apps and writing books and blog articles about programming. He has a solid experience of startups as he manages products for few startups. He is also passionate about researching and learning new technologies and trends. He has also authored a book Instant Sencha Touch, published by Packt and creator of Video Course "Beginning Laravel" for Packt

Leave a Reply

Your email address will not be published. Required fields are marked *