-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshopify-cart-drawer.js.liquid
113 lines (93 loc) · 2.93 KB
/
shopify-cart-drawer.js.liquid
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
function openCartDrawer() {
document.querySelector(".cart-drawer").classList.add("cart-drawer--active");
}
function closeCartDrawer() {
document
.querySelector(".cart-drawer")
.classList.remove("cart-drawer--active");
}
function updateCartItemCounts(count) {
document.querySelectorAll(".cart-count").forEach((el) => {
el.textContent = count;
});
}
async function updateCartDrawer() {
const res = await fetch("/?section_id=cart-drawer");
const text = await res.text();
const html = document.createElement("div");
html.innerHTML = text;
const newBox = html.querySelector(".cart-drawer").innerHTML;
document.querySelector(".cart-drawer").innerHTML = newBox;
addCartDrawerListeners();
}
function addCartDrawerListeners() {
// Update quantities
document
.querySelectorAll(".cart-drawer-quantity-selector button")
.forEach((button) => {
button.addEventListener("click", async () => {
// Get line item key
const rootItem =
button.parentElement.parentElement.parentElement.parentElement
.parentElement;
const key = rootItem.getAttribute("data-line-item-key");
// Get new quantity
const currentQuantity = Number(
button.parentElement.querySelector("input").value
);
const isUp = button.classList.contains(
"cart-drawer-quantity-selector-plus"
);
const newQuantity = isUp ? currentQuantity + 1 : currentQuantity - 1;
// Ajax update\
const res = await fetch("/cart/update.js", {
method: "post",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ updates: { [key]: newQuantity } }),
});
const cart = await res.json();
updateCartItemCounts(cart.item_count);
// Update cart
updateCartDrawer();
});
});
document.querySelector(".cart-drawer-box").addEventListener("click", (e) => {
e.stopPropagation();
});
document
.querySelectorAll(".cart-drawer-header-right-close, .cart-drawer")
.forEach((el) => {
el.addEventListener("click", () => {
console.log("closing drawer");
closeCartDrawer();
});
});
}
addCartDrawerListeners();
document.querySelectorAll('form[action="/cart/add"]').forEach((form) => {
form.addEventListener("submit", async (e) => {
e.preventDefault();
// Submit form with ajax
await fetch("/cart/add", {
method: "post",
body: new FormData(form),
});
// Get cart count
const res = await fetch("/cart.js");
const cart = await res.json();
updateCartItemCounts(cart.item_count);
// Update cart
await updateCartDrawer();
// Open cart drawer
openCartDrawer();
});
});
document.querySelectorAll('a[href="/cart"]').forEach((a) => {
a.addEventListener("click", (e) => {
e.preventDefault();
openCartDrawer();
});
});