-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAGETWO-58035: [Backport] - Configurable product options not saved wh…
…en editing - for 2.0
- Loading branch information
Showing
15 changed files
with
229 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app/code/Magento/ConfigurableProduct/view/frontend/web/js/configurable-customer-data.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
require([ | ||
'jquery', | ||
'Magento_ConfigurableProduct/js/options-updater' | ||
], function ($, Updater) { | ||
'use strict'; | ||
|
||
var selectors = { | ||
formSelector: '#product_addtocart_form' | ||
}, | ||
configurableWidgetName = 'mageConfigurable', | ||
widgetInitEvent = 'configurable.initialized', | ||
|
||
/** | ||
* Sets all configurable attribute's selected values | ||
*/ | ||
updateConfigurableOptions = function () { | ||
var configurableWidget = $(selectors.formSelector).data(configurableWidgetName); | ||
|
||
if (!configurableWidget) { | ||
return; | ||
} | ||
configurableWidget.options.values = this.productOptions || {}; | ||
configurableWidget._configureForValues(); | ||
}, | ||
updater = new Updater(widgetInitEvent, updateConfigurableOptions); | ||
|
||
updater.listen(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
app/code/Magento/ConfigurableProduct/view/frontend/web/js/options-updater.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
define([ | ||
'jquery', | ||
'Magento_Customer/js/customer-data' | ||
], function ($, customerData) { | ||
'use strict'; | ||
|
||
var selectors = { | ||
formSelector: '#product_addtocart_form', | ||
productIdSelector: '#product_addtocart_form [name="product"]' | ||
}, | ||
cartData = customerData.get('cart'), | ||
productId = $(selectors.productIdSelector).val(), | ||
|
||
/** | ||
* set productOptions according to cart data from customer-data | ||
* | ||
* @param {Object} data - cart data from customer-data | ||
* @returns {Boolean} - whether the new options differ from previous | ||
*/ | ||
setProductOptions = function (data) { | ||
var changedProductOptions; | ||
|
||
if (!(data && data.items && data.items.length && productId)) { | ||
return false; | ||
} | ||
changedProductOptions = data.items.find(function (item) { | ||
return item['product_id'] === productId; | ||
}); | ||
changedProductOptions = changedProductOptions && changedProductOptions.options && | ||
changedProductOptions.options.reduce(function (obj, val) { | ||
obj[val['option_id']] = val['option_value']; | ||
|
||
return obj; | ||
}, {}); | ||
|
||
if (JSON.stringify(this.productOptions || {}) === JSON.stringify(changedProductOptions || {})) { | ||
return false; | ||
} | ||
|
||
this.productOptions = changedProductOptions; | ||
|
||
return true; | ||
}, | ||
|
||
/** | ||
* Listens to update of cart data or options initialization and update selected option according to customer data | ||
* | ||
*/ | ||
listen = function () { | ||
cartData.subscribe(function (updateCartData) { | ||
if (this.setProductOptions(updateCartData)) { | ||
this.updateOptions(); | ||
} | ||
}.bind(this)); | ||
$(selectors.formSelector).on(this.eventName, function () { | ||
this.setProductOptions(cartData()); | ||
this.updateOptions(); | ||
}.bind(this)); | ||
}, | ||
|
||
/** | ||
* Updater constructor function | ||
* | ||
*/ | ||
Updater = function (eventName, updateOptionsCallback) { | ||
if (this instanceof Updater) { | ||
this.eventName = eventName; | ||
this.updateOptions = updateOptionsCallback; | ||
this.productOptions = {}; | ||
} | ||
}; | ||
|
||
Updater.prototype.setProductOptions = setProductOptions; | ||
Updater.prototype.listen = listen; | ||
|
||
return Updater; | ||
}); |
14 changes: 14 additions & 0 deletions
14
app/code/Magento/Swatches/view/frontend/layout/checkout_cart_configure_type_configurable.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> | ||
<head> | ||
<link src="Magento_Swatches::js/configurable-customer-data.js"/> | ||
</head> | ||
<update handle="catalog_product_view_type_configurable"/> | ||
<body/> | ||
</page> |
12 changes: 0 additions & 12 deletions
12
app/code/Magento/Swatches/view/frontend/requirejs-config.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app/code/Magento/Swatches/view/frontend/web/js/configurable-customer-data.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
require([ | ||
'jquery', | ||
'Magento_ConfigurableProduct/js/options-updater' | ||
], function ($, Updater) { | ||
'use strict'; | ||
|
||
var selectors = { | ||
formSelector: '#product_addtocart_form', | ||
swatchSelector: '.swatch-opt' | ||
}, | ||
swatchWidgetName = 'customSwatchRenderer', | ||
widgetInitEvent = 'swatch.initialized', | ||
|
||
/** | ||
* Sets all configurable swatch attribute's selected values | ||
*/ | ||
updateSwatchOptions = function () { | ||
var swatchWidget = $(selectors.swatchSelector).data(swatchWidgetName); | ||
|
||
if (!swatchWidget || !swatchWidget._EmulateSelectedByAttributeId) { | ||
return; | ||
} | ||
swatchWidget._EmulateSelectedByAttributeId(this.productOptions); | ||
}, | ||
updater = new Updater(widgetInitEvent, updateSwatchOptions); | ||
|
||
updater.listen(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters