$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; foreach ($products_in_cart as $num => $product) { // Calculate the subtotal $subtotal += (float)$product['options_price'] * (int)$product['quantity']; } template_header('Shopping Cart'); $view = '

'.$h1_cart_name.'

'.$navigation_back_to_store.'

'; if (empty($products_in_cart)){ $view .= ' '; } else { foreach ($products_in_cart as $num => $product){ // Ensure product price is a numeric value $product['options_price'] = isset($product['options_price']) && $product['options_price'] > 0 ? floatval($product['options_price']) : 0.00; if (isset($product['options']) && $product['options'] !=''){ $prod_options = ''; foreach ($product['options'] as $prod_opt){ $prod_options .= (${$prod_opt} ?? $prod_opt).', '; } } $view .= ' '; if ($product['options'] == $h2_cart_sample_product && !empty(category_id_checkout_samples)){ $view .= ' '; } else { $view .= ' '; } $view .= ' '; } } $view .= '
'.$tr_product.' '.$tr_price.' '.$tr_quantity.' '.$tr_total.'
'.$cart_message_empty.'
'; if (!empty($product['meta']['img'])){ $view .= ' '.$product['meta']['name'].' '; } $view .= ' '.(${$product['meta']['name']} ?? $product['meta']['name']).'
Remove
'.htmlspecialchars(substr($prod_options, 0,-2), ENT_QUOTES).' '.currency_code.''.number_format($product['options_price'],2).' '.currency_code.''.number_format($product['options_price'] * $product['quantity'],2).'
'; //SUGGESTIONS if (!empty($products_in_cart) && !empty(category_id_checkout_suggestions)){ $view .= getAccessoiries($clientsecret,category_id_checkout_suggestions); } // SAMPLES if (!empty($products_in_cart) && !empty(category_id_checkout_samples)){ $view .= getSamples($clientsecret,category_id_checkout_samples); } $view .= '
'.$total_subtotal.' '.currency_code.''.number_format($subtotal,2).' '.$total_note.'

'.$navigation_back_to_store.'

'; //OUTPUT echo $view; template_footer(); ?>