diff --git a/src/Console/Commands/ShopAddExampleProducts.php b/src/Console/Commands/ShopAddExampleProducts.php index e54d98d..63780fe 100644 --- a/src/Console/Commands/ShopAddExampleProducts.php +++ b/src/Console/Commands/ShopAddExampleProducts.php @@ -134,7 +134,6 @@ class ShopAddExampleProducts extends Command 'is_visible' => true, 'featured' => $this->faker->boolean(20), // 20% featured 'price' => $onSale ? $regularPrice * 0.8 : $regularPrice, - 'regular_price' => $regularPrice, 'sale_price' => $onSale ? $regularPrice * $this->faker->randomFloat(2, 0.6, 0.9) : null, 'sale_start' => $onSale ? now()->subDays($this->faker->numberBetween(1, 30)) : null, 'sale_end' => $onSale ? now()->addDays($this->faker->numberBetween(7, 60)) : null, @@ -361,7 +360,6 @@ class ShopAddExampleProducts extends Command 'status' => 'published', 'is_visible' => false, // Variations are not directly visible 'price' => $product->price + ($index * 5), // Slight price increase per size - 'regular_price' => $product->regular_price + ($index * 5), 'manage_stock' => true, 'stock_quantity' => $this->faker->numberBetween(5, 50), 'in_stock' => true, diff --git a/src/Models/Product.php b/src/Models/Product.php index 988ef7d..929c11d 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -216,11 +216,7 @@ class Product extends Model implements Purchasable, Cartable public function getCurrentPrice(): ?float { - if ($this->isOnSale()) { - return $this->sale_price; - } - - return $this->defaultPrice()->first()?->getCurrentPrice(); + return $this->defaultPrice()->first()?->getCurrentPrice($this->isOnSale()); } public function isInStock(): bool @@ -425,7 +421,7 @@ class Product extends Model implements Purchasable, Cartable return false; } - return $this->stock_quantity <= $this->low_stock_threshold; + return $this->getAvailableStock() <= $this->low_stock_threshold; } public function isVisible(): bool @@ -452,7 +448,6 @@ class Product extends Model implements Purchasable, Cartable 'short_description' => $this->getLocalized('short_description'), 'type' => $this->type, 'price' => $this->getCurrentPrice(), - 'regular_price' => $this->regular_price, 'sale_price' => $this->sale_price, 'is_on_sale' => $this->isOnSale(), 'low_stock' => $this->isLowStock(), diff --git a/src/Models/ProductPrice.php b/src/Models/ProductPrice.php index 34cd9f7..e6c18af 100644 --- a/src/Models/ProductPrice.php +++ b/src/Models/ProductPrice.php @@ -51,9 +51,9 @@ class ProductPrice extends Model implements Cartable return $query->where('active', true); } - public function getCurrentPrice(): float + public function getCurrentPrice($sale_price): float { - if ($this->sale_unit_amount && $this->sale_unit_amount < $this->unit_amount) { + if ($sale_price) { return $this->sale_unit_amount; } diff --git a/tests/Feature/StockManagementTest.php b/tests/Feature/StockManagementTest.php index 740e7bd..cb41888 100644 --- a/tests/Feature/StockManagementTest.php +++ b/tests/Feature/StockManagementTest.php @@ -210,11 +210,11 @@ class StockManagementTest extends TestCase /** @test */ public function product_tracks_low_stock_threshold() { - $product = Product::factory()->create([ - 'manage_stock' => true, - 'stock_quantity' => 15, - 'low_stock_threshold' => 10, - ]); + $product = Product::factory() + ->withStocks(12) + ->create([ + 'low_stock_threshold' => 10, + ]); $this->assertFalse($product->isLowStock()); @@ -226,14 +226,12 @@ class StockManagementTest extends TestCase /** @test */ public function it_updates_in_stock_status_automatically() { - $product = Product::factory()->create([ - 'manage_stock' => true, - 'stock_quantity' => 5, - 'in_stock' => true, - ]); + $product = Product::factory() + ->withStocks(10) + ->create(); - $product->decreaseStock(5); + $product->decreaseStock(10); - $this->assertFalse($product->fresh()->in_stock); + $this->assertFalse($product->fresh()->isInStock()); } } diff --git a/tests/Unit/ProductPricingTest.php b/tests/Unit/ProductPricingTest.php index 3385d24..dea31d3 100644 --- a/tests/Unit/ProductPricingTest.php +++ b/tests/Unit/ProductPricingTest.php @@ -25,13 +25,16 @@ class ProductPricingTest extends TestCase public function it_returns_sale_price_when_on_sale() { $product = Product::factory() - ->withPrices(2, 100) - ->onSale( - sale_price: 80, - sale_start: now()->subDay(), - sale_end: now()->addDay(), - ) - ->create(); + ->withPrices(1, 100) + ->create([ + 'sale_start' => now()->subDay(), + 'sale_end' => now()->addDay(), + ]); + + $price = $product->prices()->first(); + $price->sale_unit_amount = 80; + + $price->save(); $this->assertEquals(80, $product->getCurrentPrice()); } @@ -39,11 +42,19 @@ class ProductPricingTest extends TestCase /** @test */ public function it_returns_regular_price_when_sale_has_ended() { - $product = Product::factory()->create([ - 'regular_price' => 100, - 'sale_price' => 80, - 'sale_start' => now()->subDays(7), - 'sale_end' => now()->subDay(), + $product = Product::factory()->withPrices(1, 100)->create([ + 'sale_start' => now()->subWeek(), + 'sale_end' => now()->addHour(), + ]); + + $price = $product->prices()->first(); + $price->sale_unit_amount = 80; + $price->save(); + + $this->assertEquals(80, $product->getCurrentPrice()); + + $product->update([ + 'sale_end' => now()->subHour(), ]); $this->assertEquals(100, $product->getCurrentPrice()); @@ -52,26 +63,21 @@ class ProductPricingTest extends TestCase /** @test */ public function it_returns_regular_price_when_sale_hasnt_started() { - $product = Product::factory()->create([ - 'regular_price' => 100, - 'sale_price' => 80, + $product = Product::factory()->withPrices(1, 100)->create([ 'sale_start' => now()->addDay(), 'sale_end' => now()->addWeek(), ]); - $this->assertEquals(100, $product->getCurrentPrice()); - } + $price = $product->prices()->first(); + $price->sale_unit_amount = 80; + $price->save(); - /** @test */ - public function it_calculates_discount_percentage() - { - $product = Product::factory()->create([ - 'regular_price' => 100, - 'sale_price' => 75, + $this->assertEquals(100, $product->getCurrentPrice()); + + $product->update([ + 'sale_start' => now()->subHour(), ]); - $discount = (($product->regular_price - $product->sale_price) / $product->regular_price) * 100; - - $this->assertEquals(25, $discount); + $this->assertEquals(80, $product->getCurrentPrice()); } } diff --git a/tests/Unit/StockManagementTest.php b/tests/Unit/StockManagementTest.php index 1a0ec5c..b89637c 100644 --- a/tests/Unit/StockManagementTest.php +++ b/tests/Unit/StockManagementTest.php @@ -25,9 +25,7 @@ class StockManagementTest extends TestCase /** @test */ public function it_detects_sufficient_stock() { - $product = Product::factory()->create([ - 'manage_stock' => true, - 'stock_quantity' => 50, + $product = Product::factory()->withStocks(50)->create([ 'low_stock_threshold' => 10, ]);