Create Shopping Cart Price Rule Programatically in Magento

Magento provides 2 types of the product Promotions one is Catalog Price Rules and another one is Shopping Cart Price Rules, Catalog Price Rules applied on the Whole Catalog (Products) of the Magento where as Shopping Cart Price Rules are applied when the customer reaches on the shopping cart page of the Magento.

You can create the Shopping Cart Price Rules and Catalog Price Rules from the Magento admin just by navigating to the Promotions > Shopping Cart Price Rules and Click on the Add New Rule Button on the Top Right, Basically there is 3 parts of the Shopping Cart Price Rules, Rule Information, Condition and Actions.

Here is the code below to create the Shopping Cart Price Rules,

Here I am using the sku of the product ‘ewaprodnew’ and the amount is 10.

Just create a php file in the Mageto root and run this website by using the browser or you can run it from the SSH as well.


// Let's include mage.php file
require_once 'app/Mage.php';

// Initialize the magento

$name = "Customer Discount"; // Shopping Cart Price Rule Name
$discount_amount = 10; // Fixed amount
$sku = 'ewaprodnew'; // product sku

	$uniqueId = Mage::helper('core')->getRandomString(16); 
	$ShoppingCartPriceRules = Mage::getModel('salesrule/rule'); 
	$ShoppingCartPriceRules->setDescription('Auto Generated');
	$ShoppingCartPriceRules->setFromDate(date('Y-m-d')); //Using the current date in the form of Y-m-d
	$ShoppingCartPriceRules->setCouponCode($uniqueId); //Generating uniqe and random coupon code
	$ShoppingCartPriceRules->setCustomerGroupIds(array(0,1,2,3)); //Creating coupon for all the group you can change as per your requirement.
	$ShoppingCartPriceRules->setSimpleAction('by_fixed'); // Coupon Code by fixed amount
	$ShoppingCartPriceRules->setWebsiteIds('1'); //Website Id I am using 1 you can change as per your requirement

		$conditions[1] = array(
		'type' => 'salesrule/rule_condition_combine',
		'aggregator' => 'all',
		'value' => "1", 
		'new_child' => ''

		$conditions['1--1'] = array
		'type' => 'salesrule/rule_condition_product_found',
		'value' => 1,
		'aggregator' => 'all',
		'new_child' => '', 
		$conditions['1--1--1'] = array
		'type' => 'salesrule/rule_condition_product',
		'attribute' => 'sku',
		'operator' => '==',
		'value' => $sku,


I hope it helps, Thanks and enjoy the Magento Coding.