MCPcopy
hub / github.com/continuedev/continue / addToCart

Function addToCart

manual-testing-sandbox/next-edit/next-edit-5-3.ts:56–96  ·  view source on GitHub ↗
(productId: number, quantity: number = 1)

Source from the content-addressed store, hash-verified

54
55// Add product to cart
56function addToCart(productId: number, quantity: number = 1) {
57 // Check if product exists
58 if (!PRODUCTS[productId]) {
59 console.error("Product not found!");
60 return false;
61 }
62
63 // Check if enough stock
64 if (PRODUCTS[productId].stock < quantity) {
65 console.error("Not enough stock!");
66 return false;
67 }
68
69 // Check if product already in cart
70 let existingItem = false;
71 for (let i = 0; i < cartItems.length; i++) {
72 if (cartItems[i].productId === productId) {
73 // Update quantity
74 cartItems[i].quantity += quantity;
75 existingItem = true;
76 break;
77 }
78 }
79
80 // Add new item if not already in cart
81 if (!existingItem) {
82 cartItems.push({
83 productId,
84 quantity,
85 price: PRODUCTS[productId].price,
86 });
87 }
88
89 // Update stock
90 PRODUCTS[productId].stock -= quantity;
91
92 // Recalculate cart total
93 calculateCartTotal();
94
95 return true;
96}
97
98// Remove product from cart
99function removeFromCart(productId: number, quantity: number = 1) {

Callers 1

next-edit-5-3.tsFile · 0.85

Calls 3

calculateCartTotalFunction · 0.85
errorMethod · 0.80
pushMethod · 0.65

Tested by

no test coverage detected