[Note: remember to include site URL, ZC version, list of plugins, PHP version, etc ... read the Posting Tips shown above for information to include in your post here.
And, remove this comment before actually posting!]
I am trying to add a feature where customers can add products not registered in the product database to a quote. I created a quote table with the following columns: id, products_model, quantity, price, subtotal, status, created_at, customers_id, product_name, remark, and quote_number.
When accessing the URL like https://domain.jp/index.php?main_pag...ZXX&quantity=1, the product model XZXX with quantity 1 gets added to the quote list, which shows the product number, quantity, unit price, subtotal, status, and a checkbox for purchasing.
I have written the code to add the selected item to the cart, but after checking the checkbox and clicking the purchase button, it gets registered in the customers_basket table but does not show up in the shopping cart immediately. After logging out and logging back in, the cart is updated with the product. Can anyone explain why the product does not appear in the shopping cart right after purchase?
PHP Code:
// Update session after purchase process
if (isset($_POST['submit_purchase'])) {
foreach ($_POST['purchase'] as $id => $purchase) {
if ($purchase == 'on') {
// Retrieve product information from quotes table for selected items
$product_query = $db->Execute("SELECT products_model, quantity FROM quotes WHERE id = " . (int)$id);
$products_model = $product_query->fields['products_model'];
$quantity = $product_query->fields['quantity'];
// Get corresponding products_id using products_model
$product_id_query = $db->Execute("SELECT products_id FROM products WHERE products_model = '" . zen_db_input($products_model) . "'");
if ($product_id_query->RecordCount() > 0) {
$products_id = $product_id_query->fields['products_id'];
// Before adding to the cart, check if the product already exists in the cart
$basket_check_query = $db->Execute("SELECT products_id FROM customers_basket WHERE customers_id = $customers_id AND products_id = $products_id");
if ($basket_check_query->RecordCount() > 0) {
$cart->add_cart($products_id, $quantity, [], true);
// If the product already exists in the cart, update the quantity
$db->Execute("UPDATE customers_basket
SET customers_basket_quantity = customers_basket_quantity + $quantity
WHERE customers_id = $customers_id AND products_id = $products_id");
} else {
// If the product does not exist in the cart, add it
$date_added = date('Ymd');
$db->Execute("INSERT INTO customers_basket (customers_id, products_id, customers_basket_quantity, customers_basket_date_added)
VALUES ($customers_id, '" . zen_db_input($products_id) . "', $quantity, '" . zen_db_input($date_added) . "')");
}
} else {
// Error handling if product is not found (optional)
echo "The specified product was not found.";
}
}
}
// Redirect to update changes
zen_redirect(zen_href_link(FILENAME_SHOPPING_CART, '', 'SSL'));
}
Bookmarks