Skip to content

Commit 84e680e

Browse files
committed
Fix cart icon and drawer refresh after Standard Actions updates
1 parent 258f00f commit 84e680e

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

‎assets/cart.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ class CartItems extends window.StandardEvents.createViewEventElement(HTMLElement
115115
.then((response) => response.text())
116116
.then((responseText) => {
117117
const html = new DOMParser().parseFromString(responseText, 'text/html');
118+
const drawerWrapper = document.querySelector('cart-drawer');
119+
const sourceDrawer = html.querySelector('cart-drawer');
120+
const drawerInner = drawerWrapper?.querySelector('.drawer__inner');
121+
const wasEmpty = drawerWrapper?.classList.contains('is-empty');
118122
const selectors = ['cart-drawer-items', '.cart-drawer__footer'];
119123
for (const selector of selectors) {
120124
const targetElement = document.querySelector(selector);
@@ -123,6 +127,20 @@ class CartItems extends window.StandardEvents.createViewEventElement(HTMLElement
123127
targetElement.replaceWith(sourceElement);
124128
}
125129
}
130+
if (drawerWrapper && sourceDrawer && drawerInner) {
131+
const isEmpty = sourceDrawer.classList.contains('is-empty');
132+
drawerWrapper.classList.toggle('is-empty', isEmpty);
133+
const checkoutButton = document.getElementById('CartDrawer-Checkout');
134+
if (checkoutButton) checkoutButton.disabled = isEmpty;
135+
const sourceEmpty = sourceDrawer.querySelector('.drawer__inner-empty');
136+
const liveEmpty = drawerInner.querySelector('.drawer__inner-empty');
137+
if (liveEmpty && !sourceEmpty) liveEmpty.remove();
138+
if (!liveEmpty && sourceEmpty) drawerInner.prepend(sourceEmpty);
139+
if (drawerWrapper.classList.contains('active') && wasEmpty !== isEmpty) {
140+
const container = isEmpty ? drawerInner.querySelector('.drawer__inner-empty') : drawerInner;
141+
if (container) trapFocus(container, container.querySelector('.drawer__close') || container);
142+
}
143+
}
126144
})
127145
.catch((e) => {
128146
console.error(e);

‎assets/standard-actions-override.js‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function collectCartSections() {
7979
// that isn't already refreshed by Dawn's own pubsub subscribers, then
8080
// publish 'cart-update' so the subscribers run.
8181
//
82-
// We always fetch /cart.js (with sections= when we have any) so that
82+
// We always fetch /cart.js so that
8383
// `cartData` is defined for subscribers. quick-add-bulk.js reads
8484
// `event.cartData.items` unconditionally — publishing without cartData
8585
// makes it throw.
@@ -90,14 +90,17 @@ async function refreshDawnCartUI() {
9090
: '';
9191
// `routes` is a Dawn global, but don't assume it's defined.
9292
const cartUrl = (typeof routes !== 'undefined' && routes?.cart_url) || '/cart';
93-
const url = `${cartUrl}.js${sectionsQuery}`;
94-
const cartData = await fetch(url, { headers: { Accept: 'application/json' } })
93+
const cartData = await fetch(`${cartUrl}.js`, { headers: { Accept: 'application/json' } })
9594
.then((r) => (r.ok ? r.json() : null))
9695
.catch(() => null);
9796

98-
if (cartData?.sections) {
97+
if (sections.size) {
98+
const sectionsData = await fetch(`${cartUrl}${sectionsQuery}`, { headers: { Accept: 'application/json' } })
99+
.then((r) => (r.ok ? r.json() : null))
100+
.catch(() => null);
101+
99102
for (const [id, { mount, extractSelector }] of sections) {
100-
const html = cartData.sections[id];
103+
const html = sectionsData?.[id];
101104
if (!html) continue;
102105
const source = new DOMParser()
103106
.parseFromString(html, 'text/html')
@@ -107,7 +110,7 @@ async function refreshDawnCartUI() {
107110
}
108111

109112
// Hand off to Dawn's existing subscribers. cartData is the full
110-
// Cart Ajax payload (items, item_count, token, …) plus sections;
113+
// Cart Ajax payload (items, item_count, token, …);
111114
// quick-add-bulk.js and price-per-item.js read it directly.
112115
publish(PUB_SUB_EVENTS.cartUpdate, {
113116
source: 'external-refresh',

0 commit comments

Comments
 (0)