Skip to content

Commit eec3cf1

Browse files
author
Ievgen Shakhsuvarov
committed
MAGETWO-43159: [Dev experience] Incorrect binding for cart().extra_actions
1 parent 825e9c4 commit eec3cf1

File tree

5 files changed

+67
-27
lines changed

5 files changed

+67
-27
lines changed

app/code/Magento/Checkout/view/frontend/templates/cart/minicart.phtml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
data-bind="scope: 'minicart_content'">
1515
<span class="text"><?php /* @escapeNotVerified */ echo __('My Cart'); ?></span>
1616
<span class="counter qty empty"
17-
data-bind="css: { empty: cart().summary_count == 0 }, blockLoader: isLoading">
18-
<span class="counter-number"><!-- ko text: cart().summary_count --><!-- /ko --></span>
17+
data-bind="css: { empty: !!getCartParam('summary_count') == false }, blockLoader: isLoading">
18+
<span class="counter-number"><!-- ko text: getCartParam('summary_count') --><!-- /ko --></span>
1919
<span class="counter-label">
20-
<!-- ko if: cart().summary_count -->
21-
<!-- ko text: cart().summary_count --><!-- /ko -->
20+
<!-- ko if: getCartParam('summary_count') -->
21+
<!-- ko text: getCartParam('summary_count') --><!-- /ko -->
2222
<!-- ko i18n: 'items' --><!-- /ko -->
2323
<!-- /ko -->
2424
</span>

