Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    magento-hyperlocal-marketplace-guide

    How To Set Custom Discount Or Fee At Cart In Magento 2

    How To Set Custom Discount Or Fee At Cart In Magento 2 : here i am going to explain , how you can set a discount or fee in cart. it obvious that if you set a discount, you have to subtract the discount amount from the cart price and if you want to set fee on cart price. then it will be added.

    1 => first you have to create a sales.xml file on app/code/companyname/module/etc


    2 => create file CustomDiscount.php on this path app\code\companyname\module\Model\Quote\Address\Total


    3 => create file checkout_cart_index.xml on path app\code\companyname\module\view\frontend\layout


    4 => create another file on same location.


    5 => create file custom-discount.js on path app/code/companyname/module/view/frontend/web/js/view/checkout/cart/totals


    6 => create file on this path custom-discount.js app/code/companyname/module/view/frontend/web/js/view/checkout/summary


    7 => create file custom-discount.html on path app/code/companyname/module/view/frontend/web/template/checkout/cart/totals


    8 => create file custom-discount.html on path app/code/companyname/module/view/frontend/web/template/checkout/summary

    finally you can see the custom discount amount on your cart and checkout page.

    hope so it will help you !

    . . .

    Comments (9)

    Add Your Comment

  • Aman
    Great post thanks, i have used above code and facing one issue. Discount is applied successfully but its effect on tax rates. I have configured tax rate apply after discount but with this code tax apply with this code can you please confirm what is missing here in this code for tax scenario.

    Thank Again.

  • Andžej Teličan
    Hello
    When I use this custom discount, I can’t purchase. In my payment method I have a message “‘The requested Payment Method is not available.”

    In log files no errors

    If discount amount is 0 all is good.

  • Andžej Teličan
    How get discount in
    getCustomdiscountTotal : function () {
    var price = 10;
    return this.getFormattedPrice(price);
    }
    with out hardcode
    • Webkul Support
      you can get the coupon amount using this.
      price = totals.getSegment(‘coupondiscount_total’).value;
      • Andžej Teličan
        Thanks. it works
        • Webkul Support
          you are welcome.
  • Jessy Loran
    If you check as 100% discount amount it store quote grand total is minus value and due to minus value in quote grand total magento2 does not allow checkout with free payment method as it compare amount with equal zero
  • Felipe Castilla
    When you write 4. create another file on same location. you are talking about checkout_index_index.xml or I need to create a file with what name?
  • Masterarts
    Helpful and nicely done tutorial. Setting up custom discount in Magento 2 seems easy. Checkout Magenticians magento tutorial blog as well.
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close