Why does my `updateCartUIWithNewData` function fail to update cart items correctly in vanilla JavaScript?

I’m working on an e-commerce website and trying to update the shopping cart UI when the cart data changes. I have a function updateCartUIWithNewData that is supposed to handle this, but it doesn’t work as expected.

Here is the non-working version of my updateCartUIWithNewData function. The goal of this function is to update the shopping cart UI based on the provided cart data.

function updateCartUIWithNewData(cart) {
  console.log("Updating cart UI with cart data:", cart);
  
  // Clear the existing cart UI
  itemContainer.innerHTML = '';

  // Re-render the cart items
  cart.items.forEach(item => {
    console.log("Processing cart item:", item); // Log each item in the cart
    const existingCartItem = findExistingCartItem(item.product.toString());
    updateCartUI(existingCartItem, item.product, item.price, item.quantity);
  });

  // Update the cart count
  updateTheCartCount();
}

Expected Behavior: The function should fetch the details for each product in the cart and update the UI accordingly.

Actual Behavior: The function runs without errors, but the UI doesn’t update as expected. The product details like name, price, and size are missing in the cart.

Additional Context:

  • itemContainer is a reference to the container element holding the cart items.
  • findExistingCartItem is a function that finds a cart item by product ID.
  • updateCartUI is a function that updates the cart item UI.
  • updateTheCartCount is a function that updates the cart item count displayed to the user.

Here are simplified versions of these functions for reference:

function findExistingCartItem(productId) {
  return itemContainer.querySelector(`.item[data-product-id="${productId}"]`);
}

function updateCartUI(existingCartItem, productToAdd, updatedPrice, quantity) {
  const templateElement = getTemplateElement();
  if (!templateElement) return;

  const templateContent = getTemplateContent(templateElement);
  if (!templateContent) return;

  if (existingCartItem) {
    updateExistingCartItem(existingCartItem, updatedPrice);
  } else {
    addNewItemToCart(templateContent, productToAdd, updatedPrice, quantity);
  }

  updateTheCartCount();
}

I’ve tried adding console logs to trace the execution and verify that the function is called with the correct data. However, the UI still doesn’t update as expected. I suspect that the issue might be related to how I’m fetching product details

I’ve uploaded the full project to GitHub for better context. You can find it here:
https://github.com/datogoco/Namo

https://github.com/datogoco/Namo

To reproduce the issue:

  1. Clone the repository.
  2. Follow the setup instructions in the README.
  3. Create a config.env file based on the config.env.example file and fill in the required values.
  4. Try to add an item to the cart and observe the console logs and UI behavior.

Could someone help me understand why the updateCartUIWithNewData function isn’t updating the cart UI correctly and suggest a solution to fix this issue? Any guidance on properly handling asynchronous operations in this context would be greatly appreciated.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật