-
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathadd_to_cart.js
More file actions
210 lines (179 loc) · 5.95 KB
/
add_to_cart.js
File metadata and controls
210 lines (179 loc) · 5.95 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { dispatchCustomEvent } from './helper/dispatch_custom_event';
document.addEventListener('DOMContentLoaded', () => {
/**
* Use CSS for fade-out (is much more performant than JS)
* @param element HTML element to fade out
* @param messageTimeout time after which the effect starts
*/
function fadeOut (element, messageTimeout) {
const elementToFadeOut = element;
const transitionTime = 200;
elementToFadeOut.style.transition = `opacity ${transitionTime}ms ease`;
// the fade out
window.setTimeout(() => {
elementToFadeOut.style.opacity = 0;
}, messageTimeout);
// reset element after fade out
window.setTimeout(
() => {
elementToFadeOut.style.transition = 'unset';
elementToFadeOut.style.display = 'none';
elementToFadeOut.style.opacity = 1;
},
messageTimeout + transitionTime
);
dispatchCustomEvent(
'extcode:hide-message-block',
{
elementToFadeOut
}
);
}
function renderAddToCartResultMessage (form, data) {
let messageTimeout = parseInt(
form.querySelector('[data-ajax-message-timeout]').dataset.ajaxMessageTimeout,
10
);
if (!messageTimeout) {
messageTimeout = 3000;
}
const response = JSON.parse(data);
if (response.status === '200') {
const successContainer = form.querySelector('[data-ajax-success-block]');
const successElement = form.querySelector('[data-ajax-success-message]');
successElement.innerHTML = response.messageBody;
successContainer.style.display = null;
fadeOut(successContainer, messageTimeout);
dispatchCustomEvent(
'extcode:render-add-to-cart-result-message',
{
response,
success: true,
element: successElement
}
);
} else {
const errorContainer = form.querySelector('[data-ajax-error-block]');
const errorElement = form.querySelector('[data-ajax-error-message]');
errorElement.innerHTML = response.messageBody;
errorContainer.style.display = null;
fadeOut(errorContainer, messageTimeout);
dispatchCustomEvent(
'extcode:render-add-to-cart-result-message',
{
response,
success: false,
element: errorElement
}
);
}
}
function updateMiniCart (form, data) {
const response = JSON.parse(data);
if (response.status !== '200') {
return;
}
const { count } = response;
const { net } = response;
const { gross } = response;
const miniCarts = document.querySelectorAll('.cart-preview');
miniCarts.forEach((miniCart) => {
const countElement = miniCart.querySelector('.cart-preview-count');
const netElement = miniCart.querySelector('.net');
const grossElement = miniCart.querySelector('.gross');
const linkElement = miniCart.querySelector('.checkout-link');
if (countElement) {
countElement.innerHTML = count;
}
if (netElement) {
netElement.innerHTML = net;
}
if (grossElement) {
grossElement.innerHTML = gross;
}
if (linkElement) {
if (count > 0) {
linkElement.style.display = 'block';
} else {
linkElement.style.display = 'none';
}
}
});
dispatchCustomEvent(
'extcode:minicart-was-updated',
{
count: response.count,
net: response.net,
gross: response.gross
}
);
document.querySelectorAll('form').forEach((formElement) => {
formElement.reset();
});
}
const addToCartForms = document.querySelectorAll('[data-ajax=\'1\']');
addToCartForms.forEach((addToCartForm) => {
addToCartForm.addEventListener('submit', function addProductToCart (event) {
event.preventDefault();
const data = this;
const actionUrl = data.getAttribute('action');
fetch(actionUrl, {
method: data.getAttribute('method'),
body: new FormData(data)
})
.then((response) => response.text())
.then((response) => {
renderAddToCartResultMessage(data, response);
updateMiniCart(data, response);
});
});
});
/**
* The following code is only used when your cart setup uses forms from EXT:form to add additional
* information by the customer to the product.
*/
const addToCartButton = document.querySelector('[data-add-to-cart="form"]');
const formContainer = document.querySelector('[data-add-to-cart="result"]');
function fetchAndInsertFormContent (actionUrl) {
fetch(actionUrl)
.then((response) => response.text())
.then((response) => {
formContainer.innerHTML = response;
});
const forms = document.querySelectorAll('[data-add-to-cart-uri]');
forms.forEach((form) => {
form.addEventListener('submit', function addProductToCart (event) {
event.preventDefault();
const newActionUrl = form.getAttribute('data-add-to-cart-uri');
const submitButton = form.querySelector('button[type="submit"]');
const newData = this;
const formData = new FormData(newData);
formData.append(
submitButton.getAttribute('name'),
submitButton.getAttribute('value')
);
fetch(newActionUrl, {
method: 'POST',
body: formData
})
.then((response) => response.text())
.then((response) => {
renderAddToCartResultMessage(newData, response);
updateMiniCart(newData, response);
});
});
});
}
function disableDefaultAddToCartButFetchFormInstead () {
addToCartButton.addEventListener('click', function showForm (event) {
event.preventDefault();
const actionUrl = this.getAttribute('href');
fetchAndInsertFormContent(actionUrl);
});
}
// Do only execute code if those two html elements are given.
if (!addToCartButton || !formContainer) {
return;
}
disableDefaultAddToCartButFetchFormInstead();
});