Skip to content

Commit 571a72c

Browse files
author
Ryan Simmons
committed
Fixes issue 22112 (#22112) by removing shipping address validation from the new billing address form in checkout. It appears this was added initially simply to validate the postcode in the form, but running the billing address through the actual shipping rate functionality in shipping-rates-validator's validateFields function was causing the issues leading to the address disappearing from the Ship To section & being deselected in the Shipping step. Instead, this validation is replaced in the billing address form by a simpler validator which only checks the postcode.
1 parent ede25ae commit 571a72c

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'jquery',
8+
'Magento_Checkout/js/model/postcode-validator',
9+
'mage/translate',
10+
'uiRegistry',
11+
], function (
12+
$,
13+
postcodeValidator,
14+
$t,
15+
uiRegistry,
16+
) {
17+
'use strict';
18+
19+
var postcodeElementName = 'postcode';
20+
21+
22+
return {
23+
validateZipCodeTimeout: 0,
24+
validateDelay: 2000,
25+
26+
27+
/**
28+
* Perform postponed binding for fieldset elements
29+
*
30+
* @param {String} formPath
31+
*/
32+
initFields: function (formPath) {
33+
var self = this;
34+
uiRegistry.async(formPath + '.' + postcodeElementName)(self.bindHandler.bind(self));
35+
},
36+
37+
/**
38+
* @param {Object} element
39+
* @param {Number} delay
40+
*/
41+
bindHandler: function (element, delay) {
42+
var self = this;
43+
44+
delay = typeof delay === 'undefined' ? self.validateDelay : delay;
45+
46+
element.on('value', function () {
47+
clearTimeout(self.validateZipCodeTimeout);
48+
self.validateZipCodeTimeout = setTimeout(function () {
49+
self.postcodeValidation(element);
50+
}, delay);
51+
});
52+
},
53+
54+
/**
55+
* @param {Object} postcodeElement
56+
* @return {*}
57+
*/
58+
postcodeValidation: function (postcodeElement) {
59+
var countryId = $('select[name="country_id"]:visible').val(),
60+
validationResult,
61+
warnMessage;
62+
63+
if (postcodeElement == null || postcodeElement.value() == null) {
64+
return true;
65+
}
66+
67+
postcodeElement.warn(null);
68+
validationResult = postcodeValidator.validate(postcodeElement.value(), countryId);
69+
70+
if (!validationResult) {
71+
warnMessage = $t('Provided Zip/Postal Code seems to be invalid.');
72+
73+
if (postcodeValidator.validatedPostCodeExample.length) {
74+
warnMessage += $t(' Example: ') + postcodeValidator.validatedPostCodeExample.join('; ') + '. ';
75+
}
76+
warnMessage += $t('If you believe it is the right one you can ignore this notice.');
77+
postcodeElement.warn(warnMessage);
78+
}
79+
80+
return validationResult;
81+
}
82+
};
83+
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ define([
1818
'Magento_Checkout/js/action/set-billing-address',
1919
'Magento_Ui/js/model/messageList',
2020
'mage/translate',
21-
'Magento_Checkout/js/model/shipping-rates-validator'
21+
'Magento_Checkout/js/model/billing-address-postcode-validator'
2222
],
2323
function (
2424
ko,
@@ -35,7 +35,7 @@ function (
3535
setBillingAddressAction,
3636
globalMessageList,
3737
$t,
38-
shippingRatesValidator
38+
billingAddressPostcodeValidator
3939
) {
4040
'use strict';
4141

@@ -66,7 +66,7 @@ function (
6666
quote.paymentMethod.subscribe(function () {
6767
checkoutDataResolver.resolveBillingAddress();
6868
}, this);
69-
shippingRatesValidator.initFields(this.get('name') + '.form-fields');
69+
billingAddressPostcodeValidator.initFields(this.get('name') + '.form-fields');
7070
},
7171

7272
/**

0 commit comments

Comments
 (0)