Here is the code snippet by which we can create Magento shopping cart price rule, i.e the rule which is applied on shopping cart while customer add his/her product to shopping cart.
shopping cart price rule can be divided in 3 parts
1. Information of the rule
2. Condition that will be applied on the rule.
3. Action that will be taken.
$name = "cutom programmatically created rule"; // name of 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 = 'RBT67'; // 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;
}