$v) { if (strpos($k, 'quantity') !== false && is_numeric($v)) { $id = str_replace('quantity-', '', $k); // abs() function will prevent minus quantity and (int) will ensure the value is an integer (number) $quantity = abs((int)$v); // Always do checks and validation if (is_numeric($id) && isset($_SESSION['cart'][$id]) && $quantity > 0) { // Update new quantity $_SESSION['cart'][$id]['quantity'] = $quantity; } } } // Send the user to the place order page if they click the Place Order button, also the cart should not be empty if (isset($_POST['checkout']) && !empty($_SESSION['cart'])) { header('Location: ' . url('index.php?page=checkout')); exit; } header('Location: ' . url('index.php?page=cart')); exit; } // Check if accessoiries are added if (isset($_POST['accessoiries'])){ $options = ''; $quantity = 1; $cart_product = &get_cart_product($_POST['add_product_id'], $options); if ($cart_product) { // Product exists in cart, update the quanity $cart_product['quantity'] += $quantity; } else { // Product is not in cart, add it $_SESSION['cart'][] = [ 'id' => $_POST['add_product_id'], 'quantity' => $quantity, 'options' => $options, 'options_price' => $_POST['add_product_price'], 'options_weight' => $_POST['add_product_weight'], 'shipping_price' => 0.00 ]; } } // Check if samples are added if (isset($_POST['samples'])){ $options = $h2_cart_sample_product; $quantity = 1; $cart_product = &get_cart_product($_POST['add_product_id'], $options); if ($cart_product) { // Do no nothing } else { //remove existing product from CART foreach ($_SESSION['cart'] as $num => $product){ if ($product['options'] == $h2_cart_sample_product && !empty(category_id_checkout_samples)){ array_splice($_SESSION['cart'], $num, 1); } } //ADD Product to the chart $_SESSION['cart'][] = [ 'id' => $_POST['add_product_id'], 'quantity' => $quantity, 'options' => $options, 'options_price' => $_POST['add_product_price'], 'options_weight' => $_POST['add_product_weight'], 'shipping_price' => 0.00 ]; } } // Check the session variable for products in cart $products_in_cart = isset($_SESSION['cart']) ? $_SESSION['cart'] : []; $subtotal = 0.00; // If there are products in cart if ($products_in_cart) { // There are products in the cart so we need to select those products from the database // Products in cart array to question mark string array, we need the SQL statement to include: IN (?,?,?,...etc) $array_to_question_marks = implode(',', array_fill(0, count($products_in_cart), '?')); // Prepare SQL statement // $stmt = $pdo->prepare('SELECT p.id, pc.category_id, p.* FROM products p LEFT JOIN products_categories pc ON p.id = pc.product_id LEFT JOIN categories c ON c.id = pc.category_id WHERE p.id IN (' . $array_to_question_marks . ') GROUP BY p.id'); $stmt = $pdo->prepare('SELECT p.*, (SELECT m.full_path FROM products_media pm JOIN media m ON m.id = pm.media_id WHERE pm.product_id = p.id ORDER BY pm.position ASC LIMIT 1) AS img FROM products p WHERE p.id IN (' . $array_to_question_marks . ')'); // Leverage the array_column function to retrieve only the id's of the products $stmt->execute(array_column($products_in_cart, 'id')); // Fetch the products from the database and return the result as an Array $products = $stmt->fetchAll(PDO::FETCH_ASSOC); // Iterate the products in cart and add the meta data (product name, desc, etc) foreach ($products_in_cart as &$cart_product) { foreach ($products as $product) { if ($cart_product['id'] == $product['id']) { $cart_product['meta'] = $product; // Calculate the subtotal $subtotal += (float)$cart_product['options_price'] * (int)$cart_product['quantity']; } } } } ?> =template_header('Shopping Cart')?>