{"id":420958,"date":"2024-02-07T15:25:58","date_gmt":"2024-02-07T15:25:58","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=420958"},"modified":"2024-03-21T11:40:38","modified_gmt":"2024-03-21T11:40:38","slug":"how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/","title":{"rendered":"How to create custom cart rule for Buy X Get next Y in Magento 2"},"content":{"rendered":"\n<p>For an e-commerce business to flourish, it is quite necessary to inculcate effective marketing strategies that lay a strong influence on the customers. <\/p>\n\n\n\n<p>Having said this, <a href=\"https:\/\/store.webkul.com\/magento2-special-promotions.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 Special Promotions<\/a> and discounts are one of the most renowned marketing strategies.<\/p>\n\n\n\n<p>As you know Magento 2 provides us with four types of cart rules.<\/p>\n\n\n\n<p>In this blog,\u00a0We will learn how can we add a custom cart rule (Buy X Get next Y with percent discount)<\/p>\n\n\n\n<p>Let\u2019s start step by step.<br><strong>Note:-<\/strong> You must have installed your custom Module on which you are going to implement this.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Create file <strong>Webkul\\CartRule\\etc\\adminhtml\\di.xml<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;!-- \n\/**\n * Webkul Software.\n *\n * @category   Webkul\n * @package    Webkul_CartRule\n * @author     Webkul\n * @copyright  Copyright (c) Webkul Software Private Limited (https:\/\/webkul.com)\n * @license    https:\/\/store.webkul.com\/license.html\n *\/\n --&gt;\n&lt;config xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:ObjectManager\/etc\/config.xsd&quot;&gt;\n    &lt;type name=&quot;Magento\\SalesRule\\Model\\Rule\\Metadata\\ValueProvider&quot;&gt;\n        &lt;plugin name=&quot;salesrule-plugin&quot; type=&quot;Webkul\\CartRule\\Plugin\\Rule\\Metadata\\ValueProvider&quot; sortOrder=&quot;1&quot; \/&gt;\n    &lt;\/type&gt;\n&lt;\/config&gt;<\/pre>\n\n\n\n<p><strong>Step 2:&nbsp;<\/strong>We need to create the ValueProvider plugin file which add a custom cart rule option in the cart rules dropdown. <strong>Webkul\\CartRule\\Plugin\\Rule\\Metadata\\ValueProvider.php<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\n\/**\n * Webkul Software.\n *\n * @category   Webkul\n * @package    Webkul_CartRule\n * @author     Webkul\n * @copyright  Copyright (c) Webkul Software Private Limited (https:\/\/webkul.com)\n * @license    https:\/\/store.webkul.com\/license.html\n *\/\n\nnamespace Webkul\\CartRule\\Plugin\\Rule\\Metadata;\n\nclass ValueProvider\n{\n    public function afterGetMetadataValues(\n        \\Magento\\SalesRule\\Model\\Rule\\Metadata\\ValueProvider $subject,\n        $result\n    ) {\n        $applyOptions = &#091;\n            &#039;label&#039; =&gt; __(&#039;Custom&#039;),\n            &#039;value&#039; =&gt; &#091;\n                &#091;\n                    &#039;label&#039; =&gt; &#039;Buy X Get next Y with M% discount&#039;,\n                    &#039;value&#039; =&gt; &#039;buy_x_get_next_y_with_percent&#039;,\n                ]\n            ],\n        ];\n        array_push($result&#091;&#039;actions&#039;]&#091;&#039;children&#039;]&#091;&#039;simple_action&#039;]&#091;&#039;arguments&#039;]&#091;&#039;data&#039;]&#091;&#039;config&#039;]&#091;&#039;options&#039;], $applyOptions);\n        return $result;\n    }\n}<\/pre>\n\n\n\n<p>After adding the above code to your custom module. Deploy the code and&nbsp;<strong>see the result<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"610\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15-1200x610.png\" alt=\"Screenshot-from-2024-02-07-19-14-15\" class=\"wp-image-420974\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15-1200x610.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15-300x152.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15-250x127.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15-768x390.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15.png 1507w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><\/figure>\n\n\n\n<p><strong>Step 3:<\/strong> Create <strong>Webkul\/CartRule\/view\/adminhtml\/ui_component\/sales_rule_form.xml<\/strong> file to add custom fields in the Cart price rule form.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;!--\n\/**\n * Webkul Software.\n *\n * @category   Webkul\n * @package    Webkul_CartRule\n * @author     Webkul\n * @copyright  Copyright (c) Webkul Software Private Limited (https:\/\/webkul.com)\n * @license    https:\/\/store.webkul.com\/license.html\n *\/\n--&gt;\n&lt;form xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:module:Magento_Ui:etc\/ui_configuration.xsd&quot;&gt;\n    &lt;fieldset name=&quot;actions&quot; sortOrder=&quot;30&quot;&gt;\n    &lt;field name=&quot;custom_step_nqty&quot; formElement=&quot;input&quot;&gt;\n            &lt;argument name=&quot;data&quot; xsi:type=&quot;array&quot;&gt;\n                &lt;item name=&quot;config&quot; xsi:type=&quot;array&quot;&gt;\n                    &lt;item name=&quot;source&quot; xsi:type=&quot;string&quot;&gt;sales_rule&lt;\/item&gt;\n                    &lt;item name=&quot;sortOrder&quot; xsi:type=&quot;number&quot;&gt;4&lt;\/item&gt;\n                &lt;\/item&gt;\n            &lt;\/argument&gt;\n            &lt;settings&gt;\n                &lt;validation&gt;\n                    &lt;rule name=&quot;required-entry&quot; xsi:type=&quot;boolean&quot;&gt;true&lt;\/rule&gt;\n                    &lt;rule name=&quot;validate-number&quot; xsi:type=&quot;boolean&quot;&gt;true&lt;\/rule&gt;\n                    &lt;rule name=&quot;validate-zero-or-greater&quot; xsi:type=&quot;boolean&quot;&gt;true&lt;\/rule&gt;\n                &lt;\/validation&gt;\n                &lt;dataType&gt;text&lt;\/dataType&gt;\n                &lt;label translate=&quot;true&quot;&gt;Discount Qty (Buy Y)&lt;\/label&gt;\n                &lt;dataScope&gt;custom_step_nqty&lt;\/dataScope&gt;\n            &lt;\/settings&gt;\n        &lt;\/field&gt;\n        &lt;field name=&quot;promo_skus&quot; formElement=&quot;textarea&quot;&gt;\n            &lt;argument name=&quot;data&quot; xsi:type=&quot;array&quot;&gt;\n                &lt;item name=&quot;config&quot; xsi:type=&quot;array&quot;&gt;\n                    &lt;item name=&quot;source&quot; xsi:type=&quot;string&quot;&gt;sales_rule&lt;\/item&gt;\n                    &lt;item name=&quot;notice&quot; xsi:type=&quot;string&quot; translate=&quot;true&quot;&gt;Enter Y products separated by comma. Enter All SKU(s) if product is Customisable.&lt;\/item&gt;\n                &lt;\/item&gt;\n            &lt;\/argument&gt;\n            &lt;settings&gt;\n                &lt;dataType&gt;number&lt;\/dataType&gt;\n                &lt;label translate=&quot;true&quot;&gt;Promo SKU&lt;\/label&gt;\n                &lt;dataScope&gt;promo_skus&lt;\/dataScope&gt;\n            &lt;\/settings&gt;\n        &lt;\/field&gt;\n    &lt;\/fieldset&gt;\n&lt;\/form&gt;<\/pre>\n\n\n\n<p>You will see the result in the cart price rule&nbsp;:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"619\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T192850.744-1200x619.png\" alt=\"Screenshot-2024-02-07T192850.744\" class=\"wp-image-420980\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T192850.744-1200x619.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T192850.744-300x155.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T192850.744-250x129.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T192850.744-768x396.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T192850.744.png 1509w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><\/figure>\n\n\n\n<p><strong>Discount Qty (Buy Y)<\/strong>: Set quantity for <strong>Y<\/strong> products eligible for discount.<\/p>\n\n\n\n<p><strong>Promo SKU:<\/strong> This field defines the <strong>Y products<\/strong>, Enter Y products SKU separated by comma. Enter All SKU(s) if the product is Customisable.<\/p>\n\n\n\n<p><strong>Note:-<\/strong> Non-Promo products SKU are defined<strong> as X products<\/strong>.<\/p>\n\n\n\n<p><strong>Step 4:<\/strong> Create a layout file <strong>Webkul\/CartRule\/view\/adminhtml\/layout\/sales_rule_promo_quote_edit.xml<\/strong> to add a template file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;!--\n\/**\n * Webkul Software.\n *\n * @category   Webkul\n * @package    Webkul_CartRule\n * @author     Webkul\n * @copyright  Copyright (c) Webkul Software Private Limited (https:\/\/webkul.com)\n * @license    https:\/\/store.webkul.com\/license.html\n *\/\n--&gt;\n&lt;page xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd&quot;&gt;\n    &lt;body&gt;\n        &lt;referenceBlock name=&quot;head.components&quot;&gt;\n            &lt;block class=&quot;Magento\\Framework\\View\\Element\\Js\\Components&quot; name=&quot;sales_custom_rule_form_page_head_components&quot; template=&quot;Webkul_CartRule::promo\/customrulejs.phtml&quot;\/&gt;\n        &lt;\/referenceBlock&gt;\n    &lt;\/body&gt;\n&lt;\/page&gt;<\/pre>\n\n\n\n<p><strong>Step 5:<\/strong> Create a template <strong>Webkul\/view\/adminhtml\/templates\/promo\/customrulejs.phtml<\/strong> file for managing the custom field&#8217;s appearance.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\n\/**\n * Webkul Software.\n *\n * @category   Webkul\n * @package    Webkul_CartRule\n * @author     Webkul\n * @copyright  Copyright (c) Webkul Software Private Limited (https:\/\/webkul.com)\n * @license    https:\/\/store.webkul.com\/license.html\n *\/\n\n?&gt;\n&lt;script&gt;\n    require(&#091;\n        &#039;jquery&#039;,\n        &quot;uiRegistry&quot;\n    ], function($, registry) {\n\n        $(document).ready(function() {\n            $(&#039;body&#039;).on(&#039;click&#039;, &quot;.fieldset-wrapper-title&quot;, function() {\n                $(&#039;body select&#091;name=simple_action]&#039;).trigger(&#039;change&#039;);\n            });\n            $(&#039;body&#039;).on(&#039;change&#039;, &#039;select&#091;name=simple_action]&#039;, function() {\n                $(&#039;body input&#091;name=discount_amount]&#039;).parent().parent()\n                    .find(&#039;label span&#039;).text(&quot;&lt;?= \/* @noEscape *\/ __(&#039;Discount Amount&#039;); ?&gt;&quot;);\n                if ($(this).val() == &#039;buy_x_get_next_y_with_percent&#039;) {\n                    let discountAmount = Math.min(100, $(&#039;body input&#091;name=discount_amount]&#039;).val());\n                    discountAmount = discountAmount ? discountAmount : 1;\n                    let customRuleElementNode = $(&#039;body select&#091;name=simple_action] option&#091;value=&quot;buy_x_get_next_y_with_percent&quot;]&#039;);\n                    let customRuleLabel = customRuleElementNode.text().replace(&#039;M%&#039;, `${discountAmount}%`);\n                    customRuleElementNode.text(customRuleLabel);\n                    $(&#039;body textarea&#091;name=promo_skus]&#039;).parent().parent().show();\n                    $(&#039;body input&#091;name=discount_amount]&#039;).parent().parent().find(&#039;label span&#039;)\n                        .text(&quot;&lt;?= \/* @noEscape *\/ __(&#039;Discount Amount (in %)&#039;); ?&gt;&quot;);\n                    $(&#039;body input&#091;name=custom_step_nqty]&#039;).parent().parent().show();\n                } else {\n                    $(&#039;body input&#091;name=custom_step_nqty]&#039;).parent().parent().hide();\n                    $(&#039;body textarea&#091;name=promo_skus]&#039;).parent().parent().hide();\n                }\n            });\n        });\n    });\n&lt;\/script&gt;<\/pre>\n\n\n\n<p><strong>Step 6:<\/strong>  Configure declarative schema <strong>Webkul\/CartRule\/etc\/db_schema.xml<\/strong> file to save custom field data of the Cart price rule form.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;!--\n\/**\n * Webkul Software.\n *\n * @category   Webkul\n * @package    Webkul_CartRule\n * @author     Webkul\n * @copyright  Copyright (c) Webkul Software Private Limited (https:\/\/webkul.com)\n * @license    https:\/\/store.webkul.com\/license.html\n *\/\n--&gt;\n&lt;schema xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:Setup\/Declaration\/Schema\/etc\/schema.xsd&quot;&gt;\n  &lt;table name=&quot;salesrule&quot; resource=&quot;default&quot;&gt;\n    &lt;column xsi:type=&quot;text&quot; name=&quot;custom_step_nqty&quot; nullable=&quot;true&quot; comment=&quot;N Qty&quot;\/&gt;\n    &lt;column xsi:type=&quot;text&quot; name=&quot;promo_skus&quot; nullable=&quot;true&quot; comment=&quot;Promotion SKUs&quot;\/&gt;\n  &lt;\/table&gt;\n&lt;\/schema&gt;<\/pre>\n\n\n\n<p><strong>Step 7:<\/strong> <span style=\", serif;font-size: 21px\">For Calculation of the custom cart rule, create <span style=\", serif;font-size: 21px\"><strong>Webkul\\CartRule\\etc\\di.xml<\/strong><\/span><\/span>&nbsp;file<span style=\", serif;font-size: 21px\">&nbsp;and pass the class as an argument in class <strong>Magento\\SalesRule\\Model\\Rule\\Action\\Discount\\Calculator<\/strong><\/span><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;!-- \n\/**\n * Webkul Software.\n *\n * @category   Webkul\n * @package    Webkul_CartRule\n * @author     Webkul\n * @copyright  Copyright (c) Webkul Software Private Limited (https:\/\/webkul.com)\n * @license    https:\/\/store.webkul.com\/license.html\n *\/\n --&gt;\n&lt;config xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:ObjectManager\/etc\/config.xsd&quot;&gt;\n    &lt;type name=&quot;Magento\\SalesRule\\Model\\Rule\\Action\\Discount\\CalculatorFactory&quot;&gt;\n        &lt;arguments&gt;\n            &lt;argument name=&quot;discountRules&quot; xsi:type=&quot;array&quot;&gt;\n                &lt;item name=&quot;buy_x_get_next_y_with_percent&quot; xsi:type=&quot;string&quot;&gt;\\Webkul\\CartRule\\Model\\Rule\\Action\\Discount\\BuyXGetNextYWithPercent&lt;\/item&gt;\n            &lt;\/argument&gt;\n        &lt;\/arguments&gt;\n     &lt;\/type&gt;\n&lt;\/config&gt;<\/pre>\n\n\n\n<p><strong>Step 8:<\/strong> Create class  <strong>Webkul\\CartRule\\Model\\Rule\\Action\\Discount\\BuyXGetNextYWithPercent.php<\/strong> file which is passed as an argument in the above file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\n\/**\n * Webkul Software.\n *\n * @category   Webkul\n * @package    Webkul_CartRule\n * @author     Webkul\n * @copyright  Copyright (c) Webkul Software Private Limited (https:\/\/webkul.com)\n * @license    https:\/\/store.webkul.com\/license.html\n *\/\n\nnamespace Webkul\\CartRule\\Model\\Rule\\Action\\Discount;\n\nuse Magento\\SalesRule\\Model\\Rule\\Action\\Discount\\AbstractDiscount;\n\nclass BuyXGetNextYWithPercent extends AbstractDiscount\n{\n    \/**\n     * Calculate Buy X Get next Y with M% discount amount\n     *\n     * @param \\Magento\\SalesRule\\Model\\Rule $rule\n     * @param \\Magento\\Quote\\Model\\Quote\\Item\\AbstractItem $item\n     * @param float $qty\n     * @return \\Magento\\SalesRule\\Model\\Rule\\Action\\Discount\\Data\n     *\/\n    public function calculate($rule, $item, $qty)\n    {\n        $rulePercent = min(100, $rule-&gt;getDiscountAmount());\n        $discountData = $this-&gt;_calculate($rule, $item, $qty, $rulePercent);\n\n        return $discountData;\n    }\n\n    \/**\n     * @param \\Magento\\SalesRule\\Model\\Rule $rule\n     * @param \\Magento\\Quote\\Model\\Quote\\Item\\AbstractItem $item\n     * @param float $qty\n     * @param float $rulePercent\n     * @return Data\n     *\/\n    protected function _calculate($rule, $item, $qty, $rulePercent)\n    {\n        \/** @var \\Magento\\SalesRule\\Model\\Rule\\Action\\Discount\\Data $discountData *\/\n        $discountData = $this-&gt;discountFactory-&gt;create();\n\n        \/**\n         * Calculation logic here\n         *\/\n        $discountStep = $rule-&gt;getDiscountStep();\n        $maxDiscountQty = $rule-&gt;getDiscountQty();\n        $currentDiscountAmount = $rulePercent;\n        $qty = $item-&gt;getQty();\n        $cartSKUs = &#091;];\n        $promoSkus = $rule-&gt;getpromoSkus();\n        $totalQuantity = $item-&gt;getQuote()-&gt;getItemsQty();\n        $yProducts = $rule-&gt;getData(&#039;custom_step_nqty&#039;);\n        $productPriceSKUWise = &#091;];\n        $productQty = &#091;];\n        foreach ($item-&gt;getQuote()-&gt;getAllVisibleItems() as $currentItem) {\n\n            if (!$this-&gt;validator-&gt;canApplyRules($currentItem)) {\n                continue;\n            }\n            $currentItemSku = $currentItem-&gt;getSku();\n            if ($currentItem-&gt;getProductType() == &#039;bundle&#039;) {\n                $currentItemSku = $this-&gt;getBundleProductSku($currentItem-&gt;getId(), $currentItemSku, $item);\n            }\n            $productQty&#091;$currentItemSku] = $currentItem-&gt;getQty();\n            $cartSKUs&#091;] = $currentItemSku;\n            $productPriceSKUWise&#091;$currentItemSku]&#091;&#039;price&#039;] = $this-&gt;validator-&gt;getItemPrice($currentItem);\n            $productPriceSKUWise&#091;$currentItemSku]&#091;&#039;base_price&#039;] = $this-&gt;validator-&gt;getItemBasePrice($currentItem);\n        }\n\n        $discountAmount = $this-&gt;getAmountSkuWise(\n            $currentDiscountAmount,\n            $qty,\n            $maxDiscountQty,\n            $discountStep,\n            $totalQuantity,\n            $yProducts,\n            $productQty,\n            $item,\n            $cartSKUs,\n            $promoSkus,\n            $productPriceSKUWise\n        );\n\n        \/** Set the discount price in Price **\/\n        $discountData-&gt;setAmount($discountAmount&#091;&#039;discount&#039;]);\n        $discountData-&gt;setBaseAmount($discountAmount&#091;&#039;base_discount&#039;]);\n        $discountData-&gt;setOriginalAmount($discountAmount&#091;&#039;discount&#039;]);\n        $discountData-&gt;setBaseOriginalAmount($discountAmount&#091;&#039;base_discount&#039;]);\n\n        return $discountData;\n    }\n\n    \/**\n     * Bundle product SKu\n     *\n     * @param int $itemId\n     * @param string $currentItemSku\n     * @param \\Magento\\Quote\\Model\\Quote\\Item $item\n     * @return string\n     *\/\n    public function getBundleProductSku($itemId, $currentItemSku, $item)\n    {\n        foreach ($item-&gt;getQuote()-&gt;getAllItems() as $currentItem) {\n            if ($currentItem-&gt;getParentItemId() == $itemId) {\n                $currentItemSku = str_replace(&quot;-&quot; . $currentItem-&gt;getSku(), &quot;&quot;, $currentItemSku);\n            }\n        }\n        return $currentItemSku;\n    }\n\n    \/**\n     * Get discount amount as per promotional SKU\n     *\n     * @param integer $currentDiscountAmount\n     * @param integer $qty\n     * @param integer $maxDiscountQty\n     * @param integer $discountStep\n     * @param integer $totalQuantity\n     * @param int $yProducts\n     * @param array $productQty\n     * @param \\Magento\\Quote\\Model\\Quote\\Item $item\n     * @param array $cartSKUs\n     * @param string $promoSkus\n     * @param array $productPriceSKUWise\n     * @return array\n     *\/\n    private function getAmountSkuWise(\n        $currentDiscountAmount,\n        $qty,\n        $maxDiscountQty,\n        $discountStep,\n        $totalQuantity,\n        $yProducts,\n        $productQty,\n        $item,\n        $cartSKUs,\n        $promoSkus,\n        $productPriceSKUWise\n    ) {\n        $discountAmount = &#091;&#039;discount&#039; =&gt; 0, &#039;base_discount&#039; =&gt; 0];\n        if (empty($yProducts) || $yProducts == 0) {\n            $yProducts = 1;\n        }\n        $maxDiscountQty = $maxDiscountQty ? $maxDiscountQty : 1;\n\n        $currentItemSku = $item-&gt;getSku();\n        if ($item-&gt;getProductType() == &#039;bundle&#039;) {\n            $currentItemSku = $this-&gt;getBundleProductSku($item-&gt;getId(), $currentItemSku, $item);\n        }\n\n        $skus = explode(&#039;,&#039;, trim($promoSkus) ?? &#039;&#039;);\n        if (!array_diff($cartSKUs, $skus)) {\n            return $discountAmount;\n        }\n\n        if (in_array($currentItemSku, $cartSKUs) &amp;&amp; in_array($currentItemSku, $skus)) {\n            foreach ($skus as $sku) {\n                if (array_key_exists($sku, $productQty)) {\n                    $productQtyitem = $productQty&#091;$sku];\n                    if ($item-&gt;getProduct()-&gt;getTypeId() == &#039;bundle&#039;) {\n                        $promoSKUPrice = $this-&gt;getBundleProductPrice($sku, $item);\n                    } else {\n                        $promoSKUPrice = $this-&gt;getProductPrice(\n                            $sku,\n                            $productPriceSKUWise\n                        );\n                    }\n                    $qty = $totalQuantity - $productQtyitem;\n                    if (in_array($sku, $cartSKUs) &amp;&amp; $discountStep &lt;= $qty &amp;&amp; $yProducts &lt;= $productQtyitem) {\n                        if ($currentItemSku == $sku) {\n                            $nDiscountQty = min($qty, $productQtyitem, $maxDiscountQty);\n                            $discountAmount&#091;&#039;discount&#039;] = $discountAmount&#091;&#039;discount&#039;] +\n                                ($promoSKUPrice&#091;&#039;price&#039;] * $currentDiscountAmount \/ 100) * $nDiscountQty;\n                            $discountAmount&#091;&#039;base_discount&#039;] = $discountAmount&#091;&#039;base_discount&#039;] +\n                                ($promoSKUPrice&#091;&#039;base_price&#039;] * $currentDiscountAmount \/ 100) * $nDiscountQty;\n                        }\n                    }\n                }\n            }\n        }\n\n        return $discountAmount;\n    }\n\n    \/**\n     * Bundle Product Price\n     *\n     * @param string $sku\n     * @param \\Magento\\Quote\\Model\\Quote\\Item $item\n     * @return array\n     *\/\n    public function getBundleProductPrice($sku, $item)\n    {\n        $price = 0;\n        foreach ($item-&gt;getQuote()-&gt;getAllItems() as $currentItem) {\n            if (\n                $currentItem-&gt;getProductType() == &#039;bundle&#039;\n                &amp;&amp;\n                $currentItem-&gt;getProduct()-&gt;getData(&#039;sku&#039;) == $sku\n            ) {\n                $price = $this-&gt;validator-&gt;getItemPrice($currentItem);;\n                $basePrice = $this-&gt;validator-&gt;getItemBasePrice($currentItem);\n            }\n        }\n        return &#091;&#039;price&#039; =&gt; $price, &#039;base_price&#039; =&gt; $basePrice];\n    }\n\n    \/**\n     * Get Product Price\n     *\n     * @param string $sku\n     * @param array $productPriceSKUWise\n     * @return array\n     *\/\n    public function getProductPrice($sku, $productPriceSKUWise)\n    {\n        $totalSkuPrice = 0;\n        if (isset($productPriceSKUWise&#091;$sku])) {\n            $totalSkuPrice = $productPriceSKUWise&#091;$sku];\n        }\n        return $totalSkuPrice;\n    }\n}<\/pre>\n\n\n\n<p>The discount will be applicable only if the product, for which the admin defines the Promo SKU is present in the cart as shown in the image below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"622\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T203639.958-1200x622.png\" alt=\"Screenshot-2024-02-07T203639.958\" class=\"wp-image-421016\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T203639.958-1200x622.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T203639.958-300x155.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T203639.958-250x130.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T203639.958-768x398.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T203639.958.png 1509w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"607\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T204159.278-1200x607.png\" alt=\"Screenshot-2024-02-07T204159.278\" class=\"wp-image-421017\" srcset=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T204159.278-1200x607.png 1200w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T204159.278-300x152.png 300w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T204159.278-250x126.png 250w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T204159.278-768x389.png 768w, https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-2024-02-07T204159.278.png 1524w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" loading=\"lazy\" \/><\/figure>\n\n\n\n<p>SKU <strong>24-MB01<\/strong> is mentioned in the promo SKU so the cart considered this product as a <strong>Y<\/strong> product and <strong>24-MB05<\/strong> is a non-promo SKU so the cart considered this product as an <strong>X<\/strong> product.<\/p>\n\n\n\n<p>You can also go through the best <a href=\"https:\/\/store.webkul.com\/Magento-2.html?dir=desc&amp;order=date\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 extensions<\/a> catalog list. <\/p>\n\n\n\n<p class=\"has-medium-font-size\">Hope this will be helpful. Thanks \ud83d\ude42<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For an e-commerce business to flourish, it is quite necessary to inculcate effective marketing strategies that lay a strong influence on the customers. Having said this, Magento 2 Special Promotions and discounts are one of the most renowned marketing strategies. As you know Magento 2 provides us with four types of cart rules. In this <a href=\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":590,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121,1],"tags":[15282,12794],"class_list":["post-420958","post","type-post","status-publish","format-standard","hentry","category-magento-2","category-uncategorized","tag-add-custom-fields-in-cart-price-rule","tag-custom-cart-rules"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to create custom cart rule for Buy X Get next Y in Magento 2 - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to create custom cart rule for Buy X Get next Y in Magento 2 - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"For an e-commerce business to flourish, it is quite necessary to inculcate effective marketing strategies that lay a strong influence on the customers. Having said this, Magento 2 Special Promotions and discounts are one of the most renowned marketing strategies. As you know Magento 2 provides us with four types of cart rules. In this [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-07T15:25:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-21T11:40:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15-1200x610.png\" \/>\n<meta name=\"author\" content=\"Nitish Kumar Upadhyay\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nitish Kumar Upadhyay\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/\"},\"author\":{\"name\":\"Nitish Kumar Upadhyay\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/dc2d643eb440a59aa7d72c1150b9ed09\"},\"headline\":\"How to create custom cart rule for Buy X Get next Y in Magento 2\",\"datePublished\":\"2024-02-07T15:25:58+00:00\",\"dateModified\":\"2024-03-21T11:40:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/\"},\"wordCount\":440,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15-1200x610.png\",\"keywords\":[\"Add custom fields in cart price rule\",\"custom cart rules\"],\"articleSection\":[\"Magento 2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/\",\"url\":\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/\",\"name\":\"How to create custom cart rule for Buy X Get next Y in Magento 2 - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15-1200x610.png\",\"datePublished\":\"2024-02-07T15:25:58+00:00\",\"dateModified\":\"2024-03-21T11:40:38+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15.png\",\"width\":1507,\"height\":766,\"caption\":\"Screenshot-from-2024-02-07-19-14-15\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to create custom cart rule for Buy X Get next Y in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/dc2d643eb440a59aa7d72c1150b9ed09\",\"name\":\"Nitish Kumar Upadhyay\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/54d626b859687e935e7a2e6cee7e868245ab9b8d61c3961401c5ce665bbcc6b1?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/54d626b859687e935e7a2e6cee7e868245ab9b8d61c3961401c5ce665bbcc6b1?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Nitish Kumar Upadhyay\"},\"description\":\"Nitish, a skilled Software Engineer, excels in Magento Commerce B2B development. Expertise in crafting custom solutions, optimizing workflows, and integrating seamless eCommerce features drives business growth and operational efficiency.\",\"url\":\"https:\/\/webkul.com\/blog\/author\/nitishkumar-upadhyay742\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to create custom cart rule for Buy X Get next Y in Magento 2 - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/","og_locale":"en_US","og_type":"article","og_title":"How to create custom cart rule for Buy X Get next Y in Magento 2 - Webkul Blog","og_description":"For an e-commerce business to flourish, it is quite necessary to inculcate effective marketing strategies that lay a strong influence on the customers. Having said this, Magento 2 Special Promotions and discounts are one of the most renowned marketing strategies. As you know Magento 2 provides us with four types of cart rules. In this [...]","og_url":"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2024-02-07T15:25:58+00:00","article_modified_time":"2024-03-21T11:40:38+00:00","og_image":[{"url":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15-1200x610.png","type":"","width":"","height":""}],"author":"Nitish Kumar Upadhyay","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Nitish Kumar Upadhyay","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/"},"author":{"name":"Nitish Kumar Upadhyay","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/dc2d643eb440a59aa7d72c1150b9ed09"},"headline":"How to create custom cart rule for Buy X Get next Y in Magento 2","datePublished":"2024-02-07T15:25:58+00:00","dateModified":"2024-03-21T11:40:38+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/"},"wordCount":440,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15-1200x610.png","keywords":["Add custom fields in cart price rule","custom cart rules"],"articleSection":["Magento 2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/","url":"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/","name":"How to create custom cart rule for Buy X Get next Y in Magento 2 - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#primaryimage"},"thumbnailUrl":"https:\/\/webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15-1200x610.png","datePublished":"2024-02-07T15:25:58+00:00","dateModified":"2024-03-21T11:40:38+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2024\/02\/Screenshot-from-2024-02-07-19-14-15.png","width":1507,"height":766,"caption":"Screenshot-from-2024-02-07-19-14-15"},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/how-to-create-a-custom-cart-rule-for-buy-x-get-next-y-with-percent-discount-in-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to create custom cart rule for Buy X Get next Y in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/dc2d643eb440a59aa7d72c1150b9ed09","name":"Nitish Kumar Upadhyay","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/54d626b859687e935e7a2e6cee7e868245ab9b8d61c3961401c5ce665bbcc6b1?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/54d626b859687e935e7a2e6cee7e868245ab9b8d61c3961401c5ce665bbcc6b1?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Nitish Kumar Upadhyay"},"description":"Nitish, a skilled Software Engineer, excels in Magento Commerce B2B development. Expertise in crafting custom solutions, optimizing workflows, and integrating seamless eCommerce features drives business growth and operational efficiency.","url":"https:\/\/webkul.com\/blog\/author\/nitishkumar-upadhyay742\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/420958","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/590"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=420958"}],"version-history":[{"count":29,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/420958\/revisions"}],"predecessor-version":[{"id":429017,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/420958\/revisions\/429017"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=420958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=420958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=420958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}