addToCart method

void addToCart(
  1. PersistentShoppingCartItem item
)

Adds a PersistentShoppingCartItem to the shopping cart.

If the product is already in the cart, it updates the quantity. If not, it adds a new instance.

Implementation

void addToCart(PersistentShoppingCartItem item) {
  // Retrieve the existing item using the product ID as the key
  PersistentShoppingCartItem? existingItem = _cartBox.get(item.productId);
  if (existingItem != null) {
    // Product is already in the cart, update the quantity
    existingItem.quantity = item.quantity; // Resetting the quantity:)
    _cartBox.put(
      existingItem.key,
      PersistentShoppingCartItem(
        productId: existingItem.productId,
        productDescription: existingItem.productDescription,
        quantity: 1,
        productName: existingItem.productName,
        productDetails: existingItem.productDetails,
        productImages: existingItem.productImages,
        unitPrice: existingItem.unitPrice,
        productThumbnail: existingItem.productThumbnail,
      ),
    );
  } else {
    // Product is not in the cart, add a new instance
    _cartBox.put(
        item.key,
        PersistentShoppingCartItem(
          productId: item.productId,
          quantity: 1,
          productName: item.productName,
          unitPrice: item.unitPrice,
          productDescription: item.productDescription,
          productDetails: item.productDetails,
          productImages: item.productImages,
          productThumbnail: item.productThumbnail,
        ));
  }
}