Skip to content

Commit 536a728

Browse files
committed
Fix #24091 - reduce amount of js code needed to set options for configurable product on wishlist
1 parent e3bad69 commit 536a728

File tree

1 file changed

+21
-45
lines changed

1 file changed

+21
-45
lines changed

app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.js

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ define([
1919
qtyInfo: '#qty',
2020
actionElement: '[data-action="add-to-wishlist"]',
2121
productListItem: '.item.product-item',
22-
productListPriceBox: '.price-box',
2322
isProductList: false
2423
},
2524

@@ -68,16 +67,25 @@ define([
6867
_updateWishlistData: function (event) {
6968
var dataToAdd = {},
7069
isFileUploaded = false,
71-
productId = null,
70+
productItem = null,
71+
handleObjSelector = null,
7272
self = this;
7373

7474
if (event.handleObj.selector == this.options.qtyInfo) { //eslint-disable-line eqeqeq
75-
this._updateAddToWishlistButton({});
75+
this._updateAddToWishlistButton({}, productItem);
7676
event.stopPropagation();
7777

7878
return;
7979
}
80-
$(event.handleObj.selector).each(function (index, element) {
80+
81+
if (this.options.isProductList) {
82+
productItem = $(event.target).closest(this.options.productListItem);
83+
handleObjSelector = productItem.find(event.handleObj.selector);
84+
} else {
85+
handleObjSelector = $(event.handleObj.selector);
86+
}
87+
88+
handleObjSelector.each(function (index, element) {
8189
if ($(element).is('input[type=text]') ||
8290
$(element).is('input[type=email]') ||
8391
$(element).is('input[type=number]') ||
@@ -87,19 +95,7 @@ define([
8795
$(element).is('textarea') ||
8896
$('#' + element.id + ' option:selected').length
8997
) {
90-
if (!($(element).data('selector') || $(element).attr('name'))) {
91-
return;
92-
}
93-
94-
if (self.options.isProductList) {
95-
productId = self.retrieveListProductId(this);
96-
97-
dataToAdd[productId] = $.extend(
98-
{},
99-
dataToAdd[productId] ? dataToAdd[productId] : {},
100-
self._getElementData(element)
101-
);
102-
} else {
98+
if ($(element).data('selector') || $(element).attr('name')) {
10399
dataToAdd = $.extend({}, dataToAdd, self._getElementData(element));
104100
}
105101

@@ -114,34 +110,29 @@ define([
114110
if (isFileUploaded) {
115111
this.bindFormSubmit();
116112
}
117-
this._updateAddToWishlistButton(dataToAdd);
113+
this._updateAddToWishlistButton(dataToAdd, productItem);
118114
event.stopPropagation();
119115
},
120116

121117
/**
122118
* @param {Object} dataToAdd
119+
* @param {Object} productItem
123120
* @private
124121
*/
125-
_updateAddToWishlistButton: function (dataToAdd) {
126-
var productId = null,
127-
self = this;
122+
_updateAddToWishlistButton: function (dataToAdd, productItem) {
123+
var self = this,
124+
buttons = productItem ? productItem.find(this.options.actionElement) : $(this.options.actionElement);
128125

129-
$('[data-action="add-to-wishlist"]').each(function (index, element) {
130-
var params = $(element).data('post'),
131-
dataToAddObj = dataToAdd;
132-
133-
if (self.options.isProductList) {
134-
productId = self.retrieveListProductId(element);
135-
dataToAddObj = typeof dataToAdd[productId] !== 'undefined' ? dataToAdd[productId] : {};
136-
}
126+
buttons.each(function (index, element) {
127+
var params = $(element).data('post');
137128

138129
if (!params) {
139130
params = {
140131
'data': {}
141132
};
142133
}
143134

144-
params.data = $.extend({}, params.data, dataToAddObj, {
135+
params.data = $.extend({}, params.data, dataToAdd, {
145136
'qty': $(self.options.qtyInfo).val()
146137
});
147138
$(element).data('post', params);
@@ -264,21 +255,6 @@ define([
264255

265256
return;
266257
}
267-
},
268-
269-
/**
270-
* Retrieve product id from element on products list
271-
*
272-
* @param {jQuery.Object} element
273-
* @private
274-
*/
275-
retrieveListProductId: function (element) {
276-
return parseInt(
277-
$(element).closest(this.options.productListItem)
278-
.find(this.options.productListPriceBox)
279-
.data('product-id'),
280-
10
281-
);
282258
}
283259
});
284260

0 commit comments

Comments
 (0)