How to Programmatically Add Custom 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!

How to Programmatically Add Custom Discount in Magento 2 1

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.

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

(based on 15 Reviews)

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

6 comments On How to Programmatically Add Custom Discount in Magento 2

  • Hi,
    Can we show the custom discount in admin side order,invoice,shipment and credit memos and email notifications also?

  • Hi,

    What should I write instead of $10 because you have passed hard coded value to Cart Discount section. Please reply me asap.


    • Hello Mohit,
      You can change the code as per your requirements and dynamically pass the value.

      • Thanks for reply Sanjay, but I want to get the discount value with correct pricing format from discount defined in Collect function. Can you please me on this asap.

Leave a reply:

Your email address will not be published.