Skip to content

Commit cd813e4

Browse files
author
Olga Nakonechna
committed
MAGETWO-52974: CLONE - Configurable product options not saved when editing
1 parent 71f878f commit cd813e4

File tree

3 files changed

+100
-109
lines changed

3 files changed

+100
-109
lines changed
Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,28 @@
11
require([
22
'jquery',
3-
'Magento_Customer/js/customer-data'
4-
], function ($, customerData) {
3+
'Magento_ConfigurableProduct/js/options-updater'
4+
], function ($, Updater) {
55
'use strict';
66

77
var selectors = {
8-
configurableWidget: 'mageConfigurable',
9-
formSelector: '#product_addtocart_form',
10-
productIdSelector: '#product_addtocart_form [name="product"]'
11-
},
12-
configurableWidget,
13-
productOptions,
14-
changedProductOptions,
15-
cartData = customerData.get('cart'),
16-
productId = $(selectors.productIdSelector).val(),
17-
updateConfigurableOptions,
18-
setProductOptions;
8+
formSelector: '#product_addtocart_form'
9+
},
10+
configurableWidgetName = 'mageConfigurable',
11+
widgetInitEvent = 'configurable.initialized',
1912

2013
/**
2114
* Sets all configurable attribute's selected values
2215
*/
2316
updateConfigurableOptions = function () {
24-
configurableWidget = $(selectors.formSelector).data(selectors.configurableWidget);
17+
var configurableWidget = $(selectors.formSelector).data(configurableWidgetName);
2518

2619
if (!configurableWidget) {
2720
return;
2821
}
29-
configurableWidget.options.values = productOptions || {};
22+
configurableWidget.options.values = this.productOptions || {};
3023
configurableWidget._configureForValues();
31-
};
32-
33-
/**
34-
* set productOptions according to cart data from customer-data
35-
*
36-
* @param {Object} data - cart data from customer-data
37-
* @returns {Boolean} - whether the new options differ from previous
38-
*/
39-
setProductOptions = function (data) {
40-
if (!(data && data.items && data.items.length && productId)) {
41-
return false;
42-
}
43-
changedProductOptions = data.items.find(function (item) {
44-
return item['product_id'] === productId;
45-
});
46-
changedProductOptions = changedProductOptions && changedProductOptions.options &&
47-
changedProductOptions.options.reduce(function (obj, val) {
48-
obj[val['option_id']] = val['option_value'];
49-
50-
return obj;
51-
}, {});
52-
53-
if (JSON.stringify(productOptions || {}) === JSON.stringify(changedProductOptions || {})) {
54-
return false;
55-
}
56-
57-
productOptions = changedProductOptions;
58-
59-
return true;
60-
};
24+
},
25+
updater = new Updater(widgetInitEvent, updateConfigurableOptions);
6126

