Skip to content

Commit bf12959

Browse files
committed
MAGETWO-89532: Google Analytics - addToCart event triggers before adding to cart
1 parent 44502ff commit bf12959

File tree

2 files changed

+133
-5
lines changed
  • app/code/Magento/Checkout/view/frontend/web/js
  • dev/tests/js/jasmine/tests/app/code/Magento/Checkout/frontend/js

2 files changed

+133
-5
lines changed

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ define([
99
'Magento_Customer/js/customer-data',
1010
'Magento_Ui/js/modal/alert',
1111
'Magento_Ui/js/modal/confirm',
12+
'underscore',
1213
'jquery/ui',
1314
'mage/decorate',
1415
'mage/collapsible',
1516
'mage/cookies'
16-
], function ($, authenticationPopup, customerData, alert, confirm) {
17+
], function ($, authenticationPopup, customerData, alert, confirm, _) {
1718
'use strict';
1819

1920
$.widget('mage.sidebar', {
@@ -219,6 +220,11 @@ define([
219220
* @param {HTMLElement} elem
220221
*/
221222
_updateItemQtyAfter: function (elem) {
223+
var productData = this._getProductById(Number(elem.data('cart-item')));
224+
225+
if (!_.isUndefined(productData)) {
226+
$(document).trigger('ajax:updateCartItemQty', productData['product_sku']);
227+
}
222228
this._hideItemButton(elem);
223229
},
224230

@@ -241,11 +247,24 @@ define([
241247
* @private
242248
*/
243249
_removeItemAfter: function (elem) {
244-
var productData = customerData.get('cart')().items.find(function (item) {
245-
return Number(elem.data('cart-item')) === Number(item['item_id']);
246-
});
250+
var productData = this._getProductById(Number(elem.data('cart-item')));
251+
252+
if (!_.isUndefined(productData)) {
253+
$(document).trigger('ajax:removeFromCart', productData['product_sku']);
254+
}
255+
},
247256

248-
$(document).trigger('ajax:removeFromCart', productData['product_sku']);
257+
/**
258+
* Retrieves product data by Id.
259+
*
260+
* @param {Number} productId - product Id
261+
* @returns {Object|undefined}
262+
* @private
263+
*/
264+
_getProductById: function (productId) {
265+
return _.find(customerData.get('cart')().items, function (item) {
266+
return productId === Number(item['item_id']);
267+
});
249268
},
250269

251270
/**
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
/* eslint-disable max-nested-callbacks */
6+
/*jscs:disable jsDoc*/
7+
8+
define([
9+
'squire',
10+
'jquery',
11+
'ko'
12+
], function (Squire, $, ko) {
13+
'use strict';
14+
15+
describe('Magento_Checkout/js/sidebar', function () {
16+
var injector = new Squire(),
17+
mocks = {
18+
'Magento_Customer/js/customer-data': {
19+
get: function () {
20+
return ko.observable();
21+
}
22+
}
23+
},
24+
sidebar,
25+
cartData = {
26+
'items': [
27+
{
28+
'item_id': 1,
29+
'product_sku': 'bundle'
30+
},
31+
{
32+
'item_id': 5,
33+
'product_sku': 'simple'
34+
},
35+
{
36+
'item_id': 7,
37+
'product_sku': 'configurable'
38+
}
39+
]
40+
},
41+
cart = ko.observable(cartData);
42+
43+
beforeEach(function (done) {
44+
injector.mock(mocks);
45+
injector.require(['Magento_Checkout/js/sidebar'], function (Constr) {
46+
sidebar = new Constr;
47+
done();
48+
});
49+
});
50+
51+
describe('Check remove mini-cart item callback.', function () {
52+
beforeEach(function () {
53+
spyOn(jQuery.fn, 'trigger');
54+
spyOn(mocks['Magento_Customer/js/customer-data'], 'get').and.returnValue(cart);
55+
});
56+
57+
it('Method "_removeItemAfter" is defined', function () {
58+
expect(sidebar._removeItemAfter).toBeDefined();
59+
});
60+
61+
it('Cart item is exists', function () {
62+
var elem = $('<input>').data('cart-item', 5);
63+
64+
sidebar._removeItemAfter(elem);
65+
expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('cart');
66+
expect(jQuery('body').trigger).toHaveBeenCalledWith('ajax:removeFromCart', 'simple');
67+
});
68+
69+
it('Cart item doesn\'t exists', function () {
70+
var elem = $('<input>').data('cart-item', 100);
71+
72+
sidebar._removeItemAfter(elem);
73+
expect(jQuery('body').trigger).not.toHaveBeenCalled();
74+
});
75+
});
76+
77+
describe('Check update item quantity callback.', function () {
78+
beforeEach(function () {
79+
spyOn(jQuery.fn, 'trigger');
80+
spyOn(mocks['Magento_Customer/js/customer-data'], 'get').and.returnValue(cart);
81+
});
82+
83+
it('Method "_updateItemQtyAfter" is defined', function () {
84+
expect(sidebar._updateItemQtyAfter).toBeDefined();
85+
});
86+
87+
it('Cart item is exists', function () {
88+
var elem = $('<input>').data('cart-item', 5);
89+
90+
spyOn(sidebar, '_hideItemButton');
91+
92+
sidebar._updateItemQtyAfter(elem);
93+
expect(mocks['Magento_Customer/js/customer-data'].get).toHaveBeenCalledWith('cart');
94+
expect(jQuery('body').trigger).toHaveBeenCalledWith('ajax:updateCartItemQty', 'simple');
95+
expect(sidebar._hideItemButton).toHaveBeenCalledWith(elem);
96+
});
97+
98+
it('Cart item doesn\'t exists', function () {
99+
var elem = $('<input>').data('cart-item', 100);
100+
101+
spyOn(sidebar, '_hideItemButton');
102+
103+
sidebar._updateItemQtyAfter(elem);
104+
expect(jQuery('body').trigger).not.toHaveBeenCalled();
105+
expect(sidebar._hideItemButton).toHaveBeenCalledWith(elem);
106+
});
107+
});
108+
});
109+
});

0 commit comments

Comments
 (0)