Home >How to Subtotal with Discount in Shopping Cart Price Rule in Magento 2

How to Subtotal with Discount in Shopping Cart Price Rule in Magento 2

In the previous part: ‘’How to schedule Magento Shopping Cart Price Rule’’, we had a condition called ‘’Subtotal’’. However, there are cases that customers want to run the rule based on Final Price, after subtracting all discounts. The default condition of Magento does not allow to set up a rule like this.

Therefore, to add one more extra condition, we need to extend class Mage_SalesRule_Model_Rule_Condition_Address and override all functions following as example code below:

<?php

class Yourcompany_YourmoduleName_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 after discount');

       $this->setAttributeOption($attributes);

    return $this;

}

/**

* (non-PHPdoc)

* @see Mage_SalesRule_Model_Rule_Condition_Address::getInputType()

*/

public function getInputType()

{

    if ($this->getAttribute() == 'base_subtotal_with_discount')

        return 'numeric';

    return parent::getInputType();

}

/**

* Add field "base_subtotal_with_discount" to address.

* It is need to validate the "base_subtotal_with_discount" attribute

* @see Mage_SalesRule_Model_Rule_Condition_Address::validate()

*/

public function validate(Varien_Object $address)

{

 

  $subAfterDiscount = $address->getBaseSubtotal() + $address->getDiscountAmount();   

       $address->setData('base_subtotal_with_discount', $subAfterDiscount);



    return parent::validate($address);

}

}

Result:

How to use Subtotal with Discount in Shopping Cart Price Rule-image1 How to use Subtotal with Discount in Shopping Cart Price Rule-image2

Magento will run through all the rules in order to validate which rule can be applied. Therefore, with the condition, we need to pay attention to the priority of each rule to make sure that the recent setup rule will run before or after. The higher priority number is, the lower level of priority is and vice versa. For instance, the priority of a rule is number 2 will run after the number 1 one.

How to use Subtotal with Discount in Shopping Cart Price Rule-image3

Notable case: if your website installs another module which allows getting discounts in the shopping cart (e.g: Rewards Point, Affiliate…), the function validate() needs to subtract all these discount values. Depending on the module having different discount calculations, we need to check code to get the most exact result.

If you get any trouble, you can contact us via our email: support@bsscommerce.com or Skype: support.bsscommerce

< Previous Post
Next Post >
+ posts