-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathupdate_country.js
More file actions
104 lines (88 loc) · 3.92 KB
/
Copy pathupdate_country.js
File metadata and controls
104 lines (88 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { dispatchCustomEvent } from './helper/dispatch_custom_event';
import { createHtmlElementFromString, replaceHtmlElementByIdentifier } from './helper/html_helper';
document.addEventListener('DOMContentLoaded', () => {
const shippingSameAsBillingElement = document.querySelector('#shipping-same-as-billing');
const billingCountryElement = document.querySelector('#billingAddress-country');
const shippingCountryElement = document.querySelector('#shippingAddress-country');
function setDisabledStatus (parentElement, fieldType, disabledStatus) {
parentElement.querySelectorAll(fieldType).forEach(
(field) => {
if (field.dataset.disableShipping) {
const inputField = field;
inputField.disabled = disabledStatus;
}
}
);
}
/**
* Listen to changes of cart form field for billing country.
*/
billingCountryElement.addEventListener('change', () => {
const billingCountry = billingCountryElement.value;
let shippingCountry = '';
if (!shippingSameAsBillingElement.checked) {
shippingCountry = shippingCountryElement.value;
}
updateCountry(billingCountry, shippingCountry);
});
/**
* Listen to changes of cart form field for shipping country.
*/
shippingCountryElement.addEventListener('change', () => {
const billingCountry = billingCountryElement.value;
const shippingCountry = shippingCountryElement.value;
updateCountry(billingCountry, shippingCountry);
});
/**
* Listen to changes of cart form field whether shipping address is same as billing address.
*/
shippingSameAsBillingElement.addEventListener('change', function () {
const stepShippingAddressElement = document.querySelector('#checkout-step-shipping-address');
if (shippingSameAsBillingElement.checked) {
stepShippingAddressElement.style.display = 'none';
} else {
stepShippingAddressElement.style.display = null;
}
const billingCountry = billingCountryElement.value;
// Shipping costs shall depend on billing country if shipping address == billing address.
// Due to this the value of the shipping country is only considered if the shipping address
// differs from the billing address.
const shippingCountry = shippingSameAsBillingElement.checked
? billingCountryElement.value
: shippingCountryElement.value;
// Disable shipping fields if shipping address == billing address.
const disabledStatus = shippingSameAsBillingElement.checked;
setDisabledStatus(stepShippingAddressElement, 'input', disabledStatus);
setDisabledStatus(stepShippingAddressElement, 'select', disabledStatus);
updateCountry(billingCountry, shippingCountry);
});
/**
* Collect data from form fields,
* make an AJAX request and replace elements of the form with the incoming response.
*/
function updateCountry (billingCountry, shippingCountry) {
const formData = new FormData();
formData.append('tx_cart_cart[shipping_same_as_billing]', shippingSameAsBillingElement.checked);
formData.append('tx_cart_cart[billing_country]', billingCountry);
formData.append('tx_cart_cart[shipping_country]', shippingCountry);
const actionUrl = document.querySelector('[data-ajax-update-country-action-url]').dataset.ajaxUpdateCountryActionUrl;
fetch(actionUrl, {
method: 'POST',
body: formData
})
.then((response) => response.text())
.then((response) => {
const responseAsHtml = createHtmlElementFromString(response);
replaceHtmlElementByIdentifier(responseAsHtml, '#form-cart');
replaceHtmlElementByIdentifier(responseAsHtml, '#checkout-step-shipping-method');
replaceHtmlElementByIdentifier(responseAsHtml, '#checkout-step-payment-method');
replaceHtmlElementByIdentifier(responseAsHtml, '#checkout-step-summary');
dispatchCustomEvent(
'extcode:country-updated',
{
response
}
);
});
}
});