ÇOK SATANLAR
HIZLI KARGO
Aynı gün kargo imkanı
TAKSİT İMKANI
Kredi kartına taksit seçenekleri
GÜVENLİ ÖDEME
SSL sertifikalı güvenli ödeme
KOLAY İADE
14 gün içerisinde koşulsuz iade
// Cart functions function addToCart($product_id, $quantity = 1) { if (!isset($_SESSION['cart'])) { $_SESSION['cart'] = array(); } // Check if product exists and has stock $stmt = $GLOBALS['pdo']->prepare("SELECT * FROM products WHERE id = ? AND status = 'active'"); $stmt->execute([$product_id]); $product = $stmt->fetch(); if (!$product) { return false; } // Check stock $current_quantity = $_SESSION['cart'][$product_id] ?? 0; $new_quantity = $current_quantity + $quantity; if ($new_quantity > $product['stock_quantity']) { return false; } $_SESSION['cart'][$product_id] = $new_quantity; return true; } function removeFromCart($product_id) { if (isset($_SESSION['cart'][$product_id])) { unset($_SESSION['cart'][$product_id]); } } function updateCartQuantity($product_id, $quantity) { if ($quantity <= 0) { removeFromCart($product_id); return; } // Check stock $stmt = $GLOBALS['pdo']->prepare("SELECT stock_quantity FROM products WHERE id = ?"); $stmt->execute([$product_id]); $product = $stmt->fetch(); if ($product && $quantity <= $product['stock_quantity']) { $_SESSION['cart'][$product_id] = $quantity; } } function clearCart() { $_SESSION['cart'] = array(); } function getCartItems() { if (empty($_SESSION['cart'])) { return array(); } $product_ids = array_keys($_SESSION['cart']); $placeholders = str_repeat('?,', count($product_ids) - 1) . '?'; $stmt = $GLOBALS['pdo']->prepare(" SELECT p.*, c.name as category_name FROM products p LEFT JOIN categories c ON p.category_id = c.id WHERE p.id IN ($placeholders) AND p.status = 'active' "); $stmt->execute($product_ids); $products = $stmt->fetchAll(); $cart_items = array(); foreach ($products as $product) { $product['quantity'] = $_SESSION['cart'][$product['id']]; $cart_items[] = $product; } return $cart_items; } function getCartTotal() { $cart_items = getCartItems(); $total = 0; foreach ($cart_items as $item) { $total += $item['price'] * $item['quantity']; } return $total; } function getCartCount() { if (empty($_SESSION['cart'])) { return 0; } return array_sum($_SESSION['cart']); } function getCartItemCount($product_id) { return $_SESSION['cart'][$product_id] ?? 0; } // Order functions function createOrder($customer_data, $shipping_data, $payment_method) { $cart_items = getCartItems(); if (empty($cart_items)) { return false; } $pdo = $GLOBALS['pdo']; try { $pdo->beginTransaction(); // Calculate totals $subtotal = getCartTotal(); $shipping_cost = $subtotal >= 500 ? 0 : 29.90; $total = $subtotal + $shipping_cost; // Create order $order_number = 'ODS' . date('Ymd') . rand(1000, 9999); $stmt = $pdo->prepare(" INSERT INTO orders ( order_number, customer_name, customer_email, customer_phone, shipping_address, shipping_city, shipping_district, shipping_postal_code, subtotal, shipping_cost, total, payment_method, status, created_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending', NOW()) "); $stmt->execute([ $order_number, $customer_data['name'], $customer_data['email'], $customer_data['phone'], $shipping_data['address'], $shipping_data['city'], $shipping_data['district'], $shipping_data['postal_code'], $subtotal, $shipping_cost, $total, $payment_method ]); $order_id = $pdo->lastInsertId(); // Add order items $stmt = $pdo->prepare(" INSERT INTO order_items (order_id, product_id, product_name, product_sku, quantity, price, total) VALUES (?, ?, ?, ?, ?, ?, ?) "); foreach ($cart_items as $item) { $item_total = $item['price'] * $item['quantity']; $stmt->execute([ $order_id, $item['id'], $item['name'], $item['sku'], $item['quantity'], $item['price'], $item_total ]); // Update stock $update_stock = $pdo->prepare("UPDATE products SET stock_quantity = stock_quantity - ? WHERE id = ?"); $update_stock->execute([$item['quantity'], $item['id']]); } $pdo->commit(); // Clear cart clearCart(); return [ 'order_id' => $order_id, 'order_number' => $order_number, 'total' => $total ]; } catch (Exception $e) { $pdo->rollBack(); error_log("Order creation failed: " . $e->getMessage()); return false; } } // Email functions function sendOrderConfirmationEmail($order_id) { // This would integrate with your email service // For now, just log the action error_log("Order confirmation email should be sent for order ID: " . $order_id); } // Utility functions function formatPrice($price) { return number_format($price, 2, ',', '.') . ' TL'; } function generateOrderNumber() { return 'ODS' . date('Ymd') . rand(1000, 9999); } function validateEmail($email) { return filter_var($email, FILTER_VALIDATE_EMAIL); } function validatePhone($phone) { // Turkish phone number validation $phone = preg_replace('/[^0-9]/', '', $phone); return preg_match('/^(5[0-9]{9}|0?5[0-9]{9})$/', $phone); } function sanitizeInput($input) { return htmlspecialchars(trim($input), ENT_QUOTES, 'UTF-8'); } function redirectWithMessage($url, $message, $type = 'success') { $_SESSION['flash_message'] = $message; $_SESSION['flash_type'] = $type; header("Location: $url"); exit; } function getFlashMessage() { if (isset($_SESSION['flash_message'])) { $message = $_SESSION['flash_message']; $type = $_SESSION['flash_type'] ?? 'info'; unset($_SESSION['flash_message'], $_SESSION['flash_type']); return ['message' => $message, 'type' => $type]; } return null; }
Aynı gün kargo imkanı
Kredi kartına taksit seçenekleri
SSL sertifikalı güvenli ödeme
14 gün içerisinde koşulsuz iade