Skip to content

Commit 0f1cca0

Browse files
author
Ievgen Shakhsuvarov
committed
MAGETWO-37495: Wrong behaviour for save new shipping addres during checkout
1 parent 3c66c19 commit 0f1cca0

File tree

5 files changed

+62
-62
lines changed

5 files changed

+62
-62
lines changed

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,7 @@ define(['jquery'], function($) {
2222
return address;
2323
},
2424
getAddresses: function() {
25-
if (addresses.indexOf(this.newAddress) == -1) {
26-
this.add(this.newAddress);
27-
}
28-
return addresses;
29-
},
30-
newAddress: {
31-
getAddressInline: function() {
32-
return 'New Address';
33-
},
34-
customerAddressId: null
25+
return addresses.slice(0);
3526
}
3627
};
3728
});

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

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33
* See COPYING.txt for license details.
44
*/
55
/*jshint browser:true*/
6-
/*global define,alert*/
6+
/*global define*/
77
define(
88
[
99
"jquery",
1010
'Magento_Ui/js/form/form',
1111
'ko',
1212
'Magento_Customer/js/model/customer',
1313
'../action/select-billing-address',
14-
'Magento_Checkout/js/model/step-navigator',
14+
'../model/step-navigator',
1515
'../model/quote',
1616
'../model/addresslist',
17-
'../action/check-email-availability',
18-
'mage/validation'
17+
'mage/translate'
1918
],
20-
function ($, Component, ko, customer, selectBillingAddress, navigator, quote, addressList, checkEmailAvailability) {
19+
function ($, Component, ko, customer, selectBillingAddress, navigator, quote, addressList, $t) {
2120
"use strict";
2221
var stepName = 'billingAddress';
2322
var newAddressSelected = ko.observable(false);
@@ -35,12 +34,23 @@ define(
3534
return navigator.getStepClassAttributes(stepName);
3635
},
3736
stepNumber: navigator.getStepNumber(stepName),
38-
billingAddresses: customer.getBillingAddressList(),
39-
selectedBillingAddressId: addressList.getAddresses()[0].customerAddressId,
37+
billingAddresses: function() {
38+
var newAddress = {
39+
getAddressInline: function() {
40+
return $t('New address');
41+
},
42+
customerAddressId: null
43+
},
44+
addresses = addressList.getAddresses();
45+
addresses.push(newAddress);
46+
return addresses;
47+
},
48+
selectedBillingAddressId: ko.observable(
49+
addressList.getAddresses().length ? addressList.getAddresses()[0].customerAddressId : null
50+
),
4051
isVisible: navigator.isStepVisible(stepName),
4152
useForShipping: "1",
4253
quoteIsVirtual: quote.isVirtual(),
43-
isEmailCheckComplete: $.Deferred(),
4454
billingAddressesOptionsText: function(item) {
4555
return item.getAddressInline();
4656
},
@@ -53,42 +63,32 @@ define(
5363
return additionalData;
5464
},
5565
submitBillingAddress: function() {
56-
if (this.selectedBillingAddressId) {
57-
var additionalData = this.checkUseForShipping(this.useForShipping);
66+
var additionalData = this.checkUseForShipping(this.useForShipping);
67+
if (this.selectedBillingAddressId()) {
5868
selectBillingAddress(
59-
addressList.getAddressById(this.selectedBillingAddressId),
69+
addressList.getAddressById(this.selectedBillingAddressId()),
6070
this.useForShipping,
6171
additionalData
6272
);
6373
} else {
64-
var that = this;
6574
this.validate();
66-
$.when(this.isEmailCheckComplete).done( function() {
67-
if (!that.source.get('params.invalid')) {
68-
var addressData = that.source.get('billingAddress');
69-
var additionalData = that.checkUseForShipping(that.useForShipping);
70-
/**
71-
* All the the input fields that are not a part of the address but need to be submitted
72-
* in the same request must have data-scope attribute set
73-
*/
74-
var additionalFields = $('input[data-scope="additionalAddressData"]').serializeArray();
75-
additionalFields.forEach(function (field) {
76-
additionalData[field.name] = field.value;
77-
});
78-
if (quote.getCheckoutMethod()() && !customer.isLoggedIn()()) {
79-
addressData.email = that.source.get('customerDetails.email');
80-
}
81-
if($(billingFormSelector).validation() && $(billingFormSelector).validation('isValid')) {
82-
selectBillingAddress(addressData, that.useForShipping, additionalData);
83-
}
75+
if (!this.source.get('params.invalid')) {
76+
var addressData = this.source.get('billingAddress');
77+
/**
78+
* All the the input fields that are not a part of the address (e. g. CAPTCHA) but need to be
79+
* submitted in the same request must have data-scope attribute set
80+
*/
81+
var additionalFields = $('input[data-scope="additionalAddressData"]').serializeArray();
82+
additionalFields.forEach(function (field) {
83+
additionalData[field.name] = field.value;
84+
});
85+
if (quote.getCheckoutMethod()() && !customer.isLoggedIn()()) {
86+
addressData.email = this.source.get('customerDetails.email');
87+
}
88+
if($(billingFormSelector).validation() && $(billingFormSelector).validation('isValid')) {
89+
selectBillingAddress(addressData, this.useForShipping, additionalData);
8490
}
85-
}).fail( function() {
86-
alert(
87-
"There is already a registered customer using this email address. " +
88-
"Please log in using this email address or enter a different email address " +
89-
"to register your account."
90-
);
91-
});
91+
}
9292
}
9393
},
9494
navigateToCurrentStep: function() {
@@ -103,11 +103,7 @@ define(
103103
return newAddressSelected();
104104
},
105105
onAddressChange: function (value) {
106-
if (value === null) {
107-
newAddressSelected(true);
108-
} else {
109-
newAddressSelected(false);
110-
}
106+
value() === null ? newAddressSelected(true) : newAddressSelected(false);
111107
},
112108
validate: function() {
113109
var fields = $(billingFormSelector).find('input, select');
@@ -116,7 +112,6 @@ define(
116112
fields.trigger('change');
117113
this.source.trigger('billingAddress.data.validate');
118114
this.validateAdditionalAddressFields();
119-
this.isEmailCheckComplete.resolve();
120115
},
121116
validateAdditionalAddressFields: function() {
122117
$(billingFormSelector).validation();

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ define(
1212
'Magento_Customer/js/model/customer',
1313
'../model/quote',
1414
'Magento_Checkout/js/model/step-navigator',
15-
'../model/addresslist'
15+
'../model/addresslist',
16+
'mage/translate'
1617
],
1718
function($, Component, ko, selectShippingAddress, customer, quote, navigator, addressList) {
1819
'use strict';
@@ -21,14 +22,27 @@ define(
2122
return Component.extend({
2223
defaults: {
2324
template: 'Magento_Checkout/shipping-address',
24-
visible: true
25+
visible: true,
26+
formVisible: customer.getShippingAddressList().length === 0
2527
},
2628
stepClassAttributes: function() {
2729
return navigator.getStepClassAttributes(stepName);
2830
},
2931
stepNumber: navigator.getStepNumber(stepName),
30-
addresses: customer.getShippingAddressList(),
31-
selectedAddressId: ko.observable(addressList.getAddresses()[0].customerAddressId),
32+
addresses: function() {
33+
var newAddress = {
34+
getAddressInline: function() {
35+
return $.mage.__('New address');
36+
},
37+
customerAddressId: null
38+
},
39+
addresses = addressList.getAddresses();
40+
addresses.push(newAddress);
41+
return addresses;
42+
},
43+
selectedAddressId: ko.observable(
44+
addressList.getAddresses().length ? addressList.getAddresses()[0].customerAddressId : null
45+
),
3246
sameAsBilling: ko.observable(null),
3347
quoteHasBillingAddress: quote.getBillingAddress(),
3448
isVisible: navigator.isStepVisible(stepName),
@@ -94,10 +108,10 @@ define(
94108
},
95109
onAddressChange: function() {
96110
var billingAddress = quote.getBillingAddress();
97-
if (this.selectedAddressId() != billingAddress().customerAddressId) {
111+
if (this.selectedAddressId() !== billingAddress().customerAddressId) {
98112
this.sameAsBilling(false);
99113
}
100-
if (this.selectedAddressId() == null) {
114+
if (this.selectedAddressId() === null) {
101115
newAddressSelected(true);
102116
} else {
103117
newAddressSelected(false);

app/code/Magento/Checkout/view/frontend/web/template/billing-address.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ <h2 data-bind="text: $t('Billing Information')"></h2>
1616
<label class="label" for="billing:address-select"><span data-bind="text: $t('Select a billing address from your address book or enter a new address.')"></span></label>
1717
<div class="control">
1818
<select name="billing_address_id" id="billing:address-select" data-bind="
19-
options: billingAddresses,
19+
options: billingAddresses(),
2020
optionsText: billingAddressesOptionsText,
2121
optionsValue: 'customerAddressId',
2222
value: selectedBillingAddressId,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ <h2 data-bind="text: $t('Shipping Information')"></h2>
2222
<label class="label" for="shipping:address-select"><span data-bind="text: $t('Select a shipping address from your address book or enter a new address.')"></span></label>
2323
<div class="control">
2424
<select name="shipping_address_id" id="shipping:address-select" data-bind="
25-
options: addresses,
25+
options: addresses(),
2626
optionsText: function(item) { return item.getAddressInline(); },
2727
optionsValue: 'customerAddressId',
2828
value: selectedAddressId,
@@ -33,7 +33,7 @@ <h2 data-bind="text: $t('Shipping Information')"></h2>
3333
</div>
3434
<!-- /ko -->
3535
<fieldset id="shipping-new-address-form" class="fieldset address"
36-
data-bind="fadeVisible: isNewAddressSelected(), visible: visible">
36+
data-bind="fadeVisible: isNewAddressSelected(), visible: formVisible">
3737
<!-- ko foreach: getRegion('additional-fieldsets') -->
3838
<!-- ko template: getTemplate() --><!-- /ko -->
3939
<!--/ko-->

0 commit comments

Comments
 (0)