I am using Magento 2 enterprise edition (2.0.6)

I want to apply discount on sub total, Discount is applied from cart page (added a button on cart which applies the discount) and then if I go to checkout discount is visible there.

Now I am trying to add button on checkout second page to apply discount. The discount is applied but the order summary is not updated. If I click on button (checkout page) and goto payment express, it shows the discounted amount.

even if i goto cart page after applying discount on checkout it is visible on cart and then visible on checkout also.

I took reference from how to add fee to order totals in magento2 to apply discount

Below is my code


<?xml version="1.0"?>
<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="custom" instance="[Namespace]\[ModuleName]\Model\Total\Quote\Custom" sort_order="420">

I created Model under [Namespace]/[ModuleName]/Model/Total/Quote/Custom.php

namespace [Namespace]\[ModuleName]\Model\Total\Quote;  

  class Custom extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal

   protected $_priceCurrency;

   public function __construct(
       \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
       \Magento\Checkout\Model\Session                     $checkoutSession,
       \[Namespace]/[ModuleName]\Helper\Data $helper
       $this->_priceCurrency = $priceCurrency;
       $this->checkoutSession             = $checkoutSession;
       $this->helper = $helper;
   public function getDiscountAmount()
        return $this->helper->getDiscountAmount();
    * @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);
      $address             = $shippingAssignment->getShipping()->getAddress();
      $label               = 'My Custom Discount';
      $discountAmount      = -self::getDiscountAmount();   
      $appliedCartDiscount = 0;
      if ($address->getAddressType() == 'shipping') 
            // If a discount exists in cart and another discount is applied, the add both discounts.
            $appliedCartDiscount = $total->getDiscountAmount();
            $discountAmount      = $total->getDiscountAmount()+$discountAmount;
            $label               = $total->getDiscountDescription().', '.$label;

          $total->setSubtotalWithDiscount($total->getSubtotal() + $discountAmount);
          $total->setBaseSubtotalWithDiscount($total->getBaseSubtotal() + $discountAmount);

            $total->addTotalAmount($this->getCode(), $discountAmount - $appliedCartDiscount);
            $total->addBaseTotalAmount($this->getCode(), $discountAmount - $appliedCartDiscount);
            $total->addTotalAmount($this->getCode(), $discountAmount);
            $total->addBaseTotalAmount($this->getCode(), $discountAmount);
      return $this;
  • What's the checkout second page? The Shipping step? Commented Sep 13, 2016 at 10:54
  • First page/tab is shipping address and when you click on next button the second page/tab is payment methods page
    – Ekta Puri
    Commented Sep 13, 2016 at 11:03

1 Answer 1


I got the solution,

below is the explanation of process to approach the result. Please modify accordingly.

Actually the methodology that i followed works perfect for cart page Model file [Namespace]/[ModuleName]/Model/Total/Quote/Custom.php is not called on checkout process, On checkout need to follow the default Magento mechanism using webapi.xml

I followed the process of reward module in enterprise edition


<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <route url="/V1/rewardpoints/mine/use-reward" method="POST">
        <service class="[Namespace]\[ModuleName]\Api\RewardManagementInterface" method="set"/>
            <resource ref="self" />
            <parameter name="cartId" force="true">%cart_id%</parameter>



namespace Namespace]\[ModuleName]\Api;

interface RewardManagementInterface
    public function set($cartId);

create model to extend this interface and have the functionality in the function set($cartId)

as below

public function __construct(
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
        \[Namespace]\[ModuleName]\Helper\Data $helper,
        \[Namespace]\[ModuleName]\Model\PaymentDataImporter $importer
    ) {
        $this->quoteRepository = $quoteRepository;
        $this->helper = $helper;
        $this->importer = $importer;

     * {@inheritdoc}
    public function set($cartId)
        if ($this->helper->getEnabled()) 
            /* @var $quote \Magento\Quote\Model\Quote */
            $quote = $this->quoteRepository->getActive($cartId);
            $this->importer->import($quote, $quote->getPayment(), true);
            return true;
        return false;

create another model PaymentDataImporter which will actually apply discount on checkout process

public function __construct(
        \[Namespace]\[ModuleName]\Helper\Data $helper,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        $this->_scopeConfig = $scopeConfig;
        $this->helper = $helper;


    public function import($quote, $payment, $useRewardPoints)
        $discount = $this->helper->getDiscountAmount();
        return $this;

most important your js action should give call to this webapi stuff

        urlBuilder.createUrl('/modulename/mine/use-reward', {}), {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.