Last active
October 12, 2015 21:39
-
-
Save xcommerce-gists/4090906 to your computer and use it in GitHub Desktop.
Magento coupon code generator sample configuration file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Coupon AutoGen REST API | |
* | |
* @category CouponDemo | |
* @package CouponDemo_AutoGen | |
* @author Chuck Hudson (used with permission). For more recipes, see Chuck's book http://shop.oreilly.com/product/0636920023968.do | |
*/ | |
class CouponDemo_AutoGen_Model_Api2_Coupon_Rest_Admin_V1 extends CouponDemo_AutoGen_Model_Api2_Coupon | |
{ | |
/** | |
* Generate one or more coupon codes using the Generate Coupons rule defined in Magento. | |
* Expected parameters are: | |
* { | |
* 'qty': int, - number of coupon codes to instruct Magento to generate | |
* 'length': int, - length of each generated coupon code | |
* 'format': string, - alphanum (for alphanumeric codes), alpha (for alphabetical codes), and num (for numeric codes) | |
* } | |
* | |
* @param array $couponData | |
* @return string|void | |
*/ | |
protected function _create($couponData) | |
{ | |
$ruleId = $this->getRequest()->getParam('rule_id'); | |
$couponData['rule_id'] = $ruleId; | |
$rule = $this->_loadSalesRule($ruleId); | |
// Reference the MassGenerator on this rule. | |
/** @var Mage_SalesRule_Model_Coupon_Massgenerator $generator */ | |
$generator = $rule->getCouponMassGenerator(); | |
// Validate the generator | |
if (!$generator->validateData($couponData)) { | |
$this->_critical(Mage::helper('salesrule')->__('Coupon AutoGen API: Invalid parameters passed in.'), | |
Mage_Api2_Model_Server::HTTP_BAD_REQUEST); | |
} else { | |
// Set the data for the generator | |
$generator->setData($couponData); | |
// Generate a pool of coupon codes for the Generate Coupons rule | |
$generator->generatePool(); | |
} | |
} | |
/** | |
* Retrieve list of coupon codes. | |
* | |
* @return array | |
*/ | |
protected function _retrieveCollection() | |
{ | |
$ruleId = $this->getRequest()->getParam('rule_id'); | |
$rule = $this->_loadSalesRule($ruleId); | |
/** @var Mage_SalesRule_Model_Resource_Coupon_Collection $collection */ | |
$collection = Mage::getResourceModel('salesrule/coupon_collection'); | |
$collection->addRuleToFilter($rule); | |
$this->_applyCollectionModifiers($collection); | |
$data = $collection->load()->toArray(); | |
return $data['items']; | |
} | |
/** | |
* Load sales rule by ID. | |
* | |
* @param int $ruleId | |
* @return Mage_SalesRule_Model_Rule | |
*/ | |
protected function _loadSalesRule($ruleId) | |
{ | |
if (!$ruleId) { | |
$this->_critical(Mage::helper('salesrule') | |
->__('Rule ID not specified.'), Mage_Api2_Model_Server::HTTP_BAD_REQUEST); | |
} | |
$rule = Mage::getModel('salesrule/rule')->load($ruleId); | |
if (!$rule->getId()) { | |
$this->_critical(Mage::helper('salesrule') | |
->__('Rule was not found.'), Mage_Api2_Model_Server::HTTP_NOT_FOUND); | |
} | |
return $rule; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment