Add a Grand Total Condition in Magento

When setting up a Shopping Cart Price Rule in Magento, you can choose to base the condition on which the rule with apply on the subtotal. There are occasions where merchants may want to base the conditions on the final price, or the price that includes the subtraction of any other discounts applied. The code below shows how to add a grand total condition in Magento, which in this case will be a combination of the subtotal plus any discount applied.

A merchant may wish to base rule from the subtotal with discount rather than the subtotal if they encounter a scenario like the following:

1. The merchant has an existing Shopping Cart Price Rule (Discount1), that provides a flat £10 discount off of the whole cart (no coupon code) if the customers’ subtotal is equal or greater than £100.

2. The merchant also has an another Shopping Cart Price Rule (Discount2), that provides another flat £10 discount off of the whole cart, but requires a coupon code. This code will be applied if the customers’ subtotal is is equal or greater than £100.

3. The customer adds a product that costs £100 and goes to the cart page.

4. Discount1 is applied automatically as the customer has met the criteria.

5. The customer correctly enters in the coupon code from Discount2, and they receive another £10 discount, despite the subtotal staying at £100. The merchant may want the coupon code to only apply to the grand total, which because of Discount1, has dropped to £90.

The cart attributes that populate the Conditions dropdown come from the Mage_SalesRule_Model_Rule_Condition_Address class, specifically the loadAttributeOptions() method.

public function loadAttributeOptions()
{
    $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'),
    );

    $this->setAttributeOption($attributes);

    return $this;
}

Extend this class by overriding this method to include the ‘Subtotal with Discount’ option.

Start with the configuration within your module’s config.xml file.

// app/code/local/[Vendor]/[Module]/etc/config.xml

<?xml version="1.0"?>
<config>
    <global>
        <models>
            <[vendor]_[module]>
                <class>[Vendor]_[Module]_Model</class>
            </[vendor]_[module]>
            <salesrule>
                <rewrite>
                    <rule_condition_address>[Vendor]_[Module]_Model_SalesRule_Rule_Condition_Address</rule_condition_address>
                </rewrite>
            </salesrule>
        </models>
    </global>
</config>

Now add the overriding class.

// app/code/local/[Vendor]/[Module]/Model/SalesRule/Rule/Condition/Address.php

<?php
class [Vendor]_[Module]_Model_SalesRule_Rule_Condition_Address extends
    Mage_SalesRule_Model_Rule_Condition_Address
{
    public function loadAttributeOptions()
    {
        parent::loadAttributeOptions();

        $attributes = $this->getAttributeOption();
        $attributes['base_subtotal_with_discount'] = Mage::helper('salesrule')->__('Subtotal With Discount');
        $this->setAttributeOption($attributes);

        return $this;
    }
}

There is another method that this class needs. getInputType() is used to return the type of input (numeric, select or string) of the attribute options.

public function getInputType()
{
    if ($this->getAttribute() == 'base_subtotal_with_discount') {
        return 'numeric';
    }

    return parent::getInputType();
}

Assuming you’ve set up your module correctly, you should notice that the Subtotal With Discount option has been successfully added to the Conditions dropdown, and will be available to use if you want to apply rules that include a combination of subtotal and discount amounts.

Add a Grand Total Condition in Magento

Note: This article is based on Magento Open Source version 1.9.