Mage Tech Support

Custom Shopping Cart Price Rule Condition

Scenario : The platform offers Magento Shopping cart price rule, which will be helpful for the merchant’s various promotions. However, there are many difficult situations that merchants face to promote some unique offer with given conditions & attributes. Hence merchant requires having an additional condition for shopping cart price rule (Discount with or without coupon).

Detailed Description: Magento is one of the leading open-source E-Commerce platforms in PHP. Being fully functional, user-friendly, customizable, secure, robust and feature-rich, Magento has strong evidence of success in the E-Commerce market.
Successful companies know that sales promotions are among the most effective methods of increasing sales and building customer satisfaction. A survey found that up to 50% of consumers make a purchase only with a promotion.

Magento offers standard price rules for Catalog and Shopping cart. Many a time, the standard rules are not sufficient to create new promotional rules for a merchant offer. Here we explain the method to add a custom condition (discount applicable only for orders from mobile app) in Shopping cart price rules.

Solution:

Step -1: Override the class Mage_SalesRule_Model_Rule_Condition_Address. (Copy the file Address.php from app/code/core/Mage/SalesRule/Model/Rule/Condition/ to app/code/local/Mage/SalesRule/Model/Rule/Condition/)

Step -2: Add the new condition (highlighted) in the $attributes array as given below

$attributes = array(
             'base_subtotal' => Mage::helper('salesrule')->__('Subtotal'),
             'total_qty' => Mage::helper('salesrule')->__('Total Items Quantity'),
             'weight' => Mage::helper('salesrule')->__('Total Weight'),
             'payment_method' => Mage::helper('salesrule')->__('Payment Method'),
             'shipping_method' => Mage::helper('salesrule')->__('Shipping Method'),
             'postcode' => Mage::helper('salesrule')->__('Shipping Postcode'),
             'region' => Mage::helper('salesrule')->__('Shipping Region'),
             'region_id' => Mage::helper('salesrule')->__('Shipping State/Province'),
             'country_id' => Mage::helper('salesrule')->__('Shipping Country'),
             'only_for_app' => Mage::helper('salesrule')->__('Only for Mobile App'),  /* Newly Added Condition */
            );

Step -3: Now the condition will display in the shopping cart price rule, Admin -> Promotions -> Shopping Cart Price Rules -> Add New Rule -> Conditions -> Add New.

Step -4: To validate this new condition while applying shopping cart price rules, add the below condition (you shall change condition according to your requirement) into validate function

public function validate(Varien_Object $object)
{
    :
    :
    if ($this->getAttribute() == 'only_for_app')  /* checking new condition */
    {
       /* Your custom logic to validate the condition */
       if(Mage::getSingleton('core/session')->getMobile()) 
       {
           return true; /* If validation success apply rule */
       }
    }
    :
    :
    return parent::validate($object);
}

Step -5: After adding your logic in validate function, create the shopping cart price rule with your own new condition and check it out from the front-end.