Support & Downloads

Quisque actraqum nunc no dolor sit ametaugue dolor. Lorem ipsum dolor sit amet, consyect etur adipiscing elit.

s f

Contact Info
198 West 21th Street, Suite 721
New York, NY 10010
[email protected]
+88 (0) 101 0000 000
Discount in Magento 2

How to Programmatically Add Custom Discount in Magento 2

Sometimes E-commerce businesses go through the period when the sales go down than the regular season. In this time to increase sales, bring new customers and retain the old ones, store owners implement sales promotion techniques and offering discounts is one of them. Discount offers are the short term tactics to boost your sale. Customers are generally attracted to the sale and it benefits your stores!

Being a Magento 2 store owner, you may need this strategy to implement in your store. Rather than simply applying discount percentage, you may require to Programmatically Add custom discounts in Magento 2 as a part of the marketing strategies and increase the sale. In order to do so, you need to cut down the price of the product that customers need to pay. The technical work to implement this strategy is important and I have tried to prepare an easy step by step guide here.

Steps to Programmatically Add Custom Discount in Magento 2:

  • Register a total in the file sale.xml located at app/code/Meetanshi/HelloWorld/etc/sales.xml

Add discount to change the total in the model at app/code/Meetanshi/HelloWorld/Model/Total/Quote/Custom.php:

Now, you will see the Grand Total was changed. But there is no total discount information. Because the Magento use knockout js to show the total.

  • Add the total in the Layout file resided at app/code/Meetanshi/HelloWorld/view/frontend/layout/checkout_cart_index.xml

View Model KnockOut, app/code/Meetanshi/HelloWorld/view/frontend/web/js/view/checkout/summary/customdiscount.js

the total discount will be shown in the template knockout, app/code/Meetanshi/HelloWorld/view/frontend/web/template/checkout/summary/customdiscount.html

The result will be seen as below with the name and the value of the custom discount when you complete the above steps successfully!

Offering custom discounts in your Magento 2 store is always a good idea to boost sales. Try the code now and let me know how this strategy helped you to boost store sales.

Hopefully, you are now familiar with programmatically adding custom discount in Magento 2. I am really eager to get your feedback and suggestions. If you need help with any errors in the implementation, let me know in the Comments section below.

Sagar Doshi

CEO / Managing Partner

[email protected]

Post a Comment