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.

<?php

// Let's include mage.php file
require_once 'app/Mage.php';
umask(0);

// Initialize the magento
Mage::app('default');

$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->setName($name);
	$ShoppingCartPriceRules->setDescription('Auto Generated');
	$ShoppingCartPriceRules->setFromDate(date('Y-m-d')); //Using the current date in the form of Y-m-d
	$ShoppingCartPriceRules->setCouponType(2);
	$ShoppingCartPriceRules->setCouponCode($uniqueId); //Generating uniqe and random coupon code
	$ShoppingCartPriceRules->setUsesPerCoupon(1);
	$ShoppingCartPriceRules->setUsesPerCustomer(1);
	$ShoppingCartPriceRules->setCustomerGroupIds(array(0,1,2,3)); //Creating coupon for all the group you can change as per your requirement.
	$ShoppingCartPriceRules->setIsActive(1);
	$ShoppingCartPriceRules->setStopRulesProcessing(0);
	$ShoppingCartPriceRules->setIsRss(0);
	$ShoppingCartPriceRules->setIsAdvanced(1);
	$ShoppingCartPriceRules->setProductIds('');
	$ShoppingCartPriceRules->setSortOrder(0);
	$ShoppingCartPriceRules->setSimpleAction('by_fixed'); // Coupon Code by fixed amount
	$ShoppingCartPriceRules->setDiscountAmount($discount_amount);
	$ShoppingCartPriceRules->setDiscountQty(0);
	$ShoppingCartPriceRules->setDiscountStep(0);
	$ShoppingCartPriceRules->setSimpleFreeShipping(0);
	$ShoppingCartPriceRules->setApplyToShipping(0);
	$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,
		);

		$ShoppingCartPriceRules->setData('conditions',$conditions);
		$ShoppingCartPriceRules->loadPost($ShoppingCartPriceRules->getData());
		$ShoppingCartPriceRules->save();
?>

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

Thanks