Programmatically create shopping cart rule in magento

Below script will helpful to create a shopping cart rule in magento by programmatically.

$rule = Mage::getModel(‘salesrule/rule’);
$rule->setName(“RULE NAME”)
->setDescription(“RULE DESCRIPTION”)
->setFromDate(”)
->setCouponType(Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC)
->setCouponCode(“COUPONCODE”)
->setUsesPerCustomer(“HOW MANY NO.OF CUSTOMER CAN USE”)
->setUsesPerCoupon(“HOW MANY TIMES CUSTOMER CAN USE”)
->setCustomerGroupIds(“CUSTOMER GROUP”) //array(1,2,3,4)
->setFromDate(“FROM DATE”)
->setToDate(“TO DATE”)
->setIsActive(“ACTIVE OR DISABLE”)
->setConditionsSerialized(”)
->setActionsSerialized(”)
->setStopRulesProcessing(0)
->setIsAdvanced(1)
->setProductIds(”)
->setSortOrder(0)
->setSimpleAction(Mage_SalesRule_Model_Rule::CART_FIXED_ACTION)
->setDiscountAmount(“DISCOUNT AMOUNT”)
->setDiscountQty(0)
->setDiscountStep(0)
->setSimpleFreeShipping(‘0’)
->setApplyToShipping(‘0’)
->setIsRss(0)
->setWebsiteIds(array(1))
->setStoreLabels(array(‘THESE IS THE LABEL WILL WHICH IS DISPLAY FROM THE CART AFTER APPLY THE RULE BY CUSTOMER’));

$conditions = array();
$conditions[1] = array(‘type’ => ‘salesrule/rule_condition_combine’,’aggregator’ => ‘all’,’value’ => “1”,’new_child’ => ”);
$conditions[‘1–1’] = Array(‘type’ => ‘salesrule/rule_condition_address’,’attribute’ => ‘base_subtotal’,’operator’ => ‘>=’,’value’ => “CART MIN.AMOUNT ENTER HERE”);
$rule->setData(‘conditions’,$conditions);
$rule->loadPost($rule->getData());
$rule->setCouponType(2);
$rule->save();
SetSimpleAction : we can use any one from below condition
BUY_X_GET_Y_ACTION = ‘buy_x_get_y’
BY_FIXED_ACTION = ‘by_fixed’
BY_PERCENT_ACTION = ‘by_percent’
CART_FIXED_ACTION = ‘cart_fixed’
COUPON_TYPE_AUTO = 3
COUPON_TYPE_NO_COUPON = 1
COUPON_TYPE_SPECIFIC = 2
TO_FIXED_ACTION = ‘to_fixed’
TO_PERCENT_ACTION = ‘to_percent’

 

CORNER OF BLOG:

“Appear with fame, if you must appear; if not,
it is better not to appear than appear.

Leave a Reply