$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['product'])) { //VALIDATE THE INPUT FOR THE SHOPPING CART $payload = json_encode($_POST['product'], JSON_UNESCAPED_UNICODE); $product_to_cart = ioAPIv2('/v2/shopping_cart/',$payload,$clientsecret); $product_to_cart = json_decode($product_to_cart,true); // Check if the product exists (array is not empty) if ($product_to_cart['quantity'] > 0) { // Product exists in database, now we can create/update the session variable for the cart if (!isset($_SESSION['cart'])) { // Shopping cart session variable doesnt exist, create it $_SESSION['cart'] = []; } $cart_product = &get_cart_product($product_to_cart['id'], $product_to_cart['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'][] = $product_to_cart; } } // Prevent form resubmission... header('Location: ' . url('index.php?page=cart')); exit; } // 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; foreach ($products_in_cart as $num => $product) { // Calculate the subtotal $subtotal += (float)$product['options_price'] * (int)$product['quantity']; } template_header(($shopping_cart_header ?? 'Shopping Cart')); $view = '