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.


<?php

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');

$shoppingCartPriceRule
        ->setName("25% off")
        ->setDescription('Get 25% Discount on Product which price is greater than or equal to 200')
        ->setIsActive(1)
        ->setWebsiteIds(array(1))
        ->setCustomerGroupIds(array(0,1,2,3,4))
        ->setFromDate('')
        ->setToDate('')
        ->setSortOrder('')
        ->setSimpleAction('by_percent')
        ->setDiscountAmount(25)
        ->setStopRulesProcessing(0);

$priceCondition = Mage::getModel('salesrule/rule_condition_product')
        ->setType('salesrule/rule_condition_product')
        ->setAttribute('price’)
        ->setOperator('>=')
        ->setValue(200);

try
{
        $shoppingCartPriceRule->getActions()->addCondition($skuCondition);
        $shoppingCartPriceRule->save();
        $rule_id = $shoppingCartPriceRule->getId();
        $shoppingCartPriceRule = Mage::getResourceModel('salesrule/rule');
        $shoppingCartPriceRule->saveStoreLabels($rule_id,$labels);
}
catch (Exception $e)
{
        Mage::getSingleton('core/session')->addError(Mage::helper('catalog')->__($e->getMessage()));
        return;
}

?>

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??????????????????


Speak Your Mind