Today, we’re going to teach you guys how to add a custom discount programmatically in Magento 2 store.
Many times, all eCommerce businesses go through a period where the sales go down.
During this time, eCommerce business owners often implement various sales promotion strategies like offering special discounts to bring new customers & retain the existing ones to increase sales.
And in this tutorial, we’re going to teach you exactly how to add a custom discount in your Magento 2 store to increase your sales during the slow season.
Step-by-Step Process to Add a Custom Discount Programmatically in Magento 2
In order to add a custom discount programmatically in your Magento 2 store, you need to implement the below-mentioned steps to configure a custom discount in your store.
Step #1
First of all, you need to enter a total in the sale.xml file inside app/code/MageDelight/HelloWorld/etc/ folder and copy the following code.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd"> <section name="quote"> <group name="totals"> <item name="customer_discount" instance="MageDelight\HelloWorld\Model\Total\Quote\Custom" sort_order="420"/> </group> </section> </config>
Step #2
After that, we need to decide and insert the value of the custom discount in the Custom.php model file inside the app/code/MageDelight/HelloWorld/Model/Total/Quote/ folder and copy the following code.
<?php namespace MageDelight\HelloWorld\Model\Total\Quote; /** * Class Custom * @package MageDelight\HelloWorld\Model\Total\Quote */ class Custom extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal { /** * @var \Magento\Framework\Pricing\PriceCurrencyInterface */ protected $_priceCurrency; /** * Custom constructor. * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency */ public function __construct( \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency ){ $this->_priceCurrency = $priceCurrency; } /** * @param \Magento\Quote\Model\Quote $quote * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment * @param \Magento\Quote\Model\Quote\Address\Total $total * @return $this|bool */ public function collect( \Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total ) { parent::collect($quote, $shippingAssignment, $total); $baseDiscount = 10; $discount = $this->_priceCurrency->convert($baseDiscount); $total->addTotalAmount('customdiscount', -$discount); $total->addBaseTotalAmount('customdiscount', -$baseDiscount); $total->setBaseGrandTotal($total->getBaseGrandTotal() - $baseDiscount); $quote->setCustomDiscount(-$discount); return $this; } }
<h2">Step #3
Once you implement the above two steps, the grand total in your store will be changed with the updated price. However, your customers will not be able to see any information about the discount.
For that, we need to execute a command to add the total in the checkout_cart_index.xml layout file inside app/code/MageDelight/HelloWorld/view/frontend/layout/ folder and copy the following code.
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="checkout.cart.totals"> <arguments> <argument name="jsLayout" xsi:type="array"> <item name="components" xsi:type="array"> <item name="block-totals" xsi:type="array"> <item name="children" xsi:type="array"> <item name="custom_discount" xsi:type="array"> <item name="component" xsi:type="string">MageDelight_HelloWorld/js/view/checkout/summary/customdiscount</item> <item name="sortOrder" xsi:type="string">20</item> <item name="config" xsi:type="array"> <item name="custom_discount" xsi:type="string" translate="true">Custom Discount</item> </item> </item> </item> </item> </item> </argument> </arguments> </referenceBlock> </body> </page>
Step #4
Lastly, we need to display the total by calling the model knockout customdiscount.js file inside app/code/MageDelight/HelloWorld/view/frontend/web/js/view/checkout/summary/ folder and copy the following code.
define( [ 'jquery', 'Magento_Checkout/js/view/summary/abstract-total' ], function ($,Component) { "use strict"; return Component.extend({ defaults: { template: 'MageDelight_HelloWorld/checkout/summary/customdiscount' }, isDisplayedCustomdiscount : function(){ return true; }, getCustomDiscount : function(){ return '$10'; } }); } );
Finally, we also need to get the total discount in the customdiscount.html template knockout file inside app/code/MageDelight/HelloWorld/view/frontend/web/template/checkout/summary/ folder and add the following code.
<!-- ko if: isDisplayedCustomdiscount() --> <tr class="totals customdiscount excl"> <th class="mark" colspan="1" scope="row" data-bind="text: custom_discount"></th> <td class="amount"> <span class="price" data-bind="text: getCustomDiscount(), attr: {'data-th': custom_discount}"></span> </td> </tr> <!-- /ko →
And that’s it!
Wrapping Up…
Once you follow all these steps, the custom discount will be automatically applied in the customers’ carts with the value we defined for the custom discount.
With that being said, we hope that you found this tutorial helpful. If you have any queries, please ask them in the comments below.
And if you need our professional assistance with Magento eCommerce Agency, feel free to reach out to us.
Recommended: Magento 2 Price Per Customer to offer Custom Price to Customers
Also, you can refer this Magento 2 Price Per Customer Extension's FAQ Page for most common question and it’s answers.