Skip to content

Commit c2fb117

Browse files
committed
fix: Improve cart stock handling and update mutation
Enhanced cart logic to handle stock quantity updates and prevent adding items beyond available stock. Updated GraphQL mutation to include stockQuantity and stockStatus fields for product variations.
1 parent d0bfab2 commit c2fb117

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

app/composables/useCart.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
1+
import { push } from 'notivue';
2+
13
export const useCart = () => {
24
const cart = useState('cart', () => []);
35
const addToCartButtonStatus = ref('add');
46
const removeFromCartButtonStatus = ref('remove');
5-
const { push } = useNotivue();
6-
const findItemByVariationId = id => cart.value.find(i => i?.variation?.node?.databaseId === id);
7+
const findItem = id => cart.value.find(i => i?.variation?.node?.databaseId === id);
78

89
const handleAddToCart = productId => {
910
addToCartButtonStatus.value = 'loading';
10-
11-
$fetch('/api/cart/add', {
12-
method: 'POST',
13-
body: { productId },
14-
})
11+
$fetch('/api/cart/add', { method: 'POST', body: { productId } })
1512
.then(res => {
16-
updateCart([...cart.value, res.addToCart.cartItem]);
13+
const incoming = res.addToCart.cartItem;
14+
const existing = cart.value.find(i => i.key === incoming.key);
15+
if (existing) {
16+
existing.quantity = incoming.quantity;
17+
if (typeof incoming?.variation?.node?.stockQuantity === 'number') {
18+
existing.variation.node.stockQuantity = incoming.variation.node.stockQuantity;
19+
}
20+
updateCart([...cart.value]);
21+
} else {
22+
updateCart([...cart.value, incoming]);
23+
}
1724
addToCartButtonStatus.value = 'added';
1825
setTimeout(() => (addToCartButtonStatus.value = 'add'), 2000);
1926
})
2027
.catch(err => {
2128
addToCartButtonStatus.value = 'add';
22-
const errorMessage = err.response.errors[0].message
23-
.replace(/<a[^>]*>(.*?)<\/a>/g, '')
24-
.replace(/&mdash;/g, '—')
25-
.trim();
26-
push.error(errorMessage);
29+
push.error('Insufficient stock');
2730
});
2831
};
2932

@@ -45,22 +48,17 @@ export const useCart = () => {
4548

4649
const changeQty = (key, quantity) => {
4750
$fetch('/api/cart/update', { method: 'POST', body: { items: [{ key, quantity }] } });
48-
if (quantity > 0) {
49-
const idx = cart.value.findIndex(i => i.key === key);
50-
cart.value[idx].quantity = quantity;
51-
updateCart([...cart.value]);
52-
} else {
53-
updateCart(cart.value.filter(i => i.key !== key));
54-
}
51+
updateCart(quantity > 0 ? cart.value.map(i => (i.key === key ? { ...i, quantity } : i)) : cart.value.filter(i => i.key !== key));
5552
};
5653

5754
const increment = id => {
58-
const item = findItemByVariationId(id);
59-
item ? changeQty(item.key, item.quantity + 1) : handleAddToCart(id);
55+
const item = findItem(id);
56+
if (item.quantity >= (item?.variation?.node?.stockQuantity ?? Infinity)) return push.error('Insufficient stock');
57+
changeQty(item.key, item.quantity + 1);
6058
};
6159

6260
const decrement = id => {
63-
const item = findItemByVariationId(id);
61+
const item = findItem(id);
6462
if (item) changeQty(item.key, item.quantity - 1);
6563
};
6664

app/gql/mutations/addToCart.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export const addToCartMutation = gql`
2020
databaseId
2121
salePrice(format: RAW)
2222
regularPrice(format: RAW)
23+
stockQuantity
24+
stockStatus
2325
image {
2426
sourceUrl(size: WOOCOMMERCE_THUMBNAIL)
2527
}

0 commit comments

Comments
 (0)