prepare('SELECT * FROM products WHERE status = 1 AND (id = ? OR url_slug = ?)'); $stmt->execute([ $_GET['id'], $_GET['id'] ]); // Fetch the product from the database and return the result as an Array $product = $stmt->fetch(PDO::FETCH_ASSOC); // Check if the product exists (array is not empty) if (!$product) { // Output simple error if the id for the product doesn't exists (array is empty) http_response_code(404); exit('Product does not exist!'); } // Select the product images (if any) from the products_images table $stmt = $pdo->prepare('SELECT m.*, pm.position FROM products_media pm JOIN media m ON m.id = pm.media_id WHERE pm.product_id = ? ORDER BY pm.position ASC'); $stmt->execute([ $product['id'] ]); // Fetch the product images from the database and return the result as an Array $product_media = $stmt->fetchAll(PDO::FETCH_ASSOC); // Select the product options (if any) from the products_options table $stmt = $pdo->prepare('SELECT CONCAT(title, "::", type, "::", required) AS k, name, quantity, price, price_modifier, weight, weight_modifier, type, id, required FROM products_options WHERE product_id = ? ORDER BY position ASC'); $stmt->execute([ $product['id'] ]); // Fetch the product options from the database and return the result as an Array $product_options = $stmt->fetchAll(PDO::FETCH_GROUP); // Add the HTML meta data (for SEO purposes) $meta = ' '; if (isset($product_media[0]) && file_exists($product_media[0]['full_path'])) { $meta .= ''; } // If the user clicked the add to cart button if (isset($_POST['quantity']) && is_numeric($_POST['quantity'])) { // abs() function will prevent minus quantity and (int) will ensure the value is an integer (number) $quantity = abs((int)$_POST['quantity']); // Get product options $options = ''; $options_price = (float)$product['price']; $options_weight = (float)$product['weight']; // Iterate post data foreach ($_POST as $k => $v) { if (strpos($k, 'option-') !== false) { if (is_array($v)) { // Option is checkbox or radio element foreach ($v as $vv) { if (empty($vv)) continue; $options .= str_replace(['_', 'option-'], [' ', ''], $k) . '-' . $vv . ','; $stmt = $pdo->prepare('SELECT * FROM products_options WHERE title = ? AND name = ? AND product_id = ?'); $stmt->execute([ str_replace(['_', 'option-'], [' ', ''], $k), $vv, $product['id'] ]); $option = $stmt->fetch(PDO::FETCH_ASSOC); $options_price = $option['price_modifier'] == 'add' ? $options_price + $option['price'] : $options_price - $option['price']; $options_weight = $option['weight_modifier'] == 'add' ? $options_weight + $option['weight'] : $options_weight - $option['weight']; } } else { if (empty($v)) continue; $options .= str_replace(['_', 'option-'], [' ', ''], $k) . '-' . $v . ','; //------------------ //Update name otherwise option is not found //------------------ $name_update = '%|^|'.$v; $stmt = $pdo->prepare('SELECT * FROM products_options WHERE title = ? AND name like ? AND product_id = ?'); $stmt->execute([ str_replace(['_', 'option-'], [' ', ''], $k), $name_update, $product['id'] ]); //------------------ // OLD CODE //------------------ //$stmt = $pdo->prepare('SELECT * FROM products_options WHERE title = ? AND name = ? AND product_id = ?'); //$stmt->execute([ str_replace(['_', 'option-'], [' ', ''], $k), $v, $product['id'] ]); //------------------ $option = $stmt->fetch(PDO::FETCH_ASSOC); if (!$option) { // Option is text or datetime element $stmt = $pdo->prepare('SELECT * FROM products_options WHERE title = ? AND product_id = ?'); $stmt->execute([ str_replace(['_', 'option-'], [' ', ''], $k), $product['id'] ]); $option = $stmt->fetch(PDO::FETCH_ASSOC); } $options_price = $option['price_modifier'] == 'add' ? $options_price + $option['price'] : $options_price - $option['price']; $options_weight = $option['weight_modifier'] == 'add' ? $options_weight + $option['weight'] : $options_weight - $option['weight']; } } } $options_price = $options_price < 0 ? 0 : $options_price; $options = rtrim($options, ','); // Check if the product exists (array is not empty) if ($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['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' => $product['id'], 'quantity' => $quantity, 'options' => $options, 'options_price' => $options_price, 'options_weight' => $options_weight, 'shipping_price' => 0.00 ]; } } // Prevent form resubmission... header('Location: ' . url('index.php?page=cart')); exit; } } else { // Output simple error if the id wasn't specified http_response_code(404); exit('Product does not exist!'); } //get all media $stmt = $pdo->query('SELECT id, full_path, caption FROM media'); $stmt->execute(); $media2 = $stmt->fetchAll(PDO::FETCH_ASSOC); //LINK to products page: $products_link = url(link_to_collection); $product_link = url('index.php?page=product&id='.($product['url_slug'] ? $product['url_slug'] : $product['id'])); //Notifier - when 1 user ask for product notification $notifier = 0; if (isset($_POST["notifier"])){ $stmt = $pdo->prepare('SELECT * FROM accounts WHERE id = ?'); $stmt->execute([ $_SESSION['account_id'] ]); $account = $stmt->fetch(PDO::FETCH_ASSOC); $email = $account['email']; send_product_notification_email($email, $_POST["product_details"]); $notifier = 1; } //CREATE OPTION_PICTURE ARRAY FOR USE IN OPTION OVERVIEW $option_profile = json_decode($product['product_config']) ?? ''; if (!empty($option_profile) && $option_profile !=''){ //CREATE OPTION PICTURE ARRAY $option_picture[] = ''; foreach ($option_profile as $option){ //CHECK FOR RELATED MEDIA foreach ($media2 as $media_item2){ if ($media_item2['id'] == $option->IMG_large_id){ $option_picture[$option->option_id] = $media_item2['full_path']; } } } } ?> =template_header($product['name'], $meta)?>
=$error?>
/ =$product['name']?>
'.$stock_status.'
'; ?>