From 71b34b9acc9217c6e2d8e7d0665aa40428d043fe Mon Sep 17 00:00:00 2001 From: Milwad Khosravi Date: Sun, 30 Jun 2024 10:52:16 +0330 Subject: [PATCH] add cartable contract --- src/Cartable.php | 10 ++++++++++ src/Models/Cart.php | 11 +++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 src/Cartable.php diff --git a/src/Cartable.php b/src/Cartable.php new file mode 100644 index 0000000..588c871 --- /dev/null +++ b/src/Cartable.php @@ -0,0 +1,10 @@ +id(); } + if (! $item instanceof Cartable) { + throw new \Exception('The item must be an instance of Cartable'); + } $cart = $query->firstOrCreate(['user_id' => $userId]); $cartItem = new CartItem([ - 'itemable_id' => $item->id, + 'itemable_id' => $item->getKey(), 'itemable_type' => $item::class, 'quantity' => $quantity, ]); @@ -74,7 +81,7 @@ public function calculatedPriceByQuantity(): int { $totalPrice = 0; foreach ($this->items()->get() as $item) { - $totalPrice += (int) $item->quantity * (int) $item->itemable->price; + $totalPrice += (int) $item->quantity * (int) $item->itemable->getPrice(); } return $totalPrice;