Home » Magento

Magento: Create Shopping Cart Price Rule Programmatically

20 June 2011 Share/Bookmark

This article will show you how to create shopping cart price rule in Magento through code.

Shopping Cart Price Rules are applied when the customer reaches the shopping cart.

To create a Shopping Cart Price Rule from Admin Panel, we go to Promotions -> Shopping Cart Price Rules and select Add New Rule.

Basically, there are three main parts for Shopping Cart Price Rule, i.e. Rule Information, Conditions, and Actions.

Here is the code to create Shopping Cart Price Rule. In this code example, I have created Shopping Cart Price Rule with the following information:-

- The rule is applied to particular product with the particular SKU (in our case: ‘chair’)
- The rule is applied as Percentage Discount By certain percent (in our case: by 10 percent) of currency amount

$name = "My Shopping Cart Price Rule"; // name of Shopping Cart Price Rule
$websiteId = 1; 
$customerGroupId = 2; 
$actionType = 'by_percent'; // discount by percentage 
(other options are: by_fixed, cart_fixed, buy_x_get_y)
$discount = 10; // percentage discount
$sku = 'chair'; // product sku

$shoppingCartPriceRule = Mage::getModel('salesrule/rule');
		
$shoppingCartPriceRule
	->setName($name)
	->setDescription('')
	->setIsActive(1)
	->setWebsiteIds(array($websiteId))
	->setCustomerGroupIds(array($customerGroupId))
	->setFromDate('')
	->setToDate('')
	->setSortOrder('')
	->setSimpleAction($actionType)
	->setDiscountAmount($discount)
	->setStopRulesProcessing(0);

$skuCondition = Mage::getModel('salesrule/rule_condition_product')
					->setType('salesrule/rule_condition_product')
					->setAttribute('sku')
					->setOperator('==')
					->setValue($sku);
					
try {	
	$shoppingCartPriceRule->getConditions()->addCondition($skuCondition);
	$shoppingCartPriceRule->save();				
	$shoppingCartPriceRule->applyAll();	
} catch (Exception $e) {
	Mage::getSingleton('core/session')->addError(Mage::helper('catalog')->__($e->getMessage()));
	return;
} 

A new Shopping Cart Price Rule with the name “My Shopping Cart Price Rule” has been created. You can view the rule from Promotions -> Shopping Cart Price Rules in admin.

Hope this helps. Thanks.

From Mukesh Chapagain's Blog, post Magento: Create Shopping Cart Price Rule Programmatically

email

php magento mukesh chapagain

Get New Post by Email
RSS Feed Subscribe RSS Feed
  • Luca

    How can i create a shopping cart rule programmaticaly with conditions on the cart subtotal ?

  • samir amrutya

    Hello Mukesh,

    I have done some customize code in adding items to shopping cart. I have some special requirement to add same products as new separate entry in cart.

    Now, I have one shopping cart rule for a specific product SKU. When I apply that coupon code to shopping cart it will apply for each separate items of same products.

    Instead of apply for each items of same products, I want to apply coupon code only one item. Other items of same products will not be affected by coupon code

    Is there any way to do like this, Please help me.
     

  • Rahil28288

    How can i add new cart attribute like “Grand total” to shopping cart price rules