How to create custom cart rule in magento 2

As you know magento provides us with four types of cart rules.

But sometimes we need or need to enhance the functionality of the magento cart rule according to the requirement you can have two options either you override the core magento cart rule or create a new cart rule.

Here we create a new custom cart rule

for creating a custom cart rule you need to create the below files

Below the file create just add the option in the dropdown in the rule
WebkulCartRuleetcadminhtmldi.xml

Searching for an experienced
Magento 2 Company ?
Read More


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="MagentoSalesRuleModelRuleMetadataValueProvider">
        <plugin name="salesrule-plugin" type="WebkulCartRulePluginRuleMetadataValueProvider" sortOrder="1" />
    </type>
</config>
<?php

namespace WebkulCartRulePluginRuleMetadata;

class ValueProvider {
    public function afterGetMetadataValues(
        MagentoSalesRuleModelRuleMetadataValueProvider $subject,
        $result
    ) {


        $applyOptions = [
            'label' => __('Custom'),
            'value' => [
                [
                    'label' => 'Buy X Get next Y with M% discount',
                    'value' => 'buy_x_get_next_y_with_percent',
                ]
            ],
        ];
        array_push($result['actions']['children']['simple_action']['arguments']['data']['config']['options'], $applyOptions);
        return $result;
    }
}

You will see the cart rule in the dropdown

Now pass the class as an argument in class MagentoSalesRuleModelRuleActionDiscountCalculator
in below location
WebkulCartRuleetcdi.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="MagentoSalesRuleModelRuleActionDiscountCalculatorFactory">
        <arguments>
            <argument name="discountRules" xsi:type="array">
                <item name="buy_x_get_next_y_with_percent" xsi:type="string">WebkulCartRuleModelRuleActionDiscountBuyXGetNextYWithPercent</item>
            </argument>
        </arguments>
     </type>
</config>
<?php
namespace WebkulMagentoModelRuleActionDiscount;

use MagentoSalesRuleModelRuleActionDiscountAbstractDiscount;

class BuyXGetNextYWithPercent extends AbstractDiscount
{
    /**
     * Calculate Buy X Get next Y with M% discount amount
     *
     * @param MagentoSalesRuleModelRule $rule
     * @param MagentoQuoteModelQuoteItemAbstractItem $item
     * @param float $qty
     * @return MagentoSalesRuleModelRuleActionDiscountData
     */
    public function calculate($rule, $item, $qty)
    {
        $rulePercent = min(100, $rule->getDiscountAmount());
        $discountData = $this->_calculate($rule, $item, $qty, $rulePercent);
        
        return $discountData;
    }

    /**
     * @param MagentoSalesRuleModelRule $rule
     * @param MagentoQuoteModelQuoteItemAbstractItem $item
     * @param float $qty
     * @param float $rulePercent
     * @return Data
     */
    protected function _calculate($rule, $item, $qty, $rulePercent)
    {
        /** @var MagentoSalesRuleModelRuleActionDiscountData $discountData */
        $discountData = $this->discountFactory->create();

        /**
         * Calculate your logic here
         */
         
        /** Set the discount price in Price **/
        
        $itemPrice = $this->validator->getItemPrice($item);
        $baseItemPrice = $this->validator->getItemBasePrice($item);
        $itemOriginalPrice = $this->validator->getItemOriginalPrice($item);
        $baseItemOriginalPrice = $this->validator->getItemBaseOriginalPrice($item);

        $discountData->setAmount($itemPrice - $discountAmount);
        $discountData->setBaseAmount($baseItemPrice - $discountAmount);
        $discountData->setOriginalAmount($itemOriginalPrice - $discountAmount);
                $discountData->setBaseOriginalAmount($baseItemOriginalPrice - $discountAmount);


        return $discountData;
    }

}

Now Create the rule

cartRule2

Happy Coding 🙂
Thanks!!


Source link