Skip to content

Commit 357b6e6

Browse files
MAGETWO-69533: [BUGFIX][6244] Fix Issue with code label display in cart checkout. #9721
2 parents 0b48e93 + b344793 commit 357b6e6

File tree

6 files changed

+138
-7
lines changed

6 files changed

+138
-7
lines changed

app/code/Magento/Checkout/view/frontend/web/js/model/quote.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ define([
1111
], function (ko, _) {
1212
'use strict';
1313

14+
var proceedTotalsData = function (data) {
15+
if (_.isObject(data) && _.isObject(data['extension_attributes'])) {
16+
_.each(data['extension_attributes'], function (element, index) {
17+
data[index] = element;
18+
});
19+
}
20+
21+
return data;
22+
};
23+
1424
var billingAddress = ko.observable(null),
1525
shippingAddress = ko.observable(null),
1626
shippingMethod = ko.observable(null),
@@ -19,7 +29,7 @@ define([
1929
basePriceFormat = window.checkoutConfig.basePriceFormat,
2030
priceFormat = window.checkoutConfig.priceFormat,
2131
storeCode = window.checkoutConfig.storeCode,
22-
totalsData = window.checkoutConfig.totalsData,
32+
totalsData = proceedTotalsData(window.checkoutConfig.totalsData),
2333
totals = ko.observable(totalsData),
2434
collectedTotals = ko.observable({});
2535

@@ -78,11 +88,7 @@ define([
7888
* @param {Object} data
7989
*/
8090
setTotals: function (data) {
81-
if (_.isObject(data) && _.isObject(data['extension_attributes'])) {
82-
_.each(data['extension_attributes'], function (element, index) {
83-
data[index] = element;
84-
});
85-
}
91+
data = proceedTotalsData(data);
8692
totals(data);
8793
this.setCollectedTotals('subtotal_with_discount', parseFloat(data['subtotal_with_discount']));
8894
},

app/code/Magento/Quote/etc/extension_attributes.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@
99
<extension_attributes for="Magento\Quote\Api\Data\CartInterface">
1010
<attribute code="shipping_assignments" type="Magento\Quote\Api\Data\ShippingAssignmentInterface[]" />
1111
</extension_attributes>
12+
13+
<extension_attributes for="Magento\Quote\Api\Data\TotalsInterface">
14+
<attribute code="coupon_label" type="string" />
15+
</extension_attributes>
1216
</config>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\SalesRule\Plugin;
7+
8+
use Magento\Store\Model\StoreManagerInterface;
9+
10+
/**
11+
* Class CartTotalRepository
12+
* @package Magento\SalesRule\Plugin
13+
*/
14+
class CartTotalRepository
15+
{
16+
/**
17+
* @var \Magento\Quote\Api\Data\TotalsExtensionFactory
18+
*/
19+
private $extensionFactory;
20+
21+
/**
22+
* @var \Magento\SalesRule\Api\RuleRepositoryInterface
23+
*/
24+
private $ruleRepository;
25+
26+
/**
27+
* @var \Magento\SalesRule\Model\Coupon
28+
*/
29+
private $coupon;
30+
31+
/**
32+
* @var StoreManagerInterface
33+
*/
34+
private $storeManager;
35+
36+
/**
37+
* CartTotalRepository constructor.
38+
* @param \Magento\Quote\Api\Data\TotalsExtensionFactory $extensionFactory
39+
* @param \Magento\SalesRule\Api\RuleRepositoryInterface $ruleRepository
40+
* @param \Magento\SalesRule\Model\Coupon $coupon
41+
* @param StoreManagerInterface $storeManager
42+
*/
43+
public function __construct(
44+
\Magento\Quote\Api\Data\TotalsExtensionFactory $extensionFactory,
45+
\Magento\SalesRule\Api\RuleRepositoryInterface $ruleRepository,
46+
\Magento\SalesRule\Model\Coupon $coupon,
47+
StoreManagerInterface $storeManager
48+
) {
49+
$this->extensionFactory = $extensionFactory;
50+
$this->ruleRepository = $ruleRepository;
51+
$this->coupon = $coupon;
52+
$this->storeManager = $storeManager;
53+
}
54+
55+
/**
56+
* @param \Magento\Quote\Model\Cart\CartTotalRepository $subject
57+
* @param \Magento\Quote\Api\Data\TotalsInterface $result
58+
* @return \Magento\Quote\Api\Data\TotalsInterface
59+
*
60+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
61+
*/
62+
public function afterGet(
63+
\Magento\Quote\Model\Cart\CartTotalRepository $subject,
64+
\Magento\Quote\Api\Data\TotalsInterface $result
65+
) {
66+
if ($result->getExtensionAttributes() === null) {
67+
$extensionAttributes = $this->extensionFactory->create();
68+
$result->setExtensionAttributes($extensionAttributes);
69+
}
70+
71+
$extensionAttributes = $result->getExtensionAttributes();
72+
$couponCode = $result->getCouponCode();
73+
74+
if (empty($couponCode)) {
75+
return $result;
76+
}
77+
78+
$this->coupon->loadByCode($couponCode);
79+
$ruleId = $this->coupon->getRuleId();
80+
81+
if (empty($ruleId)) {
82+
return $result;
83+
}
84+
85+
$storeId = $this->storeManager->getStore()->getId();
86+
$rule = $this->ruleRepository->getById($ruleId);
87+
88+
$storeLabel = $storeLabelFallback = null;
89+
90+
/* @var $label \Magento\SalesRule\Model\Data\RuleLabel */
91+
foreach ($rule->getStoreLabels() as $label) {
92+
if ($label->getStoreId() === 0) {
93+
$storeLabelFallback = $label->getStoreLabel();
94+
}
95+
96+
if ($label->getStoreId() == $storeId) {
97+
$storeLabel = $label->getStoreLabel();
98+
break;
99+
}
100+
}
101+
102+
$extensionAttributes->setCouponLabel(($storeLabel) ? $storeLabel : $storeLabelFallback);
103+
$result->setExtensionAttributes($extensionAttributes);
104+
return $result;
105+
}
106+
}

app/code/Magento/SalesRule/etc/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,8 @@
178178
</argument>
179179
</arguments>
180180
</type>
181+
182+
<type name="\Magento\Quote\Model\Cart\CartTotalRepository">
183+
<plugin name="coupon_label_plugin" type="Magento\SalesRule\Plugin\CartTotalRepository" />
184+
</type>
181185
</config>

app/code/Magento/SalesRule/view/frontend/web/js/view/summary/discount.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ define([
3333
return this.totals()['coupon_code'];
3434
},
3535

36+
/**
37+
* @return {*}
38+
*/
39+
getCouponLabel: function () {
40+
if (!this.totals()) {
41+
return null;
42+
}
43+
44+
return this.totals()['coupon_label'];
45+
},
46+
3647
/**
3748
* @return {Number}
3849
*/

app/code/Magento/SalesRule/view/frontend/web/template/cart/totals/discount.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<tr class="totals">
99
<th colspan="1" style="" class="mark" scope="row">
1010
<span class="title" data-bind="text: title"></span>
11-
<span class="discount coupon" data-bind="text: getCouponCode()"></span>
11+
<span class="discount coupon" data-bind="text: getCouponLabel()"></span>
1212
</th>
1313
<td class="amount" data-bind="attr: {'data-th': title}">
1414
<span><span class="price" data-bind="text: getValue()"></span></span>

0 commit comments

Comments
 (0)