app/code/Magento/Checkout/view/frontend/web/js/view/minicart.js

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ define([
77
'Magento_Customer/js/customer-data',
88
'jquery',
99
'ko',
10+
'underscore',
1011
'sidebar'
11-
], function (Component, customerData, $, ko) {
12+
], function (Component, customerData, $, ko, _) {
1213
'use strict';
1314

1415
var sidebarInitialized = false;
@@ -69,20 +70,29 @@ define([
6970

7071
return Component.extend({
7172
shoppingCartUrl: window.checkout.shoppingCartUrl,
73+
cart: {},
74+
75+
/**
76+
* @override
77+
*/
7278
initialize: function () {
73-
var self = this;
74-
this._super();
75-
this.cart = customerData.get('cart');
76-
this.cart.subscribe(function () {
79+
var self = this,
80+
cartData = customerData.get('cart');
81+
82+
this.update(cartData());
83+
cartData.subscribe(function (updatedCart) {
7784
addToCartCalls--;
7885
this.isLoading(addToCartCalls > 0);
7986
sidebarInitialized = false;
87+
this.update(updatedCart);
8088
initSidebar();
8189
}, this);
8290
$('[data-block="minicart"]').on('contentLoading', function(event) {
8391
addToCartCalls++;
8492
self.isLoading(true);
8593
});
94+
95+
return this._super();
8696
},
8797
isLoading: ko.observable(false),
8898
initSidebar: initSidebar,
@@ -96,6 +106,36 @@ define([
96106
},
97107
getItemRenderer: function (productType) {
98108
return this.itemRenderer[productType] || 'defaultRenderer';
109+
},
110+
111+
/**
112+
* Update mini shopping cart content.
113+
*
114+
* @param {Object} updatedCart
115+
* @returns void
116+
*/
117+
update: function (updatedCart) {
118+
_.each(updatedCart, function (value, key) {
119+
if (!this.cart.hasOwnProperty(key)) {
120+
this.cart[key] = ko.observable();
121+
}
122+
this.cart[key](value);
123+
}, this);
124+
},
125+
126+
/**
127+
* Get cart param by name.
128+
* @param {String} name
129+
* @returns {*}
130+
*/
131+
getCartParam: function (name) {
132+
if (!_.isUndefined(name)) {
133+
if (!this.cart.hasOwnProperty(name)) {
134+
this.cart[name] = ko.observable();
135+
}
136+
}
137+
138+
return this.cart[name]();
99139
}
100140
});
101141
});

app/code/Magento/Checkout/view/frontend/web/template/minicart/content.html

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
<span class="text"><!-- ko i18n: 'My Cart' --><!-- /ko --></span>
1010
<span
1111
class="qty empty"
12-
data-bind="css: { empty: cart().summary_count == 0 },
12+
data-bind="css: { empty: !!getCartParam('summary_count') == false },
1313
attr: { title: $t('Items in Cart') }">
14-
<!-- ko text: cart().summary_count --><!-- /ko -->
14+
<!-- ko text: getCartParam('summary_count') --><!-- /ko -->
1515
</span>
1616
</strong>
1717
</div>
@@ -25,18 +25,18 @@
2525
<span><!-- ko i18n: 'Close' --><!-- /ko --></span>
2626
</button>
2727

28-
<!-- ko if: cart().summary_count -->
28+
<!-- ko if: getCartParam('summary_count') -->
2929
<div class="items-total">
30-
<span class="count"><!-- ko text: cart().summary_count --><!-- /ko --></span>
31-
<!-- ko if: cart().summary_count == 1 -->
30+
<span class="count"><!-- ko text: getCartParam('summary_count') --><!-- /ko --></span>
31+
<!-- ko if: getCartParam('summary_count') == 1 -->
3232
<!-- ko i18n: 'item' --><!-- /ko -->
3333
<!-- /ko -->
34-
<!-- ko if: cart().summary_count > 1 -->
34+
<!-- ko if: getCartParam('summary_count') > 1 -->
3535
<!-- ko i18n: 'items' --><!-- /ko -->
3636
<!-- /ko -->
3737
</div>
3838

39-
<!-- ko if: cart().possible_onepage_checkout -->
39+
<!-- ko if: getCartParam('possible_onepage_checkout') -->
4040
<!-- ko foreach: getRegion('subtotalContainer') -->
4141
<!-- ko template: getTemplate() --><!-- /ko -->
4242
<!-- /ko -->
@@ -46,7 +46,7 @@
4646
<!-- ko template: getTemplate() --><!-- /ko -->
4747
<!-- /ko -->
4848

49-
<!-- ko if: cart().possible_onepage_checkout -->
49+
<!-- ko if: getCartParam('possible_onepage_checkout') -->
5050
<div class="actions">
5151
<div class="primary">
5252
<button
@@ -56,29 +56,29 @@
5656
data-bind="attr: {title: $t('Go to Checkout')}">
5757
<!-- ko i18n: 'Go to Checkout' --><!-- /ko -->
5858
</button>
59-
<div data-bind="html: cart().extra_actions"></div>
59+
<div data-bind="html: getCartParam('extra_actions')"></div>
6060
</div>
6161
</div>
6262
<!-- /ko -->
6363
<!-- /ko -->
6464

65-
<!-- ko if: cart().summary_count -->
65+
<!-- ko if: getCartParam('summary_count') -->
6666
<strong class="subtitle"><!-- ko i18n: 'Recently added item(s)' --><!-- /ko --></strong>
6767
<div data-action="scroll" class="minicart-items-wrapper">
68-
<ol id="mini-cart" class="minicart-items" data-bind="foreach: { data: cart().items, as: 'item' }">
68+
<ol id="mini-cart" class="minicart-items" data-bind="foreach: { data: getCartParam('items'), as: 'item' }">
6969
<!-- ko foreach: $parent.getRegion($parent.getItemRenderer(item.product_type)) -->
7070
<!-- ko template: {name: getTemplate(), data: item, afterRender: function() {$parents[1].initSidebar()}} --><!-- /ko -->
7171
<!-- /ko -->
7272
</ol>
7373
</div>
7474
<!-- /ko -->
7575

76-
<!-- ko ifnot: cart().summary_count -->
76+
<!-- ko ifnot: getCartParam('summary_count') -->
7777
<strong class="subtitle empty" data-bind="visible: closeSidebar()">
7878
<!-- ko i18n: 'You have no items in your shopping cart.' --><!-- /ko -->
7979
</strong>
80-
<!-- ko if: cart().cart_empty_message -->
81-
<p class="minicart empty text"><!-- ko text: cart().cart_empty_message --><!-- /ko --></p>
80+
<!-- ko if: getCartParam('cart_empty_message') -->
81+
<p class="minicart empty text"><!-- ko text: getCartParam('cart_empty_message') --><!-- /ko --></p>
8282

8383
<div class="actions">
8484
<div class="secondary">
@@ -90,7 +90,7 @@
9090
<!-- /ko -->
9191
<!-- /ko -->
9292

93-
<!-- ko if: cart().summary_count -->
93+
<!-- ko if: getCartParam('summary_count') -->
9494
<div class="actions">
9595
<div class="secondary">
9696
<a class="action viewcart" data-bind="attr: {href: shoppingCartUrl}">

app/code/Magento/Checkout/view/frontend/web/template/minicart/subtotal/totals.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
*/
66
-->
77
<div class="amount">
8-
<span data-bind="html: cart().subtotal"></span>
8+
<span data-bind="html: getCartParam('subtotal')"></span>
99
</div>

app/code/Magento/Tax/view/frontend/web/template/checkout/minicart/subtotal/totals.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
<span class="price-wrapper" data-bind="html: cart().subtotal_excl_tax"></span>
1212
<!-- /ko -->
1313

14-
<!-- ko if: !display_cart_subtotal_excl_tax && display_subtotal_incl_tax -->
14+
<!-- ko if: !display_cart_subtotal_excl_tax && display_cart_subtotal_incl_tax -->
1515
<span class="price-wrapper" data-bind="html: cart().subtotal_incl_tax"></span>
1616
<!-- /ko -->
1717

18-
<!-- ko if: !display_cart_subtotal_excl_tax && !display_subtotal_incl_tax -->
18+
<!-- ko if: !display_cart_subtotal_excl_tax && !display_cart_subtotal_incl_tax -->
1919
<span class="price-wrapper price-including-tax"
2020
data-bind="attr: { 'data-label': $t('Incl. Tax') }, html: cart().subtotal_incl_tax">
2121
</span>

0 commit comments

Comments
 (0)