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="MagentoSalesRuleModelRuleConditionAddress">
        <plugin name="load-attribute-options" type="CompanynamePackagenamePluginAddress" />
</type>
  1. Now create a Address.php file at location <root>/app/code/Companyname/Packagename/Plugin folder.
<?php
namespace CompanynamePackagenamePlugin;

class Address {
        public function afterLoadAttributeOptions(MagentoSalesRuleModelRuleConditionAddress $subject) {
        $attributes = $subject->getAttributeOption();
        $attributes['base_subtotal_with_discount'] = __('Subtotal With Discount');
        $subject->setAttributeOption($attributes);
        return $subject;
    }

     public function afterGetInputType(MagentoSalesRuleModelRuleConditionAddress $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';
    }
}
  1. 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
  1. The result will be as follows:

#Magento 1

  1. In your module, Ex:Companyname_Packagename, Create a config.xml file on the /app/code/Companyname/Packagename/etc folder.
  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:


yanping
24 声望0 粉丝

引用和评论

0 条评论