Skip to content

Commit 4ab895a

Browse files
committed
Add unit tests for product tier prices
1 parent 061bc37 commit 4ab895a

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/Models/Product.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,13 @@ public function getUnitPrice(int $quantity = 1, int $customerGroup = 0)
241241
// NOTE: We always need the option with the lowest matching value, *not* the one with the highest matching qty!
242242
// It wouldn't make sense to select a tier with a higher qty if the price is higher.
243243

244-
return $tierPrice ?? $this->price;
244+
$price = $tierPrice ?? $this->price;
245+
246+
if ($this->specialPrice && $this->specialPrice < $price) {
247+
return $this->specialPrice;
248+
}
249+
250+
return $price;
245251
}
246252

247253
public function getPrice(int $quantity = 1, int $customerGroup = 0)

src/Models/ProductTierPrice.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class ProductTierPrice extends Model
1313

1414
protected $primaryKey = 'value_id';
1515

16+
protected $guarded = [];
17+
1618
public function product(): BelongsTo
1719
{
1820
return $this->belongsTo(

tests/Unit/ProductPriceTest.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,50 @@ public function product_catalog_price_rules_apply()
5555
#[Test]
5656
public function product_can_have_tier_prices()
5757
{
58-
$this->assertTrue(true);
59-
// TODO: Base DB doesn't have tier prices. Test this some other way.
58+
$product = Product::find(4);
59+
60+
$product->tierPrices()->create(['qty' => 5, 'value' => 40, 'website_id' => 1, 'all_groups' => 1, 'customer_group_id' => 0]);
61+
$product->tierPrices()->create(['qty' => 10, 'value' => 35, 'website_id' => 1, 'all_groups' => 1, 'customer_group_id' => 0]);
62+
$product->tierPrices()->create(['qty' => 15, 'value' => 40, 'website_id' => 1, 'all_groups' => 1, 'customer_group_id' => 0]);
63+
64+
$product->tierPrices()->create(['qty' => 1, 'value' => 43, 'website_id' => 1, 'all_groups' => 0, 'customer_group_id' => 2]);
65+
$product->tierPrices()->create(['qty' => 5, 'value' => 39, 'website_id' => 1, 'all_groups' => 0, 'customer_group_id' => 2]);
66+
$product->tierPrices()->create(['qty' => 15, 'value' => 34, 'website_id' => 1, 'all_groups' => 0, 'customer_group_id' => 2]);
67+
68+
$product->tierPrices()->create(['qty' => 20, 'value' => 30, 'website_id' => 0, 'all_groups' => 1, 'customer_group_id' => 0]);
69+
70+
// All groups
71+
$this->assertEquals(45 * 4, $product->getPrice(4), 'Tier price for all groups, qty 4');
72+
$this->assertEquals(40 * 5, $product->getPrice(5), 'Tier price for all groups, qty 5');
73+
$this->assertEquals(35 * 12, $product->getPrice(12), 'Tier price for all groups, qty 12');
74+
$this->assertEquals(35 * 15, $product->getPrice(15), 'Tier price for all groups, qty 15');
75+
$this->assertEquals(30 * 23, $product->getPrice(23), 'Tier price for all groups, qty 23');
76+
77+
// Specifically customer group 2
78+
$this->assertEquals(43 * 4, $product->getPrice(4, 2), 'Tier price for group 2, qty 4');
79+
$this->assertEquals(39 * 5, $product->getPrice(5, 2), 'Tier price for group 2, qty 5');
80+
$this->assertEquals(35 * 12, $product->getPrice(12, 2), 'Tier price for group 2, qty 12');
81+
$this->assertEquals(34 * 15, $product->getPrice(15, 2), 'Tier price for group 2, qty 15');
82+
$this->assertEquals(30 * 22, $product->getPrice(22, 2), 'Tier price for group 2, qty 22');
83+
84+
// This is technically not valid, but without creating a whole new website this is the best we can do in a unit test.
85+
config()->set('rapidez.website', 0);
86+
$this->assertEquals(45 * 15, $product->getPrice(15, 2), 'Tier price on website 0 for group 2, qty 15');
87+
$this->assertEquals(45 * 15, $product->getPrice(15), 'Tier price on website 0 for all groups, qty 15');
88+
$this->assertEquals(30 * 21, $product->getPrice(21), 'Tier price on website 0 for all groups, qty 21');
89+
}
90+
91+
#[Test]
92+
public function product_can_combine_tier_prices_and_special_price()
93+
{
94+
$product = Product::find(10);
95+
96+
$product->tierPrices()->create(['qty' => 5, 'value' => 30, 'website_id' => 1, 'all_groups' => 1, 'customer_group_id' => 0]);
97+
$product->tierPrices()->create(['qty' => 10, 'value' => 20, 'website_id' => 1, 'all_groups' => 1, 'customer_group_id' => 0]);
98+
99+
// All groups
100+
$this->assertEquals(24 * 4, $product->getPrice(4), 'Tier price (should be special price) for all groups, qty 4');
101+
$this->assertEquals(24 * 5, $product->getPrice(5), 'Tier price (should be special price) for all groups, qty 5');
102+
$this->assertEquals(20 * 12, $product->getPrice(12), 'Tier price for all groups, qty 12');
60103
}
61104
}

0 commit comments

Comments
 (0)