Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit bf3e439e authored by 65160270's avatar 65160270
Browse files

update-cart

parent a3092bff
Branches
No related tags found
No related merge requests found
...@@ -32,6 +32,7 @@ router.get('/', async (req, res) => { ...@@ -32,6 +32,7 @@ router.get('/', async (req, res) => {
} }
}); });
// เพิ่มสินค้าลงตะกร้า
// เพิ่มสินค้าลงตะกร้า // เพิ่มสินค้าลงตะกร้า
router.post('/add', async (req, res) => { router.post('/add', async (req, res) => {
const { productId, quantity } = req.body; const { productId, quantity } = req.body;
...@@ -50,8 +51,8 @@ router.post('/add', async (req, res) => { ...@@ -50,8 +51,8 @@ router.post('/add', async (req, res) => {
// ตรวจสอบว่าสินค้านี้มีอยู่ในตะกร้าหรือยัง // ตรวจสอบว่าสินค้านี้มีอยู่ในตะกร้าหรือยัง
const [[existingItem]] = await pool.query( const [[existingItem]] = await pool.query(
'SELECT id, quantity FROM cart_items WHERE user_id = ? AND product_id = ?', 'SELECT id, quantity FROM cart_items WHERE session_id = ? AND product_id = ?',
[req.session.user.id, productId] // ใช้ user_id แทน session_id [req.session.user.id, productId]
); );
if (existingItem) { if (existingItem) {
...@@ -67,8 +68,8 @@ router.post('/add', async (req, res) => { ...@@ -67,8 +68,8 @@ router.post('/add', async (req, res) => {
} else { } else {
// เพิ่มสินค้าใหม่ // เพิ่มสินค้าใหม่
await pool.query( await pool.query(
'INSERT INTO cart_items (user_id, product_id, quantity) VALUES (?, ?, ?)', 'INSERT INTO cart_items (session_id, product_id, quantity) VALUES (?, ?, ?)',
[req.session.user.id, productId, parseInt(quantity)] // เปลี่ยน session_id → user_id [req.session.id, productId, parseInt(quantity)]
); );
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment