215 lines
7.8 KiB
PHP
215 lines
7.8 KiB
PHP
<?php
|
|
require_once 'config.php';
|
|
|
|
class Cart {
|
|
private $conn;
|
|
|
|
public function __construct($conn) {
|
|
$this->conn = $conn;
|
|
}
|
|
|
|
public function addToCart($product_id, $quantity = 1) {
|
|
if (!is_logged_in()) {
|
|
return ['success' => false, 'message' => 'يجب تسجيل الدخول لإضافة منتجات إلى السلة'];
|
|
}
|
|
|
|
// Check if product exists and has enough stock
|
|
$stmt = $this->conn->prepare("SELECT stock_quantity FROM products WHERE id = ?");
|
|
$stmt->bind_param("i", $product_id);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
|
|
if ($result->num_rows === 0) {
|
|
return ['success' => false, 'message' => 'المنتج غير موجود'];
|
|
}
|
|
|
|
$product = $result->fetch_assoc();
|
|
if ($product['stock_quantity'] < $quantity) {
|
|
return ['success' => false, 'message' => 'الكمية المطلوبة غير متوفرة'];
|
|
}
|
|
|
|
// Check if product already in cart
|
|
$stmt = $this->conn->prepare("SELECT id, quantity FROM shopping_cart WHERE user_id = ? AND product_id = ?");
|
|
$stmt->bind_param("ii", $_SESSION['user_id'], $product_id);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
|
|
if ($result->num_rows > 0) {
|
|
$cart_item = $result->fetch_assoc();
|
|
$new_quantity = $cart_item['quantity'] + $quantity;
|
|
|
|
if ($new_quantity > $product['stock_quantity']) {
|
|
return ['success' => false, 'message' => 'الكمية المطلوبة تتجاوز المخزون المتوفر'];
|
|
}
|
|
|
|
$stmt = $this->conn->prepare("UPDATE shopping_cart SET quantity = ? WHERE id = ?");
|
|
$stmt->bind_param("ii", $new_quantity, $cart_item['id']);
|
|
} else {
|
|
$stmt = $this->conn->prepare("INSERT INTO shopping_cart (user_id, product_id, quantity) VALUES (?, ?, ?)");
|
|
$stmt->bind_param("iii", $_SESSION['user_id'], $product_id, $quantity);
|
|
}
|
|
|
|
if ($stmt->execute()) {
|
|
return ['success' => true, 'message' => 'تمت إضافة المنتج إلى السلة'];
|
|
}
|
|
|
|
return ['success' => false, 'message' => 'حدث خطأ أثناء إضافة المنتج إلى السلة'];
|
|
}
|
|
|
|
public function updateCartItem($cart_id, $quantity) {
|
|
if (!is_logged_in()) {
|
|
return ['success' => false, 'message' => 'يجب تسجيل الدخول لتحديث السلة'];
|
|
}
|
|
|
|
// Check if cart item exists and belongs to user
|
|
$stmt = $this->conn->prepare("
|
|
SELECT c.*, p.stock_quantity
|
|
FROM shopping_cart c
|
|
JOIN products p ON c.product_id = p.id
|
|
WHERE c.id = ? AND c.user_id = ?
|
|
");
|
|
$stmt->bind_param("ii", $cart_id, $_SESSION['user_id']);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
|
|
if ($result->num_rows === 0) {
|
|
return ['success' => false, 'message' => 'المنتج غير موجود في السلة'];
|
|
}
|
|
|
|
$cart_item = $result->fetch_assoc();
|
|
if ($quantity > $cart_item['stock_quantity']) {
|
|
return ['success' => false, 'message' => 'الكمية المطلوبة غير متوفرة'];
|
|
}
|
|
|
|
if ($quantity <= 0) {
|
|
return $this->removeFromCart($cart_id);
|
|
}
|
|
|
|
$stmt = $this->conn->prepare("UPDATE shopping_cart SET quantity = ? WHERE id = ? AND user_id = ?");
|
|
$stmt->bind_param("iii", $quantity, $cart_id, $_SESSION['user_id']);
|
|
|
|
if ($stmt->execute()) {
|
|
return ['success' => true, 'message' => 'تم تحديث الكمية'];
|
|
}
|
|
|
|
return ['success' => false, 'message' => 'حدث خطأ أثناء تحديث الكمية'];
|
|
}
|
|
|
|
public function removeFromCart($cart_id) {
|
|
if (!is_logged_in()) {
|
|
return ['success' => false, 'message' => 'يجب تسجيل الدخول لحذف منتجات من السلة'];
|
|
}
|
|
|
|
$stmt = $this->conn->prepare("DELETE FROM shopping_cart WHERE id = ? AND user_id = ?");
|
|
$stmt->bind_param("ii", $cart_id, $_SESSION['user_id']);
|
|
|
|
if ($stmt->execute()) {
|
|
return ['success' => true, 'message' => 'تم حذف المنتج من السلة'];
|
|
}
|
|
|
|
return ['success' => false, 'message' => 'حدث خطأ أثناء حذف المنتج من السلة'];
|
|
}
|
|
|
|
public function getCart() {
|
|
if (!is_logged_in()) {
|
|
return ['items' => [], 'total' => 0];
|
|
}
|
|
|
|
$query = "
|
|
SELECT c.id as cart_id, c.quantity,
|
|
p.id as product_id, p.name, p.name_ar, p.price, p.sale_price,
|
|
(SELECT image_url FROM product_images WHERE product_id = p.id AND is_primary = 1 LIMIT 1) as image
|
|
FROM shopping_cart c
|
|
JOIN products p ON c.product_id = p.id
|
|
WHERE c.user_id = ?
|
|
ORDER BY c.created_at DESC
|
|
";
|
|
|
|
$stmt = $this->conn->prepare($query);
|
|
$stmt->bind_param("i", $_SESSION['user_id']);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
|
|
$items = [];
|
|
$total = 0;
|
|
while ($row = $result->fetch_assoc()) {
|
|
$price = $row['sale_price'] ?? $row['price'];
|
|
$subtotal = $price * $row['quantity'];
|
|
$total += $subtotal;
|
|
|
|
$row['price'] = $price;
|
|
$row['subtotal'] = $subtotal;
|
|
$items[] = $row;
|
|
}
|
|
|
|
return [
|
|
'items' => $items,
|
|
'total' => $total
|
|
];
|
|
}
|
|
|
|
public function clearCart() {
|
|
if (!is_logged_in()) {
|
|
return ['success' => false, 'message' => 'يجب تسجيل الدخول لتفريغ السلة'];
|
|
}
|
|
|
|
$stmt = $this->conn->prepare("DELETE FROM shopping_cart WHERE user_id = ?");
|
|
$stmt->bind_param("i", $_SESSION['user_id']);
|
|
|
|
if ($stmt->execute()) {
|
|
return ['success' => true, 'message' => 'تم تفريغ السلة'];
|
|
}
|
|
|
|
return ['success' => false, 'message' => 'حدث خطأ أثناء تفريغ السلة'];
|
|
}
|
|
|
|
public function applyCoupon($code) {
|
|
if (!is_logged_in()) {
|
|
return ['success' => false, 'message' => 'يجب تسجيل الدخول لاستخدام الكوبون'];
|
|
}
|
|
|
|
$stmt = $this->conn->prepare("
|
|
SELECT * FROM coupons
|
|
WHERE code = ?
|
|
AND is_active = 1
|
|
AND (start_date IS NULL OR start_date <= NOW())
|
|
AND (end_date IS NULL OR end_date >= NOW())
|
|
AND (usage_limit IS NULL OR used_count < usage_limit)
|
|
");
|
|
$stmt->bind_param("s", $code);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
|
|
if ($result->num_rows === 0) {
|
|
return ['success' => false, 'message' => 'الكوبون غير صالح'];
|
|
}
|
|
|
|
$coupon = $result->fetch_assoc();
|
|
$cart = $this->getCart();
|
|
|
|
if ($coupon['min_purchase'] && $cart['total'] < $coupon['min_purchase']) {
|
|
return ['success' => false, 'message' => sprintf('الحد الأدنى للطلب %s ريال', $coupon['min_purchase'])];
|
|
}
|
|
|
|
$discount = $coupon['type'] === 'percentage'
|
|
? $cart['total'] * ($coupon['value'] / 100)
|
|
: $coupon['value'];
|
|
|
|
if ($coupon['max_discount'] && $discount > $coupon['max_discount']) {
|
|
$discount = $coupon['max_discount'];
|
|
}
|
|
|
|
$_SESSION['coupon'] = [
|
|
'code' => $coupon['code'],
|
|
'discount' => $discount
|
|
];
|
|
|
|
return [
|
|
'success' => true,
|
|
'message' => 'تم تطبيق الكوبون',
|
|
'discount' => $discount,
|
|
'total_after_discount' => $cart['total'] - $discount
|
|
];
|
|
}
|
|
}
|