Last active
September 16, 2023 11:00
Magento - Create shopping cart price rule with a specific coupon code programmatically.
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 | |
/** | |
* Create Shopping Cart Sales Rule with Specific Coupon Code Programmatically | |
*/ | |
// All customer group ids | |
$customerGroupIds = Mage::getModel('customer/group')->getCollection()->getAllIds(); | |
// SalesRule Rule model | |
$rule = Mage::getModel('salesrule/rule'); | |
// Rule data | |
$rule->setName('Rule name') | |
->setDescription('Rule description') | |
->setFromDate('') | |
->setCouponType(Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC) | |
->setCouponCode('my-coupon-code') | |
->setUsesPerCustomer(1) | |
->setUsesPerCoupon(1) | |
->setCustomerGroupIds($customerGroupIds) | |
->setIsActive(1) | |
->setConditionsSerialized('') | |
->setActionsSerialized('') | |
->setStopRulesProcessing(0) | |
->setIsAdvanced(1) | |
->setProductIds('') | |
->setSortOrder(0) | |
->setSimpleAction(Mage_SalesRule_Model_Rule::BY_FIXED_ACTION) | |
->setDiscountAmount(10) | |
->setDiscountQty(1) | |
->setDiscountStep(0) | |
->setSimpleFreeShipping('0') | |
->setApplyToShipping('0') | |
->setIsRss(0) | |
->setWebsiteIds(array(1)) | |
->setStoreLabels(array('My Rule Frontend Label')); | |
// Product found condition type | |
$productFoundCondition = Mage::getModel('salesrule/rule_condition_product_found') | |
->setType('salesrule/rule_condition_product_found') | |
->setValue(1) // 0 == not found, 1 == found | |
->setAggregator('all'); // match all conditions | |
// 'Attribute set id 1' product condition | |
$attributeSetCondition = Mage::getModel('salesrule/rule_condition_product') | |
->setType('salesrule/rule_condition_product') | |
->setAttribute('attribute_set_id') | |
->setOperator('==') | |
->setValue(1); | |
// Bind attribute set condition to product found condition | |
$productFoundCondition->addCondition($attributeSetCondition); | |
// If a product with 'attribute set id 1' is found in the cart | |
$rule->getConditions()->addCondition($productFoundCondition); | |
// Only apply the rule discount to this specific product | |
$rule->getActions()->addCondition($attributeSetCondition); | |
// Here we go | |
$rule->save(); |
$coupon = Mage::getModel('salesrule/coupon');
$coupon->setId(null)
->setRuleId($ruleId)
->setCode($code)
->setUsageLimit(1)
//->setUsagePerCustomer
//->setTimesUsed
//->setExpirationDate
->setIsPrimary(1)
->setCreatedAt(time())
->setType(Mage_SalesRule_Helper_Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED)
->save();
Thanks! 👍
If I want to use it without any condition (just a coupon code for any cart), I just have to remove from line 38 to 58, right?
EDIT
Y, right ;)
What if i want to add customer's email id in condition like if this customer email matches only then apply rule. how can i achieve this
code nhu cc
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can I add "Coupon Code to" existing "Shopping Cart Price Rule" problematically?