I am trying to understand the working of discount coupon codes on the checkout page.
When we apply any coupon code, then the discount is added to cart without reloading the page.
I have checked and found some below points:
- "discount" totals segment is added by Magento_SalesRule module.
When we apply any coupon code then, using vendor\magento\module-sales-rule\view\frontend\web\js\action\set-coupon-code.js
, an ajax request is send to rest/default/V1/carts/mine/coupons/:coupon
.
Code from vendor\magento\module-sales-rule\view\frontend\web\js\action\set-coupon-code.js
:
return storage.put(
url,
{},
false
).done(function (response) {
var deferred;
if (response) {
deferred = $.Deferred();
isApplied(true);
totals.isLoading(true);
getPaymentInformationAction(deferred);
$.when(deferred).done(function ()
{
fullScreenLoader.stopLoader();
totals.isLoading(false);
});
messageContainer.addSuccessMessage({
'message': message
});
}
}).fail(function (response) {
fullScreenLoader.stopLoader();
totals.isLoading(false);
errorProcessor.process(response, messageContainer);
});
- After the above ajax call, Summary total section gets refresh and
Discount appears in it below image.
- When we click on the cancel coupon button then code from
vendor\magento\module-sales-rule\view\frontend\web\js\action\cancel-coupon.js
runs and "Discount" disappears from the summary total section.
How does the total_segments add or remove "discount"?
Best Answer
1) For customer account:
/V1/carts/mine/coupons/:couponCode
I just explain how sale rules work for customer logged
a) Set coupon:
vendor/magento/module-quote/etc/webapi.xml
So, the service class interface class
Magento\Quote\Api\CouponManagementInterface::set()
will be used andMagento\Quote\Model\CouponManagement::set
will be called.When quote collects totals:
$quote->collectTotals()
\Magento\SalesRule\Model\Quote\Discount::collect()
will be called. It will calculate the coupon logic here.b) Remove coupon:
vendor/magento/module-quote/etc/webapi.xml
Magento\Quote\Model\CouponManagement::remove()
will be called.c) Total Segment:
V1/carts/mine/payment-information
vendor/magento/module-checkout/etc/webapi.xml
vendor/magento/module-checkout/Model/PaymentInformationManagement.php
\Magento\Quote\Model\Cart\CartTotalRepository::get()
This will get the totals data and set the total segments.
\Magento\Quote\Model\Quote\TotalsReader::fetch()
2) For guest account, we can see the apis:
vendor/magento/module-quote/etc/webapi.xml