diff --git a/app/code/Magento/Checkout/view/frontend/web/js/model/quote.js b/app/code/Magento/Checkout/view/frontend/web/js/model/quote.js index d497afad66dd2..5f3fd12296b4c 100644 --- a/app/code/Magento/Checkout/view/frontend/web/js/model/quote.js +++ b/app/code/Magento/Checkout/view/frontend/web/js/model/quote.js @@ -11,6 +11,16 @@ define([ ], function (ko, _) { 'use strict'; + var proceedTotalsData = function (data) { + if (_.isObject(data) && _.isObject(data['extension_attributes'])) { + _.each(data['extension_attributes'], function (element, index) { + data[index] = element; + }); + } + + return data; + }; + var billingAddress = ko.observable(null), shippingAddress = ko.observable(null), shippingMethod = ko.observable(null), @@ -19,7 +29,7 @@ define([ basePriceFormat = window.checkoutConfig.basePriceFormat, priceFormat = window.checkoutConfig.priceFormat, storeCode = window.checkoutConfig.storeCode, - totalsData = window.checkoutConfig.totalsData, + totalsData = proceedTotalsData(window.checkoutConfig.totalsData), totals = ko.observable(totalsData), collectedTotals = ko.observable({}); @@ -78,11 +88,7 @@ define([ * @param {Object} data */ setTotals: function (data) { - if (_.isObject(data) && _.isObject(data['extension_attributes'])) { - _.each(data['extension_attributes'], function (element, index) { - data[index] = element; - }); - } + data = proceedTotalsData(data); totals(data); this.setCollectedTotals('subtotal_with_discount', parseFloat(data['subtotal_with_discount'])); }, diff --git a/app/code/Magento/Quote/etc/extension_attributes.xml b/app/code/Magento/Quote/etc/extension_attributes.xml index 2f2ba59a1188d..6fff29e32d2ab 100644 --- a/app/code/Magento/Quote/etc/extension_attributes.xml +++ b/app/code/Magento/Quote/etc/extension_attributes.xml @@ -9,4 +9,8 @@ + + + + diff --git a/app/code/Magento/SalesRule/Plugin/CartTotalRepository.php b/app/code/Magento/SalesRule/Plugin/CartTotalRepository.php new file mode 100644 index 0000000000000..dca1031888f53 --- /dev/null +++ b/app/code/Magento/SalesRule/Plugin/CartTotalRepository.php @@ -0,0 +1,106 @@ +extensionFactory = $extensionFactory; + $this->ruleRepository = $ruleRepository; + $this->coupon = $coupon; + $this->storeManager = $storeManager; + } + + /** + * @param \Magento\Quote\Model\Cart\CartTotalRepository $subject + * @param \Magento\Quote\Api\Data\TotalsInterface $result + * @return \Magento\Quote\Api\Data\TotalsInterface + */ + public function afterGet( + \Magento\Quote\Model\Cart\CartTotalRepository $subject, + \Magento\Quote\Api\Data\TotalsInterface $result + ) { + if ($result->getExtensionAttributes() === null) { + $extensionAttributes = $this->extensionFactory->create(); + $result->setExtensionAttributes($extensionAttributes); + } + + $extensionAttributes = $result->getExtensionAttributes(); + $couponCode = $result->getCouponCode(); + + if (empty($couponCode)) { + return $result; + } + + $this->coupon->loadByCode($couponCode); + $ruleId = $this->coupon->getRuleId(); + + if (empty($ruleId)) { + return $result; + } + + $storeId = $this->storeManager->getStore()->getId(); + $rule = $this->ruleRepository->getById($ruleId); + + $storeLabel = $storeLabelFallback = null; + + /* @var $label \Magento\SalesRule\Model\Data\RuleLabel */ + foreach ($rule->getStoreLabels() as $label) { + + if ($label->getStoreId() === 0) { + $storeLabelFallback = $label->getStoreLabel(); + } + + if ($label->getStoreId() == $storeId) { + $storeLabel = $label->getStoreLabel(); + break; + } + } + + $extensionAttributes->setCouponLabel(($storeLabel) ? $storeLabel : $storeLabelFallback); + + $result->setExtensionAttributes($extensionAttributes); + + return $result; + } +} diff --git a/app/code/Magento/SalesRule/etc/di.xml b/app/code/Magento/SalesRule/etc/di.xml index 5939e0331da0f..a8c350457a5a6 100644 --- a/app/code/Magento/SalesRule/etc/di.xml +++ b/app/code/Magento/SalesRule/etc/di.xml @@ -178,4 +178,8 @@ + + + + diff --git a/app/code/Magento/SalesRule/view/frontend/web/js/view/summary/discount.js b/app/code/Magento/SalesRule/view/frontend/web/js/view/summary/discount.js index 4984a7c52134c..5b04700596272 100644 --- a/app/code/Magento/SalesRule/view/frontend/web/js/view/summary/discount.js +++ b/app/code/Magento/SalesRule/view/frontend/web/js/view/summary/discount.js @@ -33,6 +33,17 @@ define([ return this.totals()['coupon_code']; }, + /** + * @return {*} + */ + getCouponLabel: function () { + if (!this.totals()) { + return null; + } + + return this.totals()['coupon_label']; + }, + /** * @return {Number} */ diff --git a/app/code/Magento/SalesRule/view/frontend/web/template/cart/totals/discount.html b/app/code/Magento/SalesRule/view/frontend/web/template/cart/totals/discount.html index 1fa9efe948cff..4b70b4b110c97 100644 --- a/app/code/Magento/SalesRule/view/frontend/web/template/cart/totals/discount.html +++ b/app/code/Magento/SalesRule/view/frontend/web/template/cart/totals/discount.html @@ -8,7 +8,7 @@ - +