Skip to content

Commit 5186a84

Browse files
author
Ievgen Shakhsuvarov
committed
MAGETWO-37495: Wrong behaviour for save new shipping addres during checkout
1 parent 28cf93d commit 5186a84

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

app/code/Magento/Checkout/view/frontend/web/js/action/select-billing-address.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,31 @@ define(
1313
'uiRegistry',
1414
'../model/url-builder',
1515
'mage/storage',
16-
'../model/payment-service'
17-
16+
'../model/payment-service',
17+
'underscore'
1818
],
19-
function (quote, addressList, navigator, selectShippingAddress, registry, urlBuilder, storage, paymentService) {
19+
function (quote, addressList, navigator, selectShippingAddress, registry, urlBuilder, storage, paymentService, _) {
2020
"use strict";
2121
var actionCallback;
2222
var result = function (billingAddress, useForShipping, additionalData) {
23+
var copyBillingToShipping = function() {
24+
var shippingAddressSource = registry.get('checkoutProvider'),
25+
shippingAddress = shippingAddressSource.get('shippingAddress');
26+
for (var property in billingAddress) {
27+
if (billingAddress.hasOwnProperty(property) && shippingAddress.hasOwnProperty(property)) {
28+
if (typeof billingAddress[property] === 'string') {
29+
shippingAddressSource.set('shippingAddress.' + property, billingAddress[property]);
30+
} else {
31+
shippingAddressSource.set('shippingAddress.' + property, _.clone(billingAddress[property]));
32+
}
33+
}
34+
}
35+
};
2336
additionalData = additionalData || {};
2437
quote.setBillingAddress(billingAddress);
2538
if (useForShipping() === '1' && !quote.isVirtual()) {
2639
if (!billingAddress.customerAddressId) {
27-
// update shipping address data in corresponding provider
28-
var shippingAddressSource = registry.get('checkoutProvider');
29-
var shippingAddress = shippingAddressSource.get('shippingAddress');
30-
for (var property in billingAddress) {
31-
if (billingAddress.hasOwnProperty(property)
32-
&& shippingAddress.hasOwnProperty(property)
33-
) {
34-
shippingAddressSource.set('shippingAddress.' + property, billingAddress[property]);
35-
}
36-
}
40+
copyBillingToShipping();
3741
}
3842
selectShippingAddress(billingAddress, useForShipping, additionalData);
3943
} else if (quote.isVirtual()) {
@@ -72,6 +76,9 @@ define(
7276
);
7377
} else {
7478
navigator.setCurrent('billingAddress').goNext();
79+
if (addressList.isBillingSameAsShipping) {
80+
copyBillingToShipping();
81+
}
7582
}
7683
};
7784
result.setActionCallback = function (value) {

app/code/Magento/Checkout/view/frontend/web/js/model/addresslist.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ define(['jquery'], function($) {
2323
},
2424
getAddresses: function() {
2525
return addresses.slice(0);
26-
}
26+
},
27+
isBillingSameAsShipping: false
2728
};
2829
});

app/code/Magento/Checkout/view/frontend/web/js/view/shipping-address.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ define(
1313
'../model/quote',
1414
'Magento_Checkout/js/model/step-navigator',
1515
'../model/addresslist',
16-
'mage/translate'
16+
'underscore'
1717
],
18-
function($, Component, ko, selectShippingAddress, customer, quote, navigator, addressList) {
18+
function($, Component, ko, selectShippingAddress, customer, quote, navigator, addressList, _) {
1919
'use strict';
2020
var stepName = 'shippingAddress';
2121
var newAddressSelected = ko.observable(false);
@@ -87,6 +87,7 @@ define(
8787
}
8888
},
8989
sameAsBillingClick: function() {
90+
addressList.isBillingSameAsShipping = !addressList.isBillingSameAsShipping;
9091
if (this.sameAsBilling()) {
9192
var billingAddress = quote.getBillingAddress()();
9293
if (billingAddress.customerAddressId) {
@@ -97,7 +98,11 @@ define(
9798
var shippingAddress = this.source.get('shippingAddress');
9899
for (var property in billingAddress) {
99100
if (billingAddress.hasOwnProperty(property) && shippingAddress.hasOwnProperty(property)) {
100-
this.source.set('shippingAddress.' + property, billingAddress[property]);
101+
if (typeof billingAddress[property] === 'string') {
102+
this.source.set('shippingAddress.' + property, billingAddress[property]);
103+
} else {
104+
this.source.set('shippingAddress.' + property, _.clone(billingAddress[property]));
105+
}
101106
}
102107
}
103108
this.selectedAddressId(null);

0 commit comments

Comments
 (0)