feat: add detailed logging to add to cart functionality for debugging
Added comprehensive console logging to track: - Cart eligibility checks - API request URL and payload - Response status and data - Error details This will help identify why items aren't being added to cart. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -354,28 +354,50 @@ document.addEventListener('alpine:init', () => {
|
||||
|
||||
// Add to cart
|
||||
async addToCart() {
|
||||
if (!this.canAddToCart) return;
|
||||
if (!this.canAddToCart) {
|
||||
console.warn('[SHOP] Cannot add to cart:', {
|
||||
canAddToCart: this.canAddToCart,
|
||||
isActive: this.product?.is_active,
|
||||
inventory: this.product?.available_inventory,
|
||||
quantity: this.quantity
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
this.addingToCart = true;
|
||||
|
||||
try {
|
||||
console.log('[SHOP] Adding to cart:', { productId: this.productId, quantity: this.quantity });
|
||||
const url = `/api/v1/shop/cart/${this.sessionId}/items`;
|
||||
const payload = {
|
||||
product_id: parseInt(this.productId),
|
||||
quantity: this.quantity
|
||||
};
|
||||
|
||||
const response = await fetch(
|
||||
`/api/v1/shop/cart/${this.sessionId}/items`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
product_id: parseInt(this.productId),
|
||||
quantity: this.quantity
|
||||
})
|
||||
}
|
||||
);
|
||||
console.log('[SHOP] Adding to cart:', {
|
||||
url,
|
||||
sessionId: this.sessionId,
|
||||
productId: this.productId,
|
||||
quantity: this.quantity,
|
||||
payload
|
||||
});
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
console.log('[SHOP] Add to cart response:', {
|
||||
status: response.status,
|
||||
ok: response.ok
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const result = await response.json();
|
||||
console.log('[SHOP] Add to cart success:', result);
|
||||
|
||||
this.cartCount += this.quantity;
|
||||
this.showToast(
|
||||
`${this.quantity} item(s) added to cart!`,
|
||||
@@ -386,10 +408,11 @@ document.addEventListener('alpine:init', () => {
|
||||
this.quantity = this.product?.min_quantity || 1;
|
||||
} else {
|
||||
const error = await response.json();
|
||||
console.error('[SHOP] Add to cart error response:', error);
|
||||
throw new Error(error.detail || 'Failed to add to cart');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[SHOP] Add to cart error:', error);
|
||||
console.error('[SHOP] Add to cart exception:', error);
|
||||
this.showToast(error.message || 'Failed to add to cart', 'error');
|
||||
} finally {
|
||||
this.addingToCart = false;
|
||||
|
||||
Reference in New Issue
Block a user