|
| 1 | +<?php |
| 2 | + |
| 3 | + namespace app\models; |
| 4 | + use app\core\DbModel; |
| 5 | + use app\core\Application; |
| 6 | + |
| 7 | + class Cart extends DbModel |
| 8 | + { |
| 9 | + public int $id; |
| 10 | + public int $cart_id; |
| 11 | + public int $user_id; |
| 12 | + public int $product_id; |
| 13 | + public int $quantity; |
| 14 | + public ?string $created_at = null; |
| 15 | + public ?string $updated_at = null; |
| 16 | + |
| 17 | + public function tableName(): string { |
| 18 | + return 'cart_items'; |
| 19 | + } |
| 20 | + |
| 21 | + public function attributes(): array { |
| 22 | + return ['cart_id','product_id','quantity']; |
| 23 | + } |
| 24 | + |
| 25 | + public function primaryKey(): string { |
| 26 | + return 'id'; |
| 27 | + } |
| 28 | + |
| 29 | + // add a product to cart |
| 30 | + public function addToCart(int $user_id, int $product_id, int $quantity) { |
| 31 | + //get the cart_id for the user |
| 32 | + $sql = 'SELECT cart_id FROM customer WHERE cus_id = :cus_id'; |
| 33 | + $stmt = self::prepare($sql); |
| 34 | + $stmt->bindValue(':cus_id', $user_id); |
| 35 | + $stmt->execute(); |
| 36 | + $cart = $stmt->fetchColumn(); |
| 37 | + |
| 38 | + //if user does not have an cart, then create a one |
| 39 | + if (!$cart) { |
| 40 | + // $this->db->query("INSERT INTO cart (user_id) VALUES (?)", [$user_id]); |
| 41 | + // $cart = $this->db->lastInsertId(); |
| 42 | + // $this->db->query("UPDATE customer SET cart_id = ? WHERE cus_id = ?", [$cart, $user_id]); |
| 43 | + $stmt = Application::$app->db->pdo->prepare("INSERT INTO cart (user_id) VALUES (?)"); |
| 44 | + $stmt->execute([$user_id]); |
| 45 | + |
| 46 | + // Get last inserted cart_id |
| 47 | + $cart = Application::$app->db->pdo->lastInsertId(); |
| 48 | + |
| 49 | + // Update customer table with the cart_id |
| 50 | + $stmt = Application::$app->db->pdo->prepare("UPDATE customer SET cart_id = ? WHERE cus_id = ?"); |
| 51 | + $stmt->execute([$cart, $user_id]); |
| 52 | + } |
| 53 | + // $existing = $this->db->query("SELECT id FROM cart_items WHERE cart_id = ? AND product_id = ?", [$cart, $product_id])->fetchColumn(); |
| 54 | + // Check if the product is already in the cart |
| 55 | + $stmt = Application::$app->db->pdo->prepare("SELECT id FROM cart_items WHERE cart_id = ? AND product_id = ?"); |
| 56 | + $stmt->execute([$cart, $product_id]); |
| 57 | + $existing = $stmt->fetchColumn(); |
| 58 | + |
| 59 | + if ($existing) { |
| 60 | + // $this->db->query("UPDATE cart_items SET quantity = quantity + ? WHERE id = ?", [$quantity, $existing]); |
| 61 | + // Update quantity if product exists in cart |
| 62 | + $stmt = Application::$app->db->pdo->prepare("UPDATE cart_items SET quantity = quantity + ? WHERE id = ?"); |
| 63 | + $stmt->execute([$quantity, $existing]); |
| 64 | + } else { |
| 65 | + // $this->db->query("INSERT INTO cart_items (cart_id, product_id, quantity) VALUES (?, ?, ?)", [$cart, $product_id, $quantity]); |
| 66 | + // Insert new product into cart_items |
| 67 | + $stmt = Application::$app->db->pdo->prepare("INSERT INTO cart_items (cart_id, product_id, quantity) VALUES (?, ?, ?)"); |
| 68 | + $stmt->execute([$cart, $product_id, $quantity]); |
| 69 | + } |
| 70 | + |
| 71 | + return true; |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + //get all items for a specific user |
| 76 | + public function getCartItems(int $user_id) { |
| 77 | + $db = Application::$app->db->pdo; |
| 78 | + $stmt = $db->prepare(" |
| 79 | + SELECT ci.id, ci.product_id, ci.quantity, p.description, p.price, p.media |
| 80 | + FROM cart_items ci |
| 81 | + JOIN cart c ON ci.cart_id = c.id |
| 82 | + JOIN product p ON ci.product_id = p.product_id |
| 83 | + WHERE c.user_id = ? |
| 84 | + "); |
| 85 | + $stmt->execute([$user_id]); |
| 86 | + return $stmt->fetchAll(\PDO::FETCH_ASSOC); |
| 87 | + } |
| 88 | + |
| 89 | + //model to remove items from the cart |
| 90 | + public function removeCartItem(int $user_id, int $product_id) { |
| 91 | + $db = Application::$app->db->pdo; |
| 92 | + |
| 93 | + $stmt = $db->prepare("SELECT id FROM cart WHERE user_id = ?"); |
| 94 | + $stmt->execute([$user_id]); |
| 95 | + $cart = $stmt->fetch(\PDO::FETCH_ASSOC); |
| 96 | + |
| 97 | + if (!$cart) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + $cart_id = $cart['id']; |
| 102 | + |
| 103 | + $stmt = $db->prepare("DELETE FROM cart_items WHERE cart_id = ? AND product_id = ?"); |
| 104 | + return $stmt->execute([$cart_id, $product_id]); |
| 105 | + } |
| 106 | + |
| 107 | + public function rules(): array |
| 108 | + { |
| 109 | + return [ |
| 110 | + |
| 111 | + ]; |
| 112 | + } |
| 113 | + |
| 114 | + public function updateRules(): array |
| 115 | + { |
| 116 | + return [ |
| 117 | + |
| 118 | + ]; |
| 119 | + } |
| 120 | + |
| 121 | + } |
0 commit comments