fix: resolve cart functionality issues with product name and frontend integration
Fixed two critical issues preventing cart from working:
1. Product Name Attribute Error:
- Product model doesn't have 'name' attribute directly
- Product name comes from marketplace_product.title relationship
- Updated all references in cart_service.py:
* get_cart() - product display in cart
* add_to_cart() - logging and exception messages
* update_cart_item() - exception messages
- Also updated image_url to use marketplace_product.image_link
2. Products Page Not Calling API:
- products.html had stub addToCart() with TODO comment
- Implemented actual API call:
* POST to /api/v1/shop/cart/{sessionId}/items
* Sends product_id and quantity
* Updates cartCount on success
* Shows error toast on failure
* Proper error handling with try/catch
Testing Results:
- API endpoint now works correctly (tested with curl)
- Cart items are properly saved to database
- Product names display correctly from marketplace_product relationship
- Frontend can successfully add items to cart
Related Models:
- Product has relationship to MarketplaceProduct
- Product.marketplace_product.title contains the product name
- Product.marketplace_product.image_link contains the image URL
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -81,11 +81,11 @@ class CartService:
|
||||
|
||||
items.append({
|
||||
"product_id": product.id,
|
||||
"product_name": product.name,
|
||||
"product_name": product.marketplace_product.title,
|
||||
"quantity": cart_item.quantity,
|
||||
"price": cart_item.price_at_add,
|
||||
"line_total": line_total,
|
||||
"image_url": product.image_link if hasattr(product, 'image_link') else None,
|
||||
"image_url": product.marketplace_product.image_link if product.marketplace_product else None,
|
||||
})
|
||||
|
||||
subtotal += line_total
|
||||
@@ -157,10 +157,10 @@ class CartService:
|
||||
raise ProductNotFoundException(product_id=product_id, vendor_id=vendor_id)
|
||||
|
||||
logger.info(
|
||||
f"[CART_SERVICE] Product found: {product.name}",
|
||||
f"[CART_SERVICE] Product found: {product.marketplace_product.title}",
|
||||
extra={
|
||||
"product_id": product_id,
|
||||
"product_name": product.name,
|
||||
"product_name": product.marketplace_product.title,
|
||||
"available_inventory": product.available_inventory
|
||||
}
|
||||
)
|
||||
@@ -195,7 +195,7 @@ class CartService:
|
||||
)
|
||||
raise InsufficientInventoryForCartException(
|
||||
product_id=product_id,
|
||||
product_name=product.name,
|
||||
product_name=product.marketplace_product.title,
|
||||
requested=new_quantity,
|
||||
available=product.available_inventory
|
||||
)
|
||||
@@ -230,7 +230,7 @@ class CartService:
|
||||
)
|
||||
raise InsufficientInventoryForCartException(
|
||||
product_id=product_id,
|
||||
product_name=product.name,
|
||||
product_name=product.marketplace_product.title,
|
||||
requested=quantity,
|
||||
available=product.available_inventory
|
||||
)
|
||||
@@ -319,7 +319,7 @@ class CartService:
|
||||
if product.available_inventory < quantity:
|
||||
raise InsufficientInventoryForCartException(
|
||||
product_id=product_id,
|
||||
product_name=product.name,
|
||||
product_name=product.marketplace_product.title,
|
||||
requested=quantity,
|
||||
available=product.available_inventory
|
||||
)
|
||||
|
||||
@@ -201,10 +201,38 @@ document.addEventListener('alpine:init', () => {
|
||||
this.loadProducts();
|
||||
},
|
||||
|
||||
addToCart(product) {
|
||||
async addToCart(product) {
|
||||
console.log('[SHOP] Adding to cart:', product);
|
||||
this.showToast(`${product.marketplace_product.title} added to cart`, 'success');
|
||||
// TODO: Implement actual cart functionality
|
||||
|
||||
try {
|
||||
const url = `/api/v1/shop/cart/${this.sessionId}/items`;
|
||||
const payload = {
|
||||
product_id: product.id,
|
||||
quantity: 1
|
||||
};
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const result = await response.json();
|
||||
console.log('[SHOP] Add to cart success:', result);
|
||||
this.cartCount += 1;
|
||||
this.showToast(`${product.marketplace_product.title} added to cart`, 'success');
|
||||
} else {
|
||||
const error = await response.json();
|
||||
console.error('[SHOP] Add to cart error:', error);
|
||||
this.showToast(error.message || 'Failed to add to cart', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[SHOP] Add to cart exception:', error);
|
||||
this.showToast('Failed to add to cart', 'error');
|
||||
}
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user