Skip to content

Commit b948612

Browse files
feat(cart-notifications): ✨ Add support for multiple cart lines in a cart notification.
1 parent 5cd1a85 commit b948612

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

assets/cart-notification.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,18 @@ class CartNotification extends HTMLElement {
3535
}
3636

3737
renderContents(parsedState) {
38-
this.cartItemKey = parsedState.key;
38+
const keys = parsedState.items ? parsedState.items.map(({ key }) => key) : [parsedState.key];
3939
this.getSectionsToRender().forEach((section) => {
40-
document.getElementById(section.id).innerHTML = this.getSectionInnerHTML(
41-
parsedState.sections[section.id],
42-
section.selector
43-
);
40+
if (typeof section.selector === 'function') {
41+
document.getElementById(section.id).innerHTML = keys
42+
.map((key) => this.getSectionOuterHTML(parsedState.sections[section.id], section?.selector(key)))
43+
.join('');
44+
} else {
45+
document.getElementById(section.id).innerHTML = this.getSectionInnerHTML(
46+
parsedState.sections[section.id],
47+
section.selector?.(keys[0])
48+
);
49+
}
4450
});
4551

4652
if (this.header) this.header.reveal();
@@ -51,7 +57,7 @@ class CartNotification extends HTMLElement {
5157
return [
5258
{
5359
id: 'cart-notification-product',
54-
selector: `[id="cart-notification-product-${this.cartItemKey}"]`,
60+
selector: (key) => `[id="cart-notification-product-${key}"]`,
5561
},
5662
{
5763
id: 'cart-notification-button',

assets/component-cart-notification.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,15 @@
9898
.cart-notification-product {
9999
align-items: flex-start;
100100
display: flex;
101+
flex-wrap: wrap;
101102
padding-bottom: 3rem;
102103
padding-top: 2rem;
103104
}
104105

106+
.cart-notification-product .cart-item {
107+
display: flex;
108+
}
109+
105110
.cart-notification-product dl {
106111
margin-bottom: 0;
107112
margin-top: 0;

assets/product-form.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ if (!customElements.get('product-form')) {
4848
if (response.status) {
4949
publish(PUB_SUB_EVENTS.cartError, {
5050
source: 'product-form',
51-
productVariantId: formData.get('id'),
51+
productVariantId: formData.get('id') ?? formData.get('items[0][id]'),
5252
errors: response.errors || response.description,
5353
message: response.message,
5454
});
@@ -70,7 +70,7 @@ if (!customElements.get('product-form')) {
7070
if (!this.error)
7171
publish(PUB_SUB_EVENTS.cartUpdate, {
7272
source: 'product-form',
73-
productVariantId: formData.get('id'),
73+
productVariantId: formData.get('id') ?? formData.get('items[0][id]'),
7474
cartData: response,
7575
}).then(() => {
7676
CartPerformance.measureFromMarker('add:wait-for-subscribers', startMarker);
@@ -82,7 +82,7 @@ if (!customElements.get('product-form')) {
8282
'modalClosed',
8383
() => {
8484
setTimeout(() => {
85-
CartPerformance.measure("add:paint-updated-sections", () => {
85+
CartPerformance.measure('add:paint-updated-sections', () => {
8686
this.cart.renderContents(response);
8787
});
8888
});
@@ -91,7 +91,7 @@ if (!customElements.get('product-form')) {
9191
);
9292
quickAddModal.hide(true);
9393
} else {
94-
CartPerformance.measure("add:paint-updated-sections", () => {
94+
CartPerformance.measure('add:paint-updated-sections', () => {
9595
this.cart.renderContents(response);
9696
});
9797
}
@@ -105,7 +105,7 @@ if (!customElements.get('product-form')) {
105105
if (!this.error) this.submitButton.removeAttribute('aria-disabled');
106106
this.querySelector('.loading__spinner').classList.add('hidden');
107107

108-
CartPerformance.measureFromEvent("add:user-action", evt);
108+
CartPerformance.measureFromEvent('add:user-action', evt);
109109
});
110110
}
111111

0 commit comments

Comments
 (0)