-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathupdate_payment_shipping.js
More file actions
42 lines (37 loc) · 1.62 KB
/
Copy pathupdate_payment_shipping.js
File metadata and controls
42 lines (37 loc) · 1.62 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
import { dispatchCustomEvent } from './helper/dispatch_custom_event';
import { createHtmlElementFromString, replaceHtmlElementByIdentifier } from './helper/html_helper';
document.addEventListener('DOMContentLoaded', () => {
// Listen in order form for setting of payment or shipping methods
function setAjaxEventListener (parentId, targetClass) {
// Use parent of parentId because element of parentId itself will be replaced.
// Listening direct for it would mean losing the EventListener.
document.querySelector(parentId).parentElement.addEventListener('click', (event) => {
if (!event.target.classList.contains(targetClass)) {
return;
}
event.preventDefault();
const actionUrl = event.target.getAttribute('href');
fetch(actionUrl, {
method: 'GET'
})
.then((response) => response.text())
.then((response) => {
const responseAsHtml = createHtmlElementFromString(response);
replaceHtmlElementByIdentifier(responseAsHtml, '#checkout-step-shipping-method');
replaceHtmlElementByIdentifier(responseAsHtml, '#checkout-step-payment-method');
replaceHtmlElementByIdentifier(responseAsHtml, '#checkout-step-summary');
const eventName = targetClass === 'set-payment'
? 'extcode:set-payment'
: 'extcode:set-shipping';
dispatchCustomEvent(
eventName,
{
response
}
);
});
});
}
setAjaxEventListener('#checkout-step-payment-method', 'set-payment');
setAjaxEventListener('#checkout-step-shipping-method', 'set-shipping');
});