Add Grand total in the shopping cart rule condition

Feb 12th, 2019 3 min to read
Table of Contents

When you are setting up a Shopping Cart Price Rule in Magento, you can choose a base condition on which the rule will apply on the subtotal. There are instances when the 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 set up the rule for the subtotal with the discount instead of the subtotal in case if they encounter any of the  scenarios given below:

  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 has been 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.

#Magento 2

1. In your module, Ex:Companyname_Packagename, Create a di.xml file on the <root>/app/code/Companyname/Packagename/etc folder.

<type name="Magento\SalesRule\Model\Rule\Condition\Address">
        <plugin name="load-attribute-options" type="Companyname\Packagename\Plugin\Address" />

2. Now create a Address.php file at location <root>/app/code/Companyname/Packagename/Plugin folder.

namespace Companyname\Packagename\Plugin;

class Address
        public function afterLoadAttributeOptions(\Magento\SalesRule\Model\Rule\Condition\Address $subject)
        $attributes = $subject->getAttributeOption();
        $attributes['base_subtotal_with_discount'] = __('Subtotal With Discount');
        return $subject;

     public function afterGetInputType(\Magento\SalesRule\Model\Rule\Condition\Address $subject)
        switch ($subject->getAttribute()) {
            case 'base_subtotal':
            case 'base_subtotal_with_discount':
            case 'weight':
            case 'total_qty':
                return 'numeric';

            case 'shipping_method':
            case 'payment_method':
            case 'country_id':
            case 'region_id':
                return 'select';

        return 'string';

3. Now execute the command given below in order to see the effective changes:

php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy en_US -f
php bin/magento cache:clean
php bin/magento cache:flush

4. The result will be as follows:

#Magento 1

<p>1. In your module, Ex:<code>Companyname_Packagename</code>, Create a <code>config.xml</code> file on the <code>/app/code/Companyname/Packagename/etc</code> folder.</p>

2. Now create Address.php file at location <root>/app/code/Companyname/Packagename/Model/SalesRule/Rule/Condition folder.

3. 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 menu, and will be available for use if you wish to apply the rules that include a combination of subtotal along with the discounts.

4. That’s it, fellas! Here it goes:

we forecast

Post a Comment

We don’t write crap content which is uninteresting, found everywhere and does not make any sense. Following our DNA, we write and speak about next generation technology, platforms and possibilities of digital commerce.

* This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

we forecast

Comments (1)

  1. In Magento 2 compile not working