Skip to content

Commit 4f71645

Browse files
author
Oleksii Korshenko
committed
MAGETWO-58035: [Backport] - Configurable product options not saved when editing - for 2.0
2 parents 52c5861 + cf98306 commit 4f71645

File tree

15 files changed

+229
-31
lines changed

15 files changed

+229
-31
lines changed

app/code/Magento/Checkout/CustomerData/DefaultItem.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ protected function doGetItemData()
7070
'item_id' => $this->item->getId(),
7171
'configure_url' => $this->getConfigureUrl(),
7272
'is_visible_in_site_visibility' => $this->item->getProduct()->isVisibleInSiteVisibility(),
73+
'product_id' => $this->item->getProduct()->getId(),
7374
'product_name' => $this->item->getProduct()->getName(),
7475
'product_url' => $this->getProductUrl(),
7576
'product_has_url' => $this->hasProductUrl(),

app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,9 +818,15 @@ public function getSelectedAttributesInfo($product)
818818
$value = $value->getSource()->getOptionText($attributeValue);
819819
} else {
820820
$value = '';
821+
$attributeValue = '';
821822
}
822823

823-
$attributes[] = ['label' => $label, 'value' => $value];
824+
$attributes[] = [
825+
'label' => $label,
826+
'value' => $value,
827+
'option_id' => $attributeId,
828+
'option_value' => $attributeValue,
829+
];
824830
}
825831
}
826832
}

app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/Type/ConfigurableTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,14 @@ public function testGetSelectedAttributesInfo()
564564

565565
$this->assertEquals(
566566
$this->_model->getSelectedAttributesInfo($productMock),
567-
[['label' => 'attr_store_label', 'value' => '']]
567+
[
568+
[
569+
'label' => 'attr_store_label',
570+
'value' => '',
571+
'option_id' => 1,
572+
'option_value' => ''
573+
]
574+
]
568575
);
569576
}
570577

app/code/Magento/ConfigurableProduct/view/frontend/layout/checkout_cart_configure_type_configurable.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
*/
77
-->
88
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
9+
<head>
10+
<link src="Magento_ConfigurableProduct::js/configurable-customer-data.js"/>
11+
</head>
912
<update handle="catalog_product_view_type_configurable"/>
1013
<body/>
1114
</page>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright © 2016 Magento. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
require([
7+
'jquery',
8+
'Magento_ConfigurableProduct/js/options-updater'
9+
], function ($, Updater) {
10+
'use strict';
11+
12+
var selectors = {
13+
formSelector: '#product_addtocart_form'
14+
},
15+
configurableWidgetName = 'mageConfigurable',
16+
widgetInitEvent = 'configurable.initialized',
17+
18+
/**
19+
* Sets all configurable attribute's selected values
20+
*/
21+
updateConfigurableOptions = function () {
22+
var configurableWidget = $(selectors.formSelector).data(configurableWidgetName);
23+
24+
if (!configurableWidget) {
25+
return;
26+
}
27+
configurableWidget.options.values = this.productOptions || {};
28+
configurableWidget._configureForValues();
29+
},
30+
updater = new Updater(widgetInitEvent, updateConfigurableOptions);
31+
32+
updater.listen();
33+
});

app/code/Magento/ConfigurableProduct/view/frontend/web/js/configurable.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ define([
5353

5454
// Setup/configure values to inputs
5555
this._configureForValues();
56+
57+
$(this.element).trigger('configurable.initialized');
5658
},
5759

5860
/**
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Copyright © 2016 Magento. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'jquery',
8+
'Magento_Customer/js/customer-data'
9+
], function ($, customerData) {
10+
'use strict';
11+
12+
var selectors = {
13+
formSelector: '#product_addtocart_form',
14+
productIdSelector: '#product_addtocart_form [name="product"]'
15+
},
16+
cartData = customerData.get('cart'),
17+
productId = $(selectors.productIdSelector).val(),
18+
19+
/**
20+
* set productOptions according to cart data from customer-data
21+
*
22+
* @param {Object} data - cart data from customer-data
23+
* @returns {Boolean} - whether the new options differ from previous
24+
*/
25+
setProductOptions = function (data) {
26+
var changedProductOptions;
27+
28+
if (!(data && data.items && data.items.length && productId)) {
29+
return false;
30+
}
31+
changedProductOptions = data.items.find(function (item) {
32+
return item['product_id'] === productId;
33+
});
34+
changedProductOptions = changedProductOptions && changedProductOptions.options &&
35+
changedProductOptions.options.reduce(function (obj, val) {
36+
obj[val['option_id']] = val['option_value'];
37+
38+
return obj;
39+
}, {});
40+
41+
if (JSON.stringify(this.productOptions || {}) === JSON.stringify(changedProductOptions || {})) {
42+
return false;
43+
}
44+
45+
this.productOptions = changedProductOptions;
46+
47+
return true;
48+
},
49+
50+
/**
51+
* Listens to update of cart data or options initialization and update selected option according to customer data
52+
*
53+
*/
54+
listen = function () {
55+
cartData.subscribe(function (updateCartData) {
56+
if (this.setProductOptions(updateCartData)) {
57+
this.updateOptions();
58+
}
59+
}.bind(this));
60+
$(selectors.formSelector).on(this.eventName, function () {
61+
this.setProductOptions(cartData());
62+
this.updateOptions();
63+
}.bind(this));
64+
},
65+
66+
/**
67+
* Updater constructor function
68+
*
69+
*/
70+
Updater = function (eventName, updateOptionsCallback) {
71+
if (this instanceof Updater) {
72+
this.eventName = eventName;
73+
this.updateOptions = updateOptionsCallback;
74+
this.productOptions = {};
75+
}
76+
};
77+
78+
Updater.prototype.setProductOptions = setProductOptions;
79+
Updater.prototype.listen = listen;
80+
81+
return Updater;
82+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © 2016 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
9+
<head>
10+
<link src="Magento_Swatches::js/configurable-customer-data.js"/>
11+
</head>
12+
<update handle="catalog_product_view_type_configurable"/>
13+
<body/>
14+
</page>

app/code/Magento/Swatches/view/frontend/requirejs-config.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

app/code/Magento/Swatches/view/frontend/templates/product/layered/renderer.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
</div>
7070

7171
<script>
72-
require(["jquery", "jquery/ui", "swatchRenderer"], function ($) {
72+
require(["jquery", "jquery/ui", "Magento_Swatches/js/swatchRenderer"], function ($) {
7373
$('.swatch-layered.<?php /* @escapeNotVerified */ echo $swatchData['attribute_code'] ?>')
7474
.find('[option-type="1"], [option-type="2"], [option-type="0"], [option-type="3"]')
7575
.SwatchRendererTooltip();

0 commit comments

Comments
 (0)