62-
cartData.subscribe(function (updateCartData) {
63-
if (setProductOptions(updateCartData)) {
64-
updateConfigurableOptions();
65-
}
66-
});
67-
$(selectors.formSelector).on('configurable.initialized', function () {
68-
setProductOptions(cartData());
69-
updateConfigurableOptions();
70-
});
27+
updater.listen();
7128
});
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
define([
2+
'jquery',
3+
'Magento_Customer/js/customer-data'
4+
], function ($, customerData) {
5+
'use strict';
6+
7+
var selectors = {
8+
formSelector: '#product_addtocart_form',
9+
productIdSelector: '#product_addtocart_form [name="product"]'
10+
},
11+
cartData = customerData.get('cart'),
12+
productId = $(selectors.productIdSelector).val(),
13+
14+
/**
15+
* set productOptions according to cart data from customer-data
16+
*
17+
* @param {Object} data - cart data from customer-data
18+
* @returns {Boolean} - whether the new options differ from previous
19+
*/
20+
setProductOptions = function (data) {
21+
var changedProductOptions;
22+
23+
if (!(data && data.items && data.items.length && productId)) {
24+
return false;
25+
}
26+
changedProductOptions = data.items.find(function (item) {
27+
return item['product_id'] === productId;
28+
});
29+
changedProductOptions = changedProductOptions && changedProductOptions.options &&
30+
changedProductOptions.options.reduce(function (obj, val) {
31+
obj[val['option_id']] = val['option_value'];
32+
33+
return obj;
34+
}, {});
35+
36+
if (JSON.stringify(this.productOptions || {}) === JSON.stringify(changedProductOptions || {})) {
37+
return false;
38+
}
39+
40+
this.productOptions = changedProductOptions;
41+
42+
return true;
43+
},
44+
45+
/**
46+
* Listens to update of cart data or options initialization and update selected option according to customer data
47+
*
48+
*/
49+
listen = function () {
50+
cartData.subscribe(function (updateCartData) {
51+
if (this.setProductOptions(updateCartData)) {
52+
this.updateOptions();
53+
}
54+
}.bind(this));
55+
$(selectors.formSelector).on(this.eventName, function () {
56+
this.setProductOptions(cartData());
57+
this.updateOptions();
58+
}.bind(this));
59+
},
60+
61+
/**
62+
* Updater constructor function
63+
*
64+
*/
65+
Updater = function (eventName, updateOptionsCallback) {
66+
if (this instanceof Updater) {
67+
this.eventName = eventName;
68+
this.updateOptions = updateOptionsCallback;
69+
this.productOptions = {};
70+
}
71+
};
72+
73+
Updater.prototype.setProductOptions = setProductOptions;
74+
Updater.prototype.listen = listen;
75+
76+
return Updater;
77+
});
Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,28 @@
11
require([
22
'jquery',
3-
'Magento_Customer/js/customer-data'
4-
], function ($, customerData) {
3+
'Magento_ConfigurableProduct/js/options-updater'
4+
], function ($, Updater) {
55
'use strict';
66

77
var selectors = {
8-
swatchWidget: 'mageSwatchRenderer',
9-
formSelector: '#product_addtocart_form',
10-
swatchSelector: '.swatch-opt',
11-
productIdSelector: '#product_addtocart_form [name="product"]'
12-
},
13-
swatchWidget,
14-
productOptions,
15-
changedProductOptions,
16-
cartData = customerData.get('cart'),
17-
productId = $(selectors.productIdSelector).val(),
18-
updateSwatchOptions,
19-
setProductOptions;
8+
formSelector: '#product_addtocart_form',
9+
swatchSelector: '.swatch-opt'
10+
},
11+
swatchWidgetName = 'mageSwatchRenderer',
12+
widgetInitEvent = 'swatch.initialized',
2013

2114
/**
2215
* Sets all configurable swatch attribute's selected values
2316
*/
2417
updateSwatchOptions = function () {
25-
swatchWidget = $(selectors.swatchSelector).data(selectors.swatchWidget);
18+
var swatchWidget = $(selectors.swatchSelector).data(swatchWidgetName);
2619

2720
if (!swatchWidget || !swatchWidget._EmulateSelectedByAttributeId) {
2821
return;
2922
}
30-
swatchWidget._EmulateSelectedByAttributeId(productOptions);
31-
};
32-
33-
/**
34-
* set productOptions according to cart data from customer-data
35-
*
36-
* @param {Object} data - cart data from customer-data
37-
* @returns {Boolean} - whether the new options differ from previous
38-
*/
39-
setProductOptions = function (data) {
40-
if (!(data && data.items && data.items.length && productId)) {
41-
return false;
42-
}
43-
changedProductOptions = data.items.find(function (item) {
44-
return item['product_id'] === productId;
45-
});
46-
changedProductOptions = changedProductOptions && changedProductOptions.options &&
47-
changedProductOptions.options.reduce(function (obj, val) {
48-
obj[val['option_id']] = val['option_value'];
49-
50-
return obj;
51-
}, {});
52-
53-
if (JSON.stringify(productOptions || {}) === JSON.stringify(changedProductOptions || {})) {
54-
return false;
55-
}
56-
57-
productOptions = changedProductOptions;
58-
59-
return true;
60-
};
23+
swatchWidget._EmulateSelectedByAttributeId(this.productOptions);
24+
},
25+
updater = new Updater(widgetInitEvent, updateSwatchOptions);
6126

62-
cartData.subscribe(function (updateCartData) {
63-
if (setProductOptions(updateCartData)) {
64-
updateSwatchOptions();
65-
}
66-
});
67-
$(selectors.formSelector).on('swatch.initialized', function () {
68-
setProductOptions(cartData());
69-
updateSwatchOptions();
70-
});
27+
updater.listen();
7128
});

0 commit comments

Comments
 (0)