Skip to content

Commit 6844fc7

Browse files
author
Sergey Semenov
committed
MAGETWO-21349: Advanced Mini Cart.
1 parent 5b93558 commit 6844fc7

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@
5353
"remove": "#mini-cart a.action.delete",
5454
"close": "#btn-minicart-close"
5555
},
56+
"showcart": {
57+
"parent": "span.counter",
58+
"qty": "span.counter-number",
59+
"label": "span.counter-label"
60+
},
61+
"minicart": {
62+
"list": "#mini-cart",
63+
"content": "#minicart-content-wrapper",
64+
"qty": "div.items-total",
65+
"subtotal": "div.subtotal span.price"
66+
},
67+
"item": {
68+
"qty": ":input.cart-item-qty",
69+
"button": ":button.update-cart-item"
70+
},
5671
"confirmMessage": "<?php echo __('Are you sure you would like to remove this item from the shopping cart?') ?>"
5772
}
5873
}

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

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,7 @@ define([
1313
$.widget('mage.sidebar', {
1414
options: {
1515
isRecursive: true,
16-
maxItemsVisible: 3,
17-
selectorItemQty: ':input.cart-item-qty',
18-
selectorItemButton: ':button.update-cart-item',
19-
selectorSummaryQty: '.block-content > div.items-total',
20-
selectorSubtotal: '.block-content > div.subtotal > div.amount span.price',
21-
selectorShowcartNumber: 'a.showcart > span.counter > span.counter-number',
22-
selectorShowcartLabel: 'a.showcart > span.counter > span.counter-label',
23-
selectorShowcart: 'a.showcart > span.counter',
24-
selectorList: '#mini-cart',
25-
selectorContentWrapper: '#minicart-content-wrapper'
16+
maxItemsVisible: 3
2617
},
2718
scrollHeight: 0,
2819

@@ -35,9 +26,13 @@ define([
3526
location.href = this.options.url.checkout;
3627
}, this));
3728

38-
$(this.options.selectorItemQty).keyup(function(event) {
29+
$(this.options.item.qty).keyup(function(event) {
3930
self._showButton($(this));
4031
});
32+
$(this.options.item.button).click(function(event) {
33+
event.stopPropagation();
34+
self._updateQty($(this))
35+
});
4136

4237
$(this.options.button.remove).click(function(event) {
4338
event.stopPropagation();
@@ -46,18 +41,6 @@ define([
4641
}
4742
});
4843

49-
$(this.options.selectorItemButton).click(function(event) {
50-
event.stopPropagation();
51-
self._updateQty($(this))
52-
});
53-
54-
$('body').on('minicart.update', function(event, elem, response) {
55-
event.stopPropagation();
56-
self._refreshQty(response.data.summary_qty, response.data.summary_text);
57-
self._refreshSubtotal(response.data.subtotal);
58-
self._refreshShowcart(response.data.summary_qty, response.data.summary_text);
59-
});
60-
6144
this._initCloseButton();
6245
this._calcHeight();
6346
this._isOverflowed();
@@ -82,7 +65,7 @@ define([
8265
* @private
8366
*/
8467
_isOverflowed: function() {
85-
var list = $(this.options.selectorList);
68+
var list = $(this.options.minicart.list);
8669
if (this.scrollHeight > list.innerHeight()) {
8770
list.parent().addClass('overflowed');
8871
} else {
@@ -138,7 +121,9 @@ define([
138121
*/
139122
_updateQtyAfter: function(elem, response) {
140123
if ($.type(response.data) === 'object') {
141-
$('body').trigger('minicart.update', [elem, response]);
124+
this._refreshQty(response.data.summary_qty, response.data.summary_text);
125+
this._refreshSubtotal(response.data.subtotal);
126+
this._refreshShowcart(response.data.summary_qty, response.data.summary_text);
142127
this._refreshItemQty(elem, response.data.summary_qty);
143128
}
144129
this._hideButton(elem);
@@ -160,11 +145,13 @@ define([
160145
*/
161146
_removeItemAfter: function(elem, response) {
162147
if ($.type(response.data) === 'object') {
163-
$('body').trigger('minicart.update', [elem, response]);
148+
this._refreshQty(response.data.summary_qty, response.data.summary_text);
149+
this._refreshSubtotal(response.data.subtotal);
150+
this._refreshShowcart(response.data.summary_qty, response.data.summary_text);
164151
}
165152
if (response.cleanup === true) {
166-
$(this.options.selectorContentWrapper).replaceWith($.trim(response.content));
167-
$(this.options.selectorShowcart).addClass('empty');
153+
$(this.options.minicart.content).replaceWith($.trim(response.content));
154+
$(this.options.showcart.parent).addClass('empty');
168155
this._initCloseButton();
169156
} else {
170157
elem.closest('li').remove();
@@ -218,29 +205,29 @@ define([
218205
_refreshQty: function(qty, text) {
219206
if (qty != undefined && text != undefined) {
220207
var self = this;
221-
$(this.options.selectorSummaryQty).fadeOut('slow', function() {
222-
$(self.options.selectorSummaryQty).text(qty + text);
208+
$(this.options.minicart.qty).fadeOut('slow', function() {
209+
$(self.options.minicart.qty).html('<span class="count">' + qty + '</span>' + text);
223210
}).fadeIn();
224211
}
225212
},
226213

227214
_refreshSubtotal: function(val) {
228215
if (val != undefined) {
229216
var self = this;
230-
$(this.options.selectorSubtotal).fadeOut('slow', function() {
231-
$(self.options.selectorSubtotal).replaceWith(val);
217+
$(this.options.minicart.subtotal).fadeOut('slow', function() {
218+
$(self.options.minicart.subtotal).replaceWith(val);
232219
}).fadeIn();
233220
}
234221
},
235222

236223
_refreshShowcart: function(qty, text) {
237224
if (qty != undefined && text != undefined) {
238225
var self = this;
239-
$(this.options.selectorShowcartNumber).fadeOut('slow', function() {
240-
$(self.options.selectorShowcartNumber).text(qty);
226+
$(this.options.showcart.qty).fadeOut('slow', function() {
227+
$(self.options.showcart.qty).text(qty);
241228
}).fadeIn();
242-
$(this.options.selectorShowcartLabel).fadeOut('slow', function() {
243-
$(self.options.selectorShowcartLabel).text(text);
229+
$(this.options.showcart.label).fadeOut('slow', function() {
230+
$(self.options.showcart.label).text(text);
244231
}).fadeIn();
245232
}
246233
},
@@ -254,7 +241,7 @@ define([
254241
var self = this,
255242
height = 0,
256243
counter = this.options.maxItemsVisible,
257-
target = $(this.options.selectorList)
244+
target = $(this.options.minicart.list)
258245
.clone()
259246
.attr('style', 'position: absolute !important; top: -10000 !important;')
260247
.appendTo('body');
@@ -269,7 +256,7 @@ define([
269256

270257
target.remove();
271258

272-
$(this.options.selectorList).css('height', height);
259+
$(this.options.minicart.list).css('height', height);
273260
}
274261
});
275262

0 commit comments

Comments
 (0)