Create Shopping Cart Price Rules Programmatically in Magento

We can create Shopping Cart Price Rules from the Admin panel of our store from Promotions->Shopping Cart Price Rules. Click on “Add New Rule” button and Fill all the information about your shopping cart price rule with Conditions and Actions which you want.

The Shopping Cart Price Rules are to be applied when customer reaches to Shopping Cart Page or adds any product into cart.

The shopping cart price rule has divided into mainly 3 tabs which are following.

1). Rule Information:-

Here, you can specify the details of the rule like, Rule Name, Description, Status, accessibility, coupon code etc.

2). Conditions: –

In a conditions tab you can put the condition for applying the price rule on cart when the conditions will true then shopping cart price rule will be applied to the cart.

3). Actions: –

In an actions tab you can update the price of particular product and whole cart as well with Fixed Amount Discount, Percent of Product Price Discount, etc. and you can also put conditions for shopping cart price rules in Actions tab as well.

4). Labels: –

Here, you can specify label of price rule which will display on cart page.

I have created the example code of shopping cart price rule with following information.

-> The Rule will only be applied when the “Price” is greater than or equal to (>=) to 200.

-> The Rule will be applied explicitly on specific product as a percentage discount by certain amount of discount (Here: BY 25 PERCENT) of Currency Amount.


ini_set('max_execution_time', 600);
ini_set('memory_limit', '1024M');

require 'app/Mage.php';
$app = Mage::app('');

$labels = array('0'=>'25% off','1'=>'25% off','2'=>'25% off','3'=>'25% off');
$shoppingCartPriceRule = Mage::getModel('salesrule/rule');

        ->setName("25% off")
        ->setDescription('Get 25% Discount on Product which price is greater than or equal to 200')

$priceCondition = Mage::getModel('salesrule/rule_condition_product')

        $rule_id = $shoppingCartPriceRule->getId();
        $shoppingCartPriceRule = Mage::getResourceModel('salesrule/rule');
catch (Exception $e)


3 Responses to “Create Shopping Cart Price Rules Programmatically in Magento”

  1. mh Says:

    I can’t get this to work. When running this error comes up:

    Fatal error: Call to a member function setRule() on a non-object in /app/code/core/Mage/Rule/Model/Condition/Combine.php on line 97

    Whats wrong?

  2. Karthi Says:

    where i need to write this code??????????????????

Leave a Reply to Karthi Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: