From bf3e439e7b77cb9439024c646e604bcc9426a4a2 Mon Sep 17 00:00:00 2001 From: 65160270 <65160270@go.buu.ac.th> Date: Fri, 28 Mar 2025 19:52:00 +0700 Subject: [PATCH] update-cart --- shop-routes/cart.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/shop-routes/cart.js b/shop-routes/cart.js index f0ab7fe..2fe64c9 100644 --- a/shop-routes/cart.js +++ b/shop-routes/cart.js @@ -32,6 +32,7 @@ router.get('/', async (req, res) => { } }); +// เพิ่มสินค้าลงตะกร้า // เพิ่มสินค้าลงตะกร้า router.post('/add', async (req, res) => { const { productId, quantity } = req.body; @@ -50,8 +51,8 @@ router.post('/add', async (req, res) => { // ตรวจสอบว่าสินค้านี้มีอยู่ในตะกร้าหรือยัง const [[existingItem]] = await pool.query( - 'SELECT id, quantity FROM cart_items WHERE user_id = ? AND product_id = ?', - [req.session.user.id, productId] // ใช้ user_id แทน session_id + 'SELECT id, quantity FROM cart_items WHERE session_id = ? AND product_id = ?', + [req.session.user.id, productId] ); if (existingItem) { @@ -67,8 +68,8 @@ router.post('/add', async (req, res) => { } else { // เพิ่มสินค้าใหม่ await pool.query( - 'INSERT INTO cart_items (user_id, product_id, quantity) VALUES (?, ?, ?)', - [req.session.user.id, productId, parseInt(quantity)] // เปลี่ยน session_id → user_id + 'INSERT INTO cart_items (session_id, product_id, quantity) VALUES (?, ?, ?)', + [req.session.id, productId, parseInt(quantity)] ); } -- GitLab