@magic-spells/cart-panel 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/cart-panel.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import './cart-panel.scss';
2
- import { CartItem } from '@magic-spells/cart-item';
2
+
3
3
  import '@magic-spells/focus-trap';
4
4
  import EventEmitter from '@magic-spells/event-emitter';
5
+ import { CartItem } from '@magic-spells/cart-item';
5
6
 
6
7
  /**
7
8
  * Custom element that creates an accessible modal cart dialog with focus management
@@ -277,7 +278,6 @@ class CartDialog extends HTMLElement {
277
278
  this.#currentCart = updatedCart;
278
279
  this.#renderCartItems(updatedCart);
279
280
  this.#renderCartPanel(updatedCart);
280
- element.setState('ready');
281
281
 
282
282
  // Emit cart updated and data changed events
283
283
  const cartWithCalculatedFields = this.#addCalculatedFields(updatedCart);
@@ -322,7 +322,7 @@ class CartDialog extends HTMLElement {
322
322
  if (!cartData) return;
323
323
 
324
324
  // Calculate subtotal from all items except those marked to ignore pricing
325
- const pricedItems = cartData.items.filter(item => {
325
+ const pricedItems = cartData.items.filter((item) => {
326
326
  const ignorePrice = item.properties?._ignore_price_in_subtotal;
327
327
  return !ignorePrice;
328
328
  });
@@ -477,6 +477,26 @@ class CartDialog extends HTMLElement {
477
477
  });
478
478
  }
479
479
 
480
+ /**
481
+ * Update existing cart-item elements with fresh cart data
482
+ * @private
483
+ */
484
+ #updateItemsInDOM(itemsContainer, cartData) {
485
+ const visibleItems = this.#getVisibleCartItems(cartData);
486
+ const existingItems = Array.from(itemsContainer.querySelectorAll('cart-item'));
487
+
488
+ existingItems.forEach((cartItemEl) => {
489
+ const key = cartItemEl.getAttribute('key');
490
+ const updatedItemData = visibleItems.find((item) => (item.key || item.id) === key);
491
+
492
+ if (updatedItemData) {
493
+ // Update cart-item with fresh data and full cart context
494
+ // The cart-item will handle HTML comparison and only re-render if needed
495
+ cartItemEl.setData(updatedItemData, cartData);
496
+ }
497
+ });
498
+ }
499
+
480
500
  /**
481
501
  * Add new items to DOM with animation delay
482
502
  * @private
@@ -535,9 +555,16 @@ class CartDialog extends HTMLElement {
535
555
  #addCalculatedFields(cartData) {
536
556
  if (!cartData) return cartData;
537
557
 
558
+ // For display counts: use visible items (excludes _hide_in_cart)
538
559
  const visibleItems = this.#getVisibleCartItems(cartData);
539
560
  const calculated_count = visibleItems.reduce((total, item) => total + item.quantity, 0);
540
- const calculated_subtotal = visibleItems.reduce(
561
+
562
+ // For pricing: use all items except those marked to ignore pricing
563
+ const pricedItems = cartData.items.filter((item) => {
564
+ const ignorePrice = item.properties?._ignore_price_in_subtotal;
565
+ return !ignorePrice;
566
+ });
567
+ const calculated_subtotal = pricedItems.reduce(
541
568
  (total, item) => total + (item.line_price || 0),
542
569
  0
543
570
  );
@@ -578,8 +605,6 @@ class CartDialog extends HTMLElement {
578
605
  // Create cart-item elements without animation
579
606
  visibleItems.forEach((itemData) => {
580
607
  const cartItem = new CartItem(itemData); // No animation
581
- // const cartItem = document.createElement('cart-item');
582
- // cartItem.setData(itemData);
583
608
  itemsContainer.appendChild(cartItem);
584
609
  });
585
610
 
@@ -599,7 +624,10 @@ class CartDialog extends HTMLElement {
599
624
  // Step 1: Remove items that are no longer in cart data
600
625
  this.#removeItemsFromDOM(itemsContainer, newKeysSet);
601
626
 
602
- // Step 2: Add new items that weren't in DOM (with animation delay)
627
+ // Step 2: Update existing items with fresh data (handles templates, bundles, etc.)
628
+ this.#updateItemsInDOM(itemsContainer, cartData);
629
+
630
+ // Step 3: Add new items that weren't in DOM (with animation delay)
603
631
  const itemsToAdd = visibleItems.filter(
604
632
  (itemData) => !currentKeys.has(itemData.key || itemData.id)
605
633
  );