diff --git a/database/factories/ProductFactory.php b/database/factories/ProductFactory.php index b3b3b4d..2ac5c14 100644 --- a/database/factories/ProductFactory.php +++ b/database/factories/ProductFactory.php @@ -31,18 +31,6 @@ class ProductFactory extends Factory ]; } - public function onSale(): static - { - return $this->state(function (array $attributes) { - $regularPrice = $attributes['regular_price']; - return [ - 'sale_price' => $regularPrice * 0.8, - 'sale_start' => now()->subDay(), - 'sale_end' => now()->addWeek(), - ]; - }); - } - public function outOfStock(): static { return $this->state([ diff --git a/src/Models/Product.php b/src/Models/Product.php index 2fa3e2f..8811e6e 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -191,9 +191,13 @@ class Product extends Model implements Purchasable, Cartable public function isOnSale(): bool { + if (!$this->sale_start) { + return false; + } + $now = now(); - if ($this->sale_start && $now->lt($this->sale_start)) { + if ($now->lt($this->sale_start)) { return false; } @@ -210,8 +214,7 @@ class Product extends Model implements Purchasable, Cartable return $this->sale_price; } - $defaultPrice = $this->defaultPrice()->first(); - return $defaultPrice ? $defaultPrice->price : $this->regular_price; + return $this->defaultPrice()->first()?->getCurrentPrice(); } public function isInStock(): bool diff --git a/src/Models/ProductPrice.php b/src/Models/ProductPrice.php index ac0b539..34cd9f7 100644 --- a/src/Models/ProductPrice.php +++ b/src/Models/ProductPrice.php @@ -50,4 +50,13 @@ class ProductPrice extends Model implements Cartable { return $query->where('active', true); } + + public function getCurrentPrice(): float + { + if ($this->sale_unit_amount && $this->sale_unit_amount < $this->unit_amount) { + return $this->sale_unit_amount; + } + + return $this->unit_amount; + } } diff --git a/src/Traits/HasShoppingCapabilities.php b/src/Traits/HasShoppingCapabilities.php index 09d4acc..cb5e274 100644 --- a/src/Traits/HasShoppingCapabilities.php +++ b/src/Traits/HasShoppingCapabilities.php @@ -205,7 +205,8 @@ trait HasShoppingCapabilities public function getCartTotal(?string $cartId = null): float { return $this->cartItems()->get()->sum(function ($item) { - return $item->purchasable->getCurrentPrice() * $item->quantity; + dump('getCurrentPrice',get_class($item->purchasable),$item->purchasable->getCurrentPrice()); + return ($item->purchasable->getCurrentPrice() ?? 0) * $item->quantity; }); } diff --git a/tests/Feature/PurchaseFlowTest.php b/tests/Feature/PurchaseFlowTest.php index fe7d65d..7b2c8a5 100644 --- a/tests/Feature/PurchaseFlowTest.php +++ b/tests/Feature/PurchaseFlowTest.php @@ -138,6 +138,9 @@ class PurchaseFlowTest extends TestCase $product1 = Product::factory()->withPrices(unit_amount:40)->create(); $product2 = Product::factory()->withPrices(unit_amount:60)->create(); + $this->assertNotNull($product1->getCurrentPrice()); + $this->assertNotNull($product2->getCurrentPrice()); + $user->addToCart($product1, quantity: 2); $user->addToCart($product2, quantity: 1); diff --git a/tests/Unit/ProductPricingTest.php b/tests/Unit/ProductPricingTest.php index 9014f20..3385d24 100644 --- a/tests/Unit/ProductPricingTest.php +++ b/tests/Unit/ProductPricingTest.php @@ -13,23 +13,25 @@ class ProductPricingTest extends TestCase /** @test */ public function it_returns_regular_price_when_not_on_sale() { - $product = Product::factory()->create([ - 'regular_price' => 100, - 'sale_price' => null, - ]); + $product = Product::factory()->withPrices(2, 100)->create(); + $this->assertEquals(2, $product->prices()->count()); + $this->assertFalse($product->isOnSale()); + $this->assertNotNull($product->defaultPrice()->first()); $this->assertEquals(100, $product->getCurrentPrice()); } /** @test */ public function it_returns_sale_price_when_on_sale() { - $product = Product::factory()->create([ - 'regular_price' => 100, - 'sale_price' => 80, - 'sale_start' => now()->subDay(), - 'sale_end' => now()->addDay(), - ]); + $product = Product::factory() + ->withPrices(2, 100) + ->onSale( + sale_price: 80, + sale_start: now()->subDay(), + sale_end: now()->addDay(), + ) + ->create(); $this->assertEquals(80, $product->getCurrentPrice()); }