Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed May 3, 2024
1 parent 32037d7 commit 6d57582
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Cart/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cone\Bazar\Cart;

use Cone\Bazar\Bazar;
use Cone\Bazar\Exceptions\CartException;
use Cone\Bazar\Gateway\Response;
use Cone\Bazar\Interfaces\Buyable;
use Cone\Bazar\Models\Address;
Expand Down Expand Up @@ -88,6 +89,10 @@ public function getItem(string $id): ?Item
*/
public function addItem(Buyable $buyable, float $quantity = 1, array $properties = []): Item
{
if (! $buyable->buyable($this->getModel())) {
throw new CartException(sprintf('Unable to add [%s] item to the cart.', get_class($buyable)));
}

$item = $buyable->toItem(
$this->getModel(),
['quantity' => $quantity, 'properties' => $properties]
Expand Down
10 changes: 10 additions & 0 deletions src/Exceptions/CartException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Cone\Bazar\Exceptions;

use Exception;

class CartException extends Exception
{
//
}
8 changes: 8 additions & 0 deletions src/Models/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Cone\Bazar\Bazar;
use Cone\Bazar\Database\Factories\CartFactory;
use Cone\Bazar\Exceptions\CartException;
use Cone\Bazar\Interfaces\Buyable;
use Cone\Bazar\Interfaces\Models\Cart as Contract;
use Cone\Bazar\Traits\Addressable;
use Cone\Bazar\Traits\InteractsWithDiscounts;
Expand Down Expand Up @@ -189,6 +191,12 @@ public function scopeExpired(Builder $query): Builder
*/
public function toOrder(): Order
{
$this->lineItems->each(function (Buyable $buyable): void {
if (! $buyable->buyable($this->order)) {
throw new CartException(sprintf('Unable to add [%s] item to the order.', get_class($buyable)));
}
});

$this->order->user()->associate($this->user)->save();

if ($this->order_id !== $this->order->getKey()) {
Expand Down
8 changes: 8 additions & 0 deletions src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public function variants(): HasMany
return $this->hasMany(Variant::getProxiedClass());
}

/**
* Determine whether the buyable object is available for the itemable instance.
*/
public function buyable(Itemable $itemable): bool
{
return true;
}

/**
* Scope the query only to the models that are out of stock.
*/
Expand Down

0 comments on commit 6d57582

Please sign in to comment.