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.
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.
No comments:
Post a Comment