Skip to content

Commit f06d561

Browse files
committed
Always take the lowest price
1 parent 4ab895a commit f06d561

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/Models/Product.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
88
use Illuminate\Database\Eloquent\Relations\HasMany;
99
use Illuminate\Database\Eloquent\Relations\HasOne;
10+
use Illuminate\Support\Arr;
1011
use Illuminate\Support\Collection;
1112
use Rapidez\Core\Facades\Rapidez;
1213
use Rapidez\Core\Models\Relations\BelongsToManyCallback;
@@ -241,13 +242,9 @@ public function getUnitPrice(int $quantity = 1, int $customerGroup = 0)
241242
// NOTE: We always need the option with the lowest matching value, *not* the one with the highest matching qty!
242243
// It wouldn't make sense to select a tier with a higher qty if the price is higher.
243244

244-
$price = $tierPrice ?? $this->price;
245+
$prices = [$tierPrice, $this->price, $this->specialPrice];
245246

246-
if ($this->specialPrice && $this->specialPrice < $price) {
247-
return $this->specialPrice;
248-
}
249-
250-
return $price;
247+
return min(Arr::whereNotNull($prices));
251248
}
252249

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

tests/Unit/ProductPriceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function product_can_have_tier_prices()
5757
{
5858
$product = Product::find(4);
5959

60+
$product->tierPrices()->create(['qty' => 2, 'value' => 50, 'website_id' => 1, 'all_groups' => 1, 'customer_group_id' => 0]);
6061
$product->tierPrices()->create(['qty' => 5, 'value' => 40, 'website_id' => 1, 'all_groups' => 1, 'customer_group_id' => 0]);
6162
$product->tierPrices()->create(['qty' => 10, 'value' => 35, 'website_id' => 1, 'all_groups' => 1, 'customer_group_id' => 0]);
6263
$product->tierPrices()->create(['qty' => 15, 'value' => 40, 'website_id' => 1, 'all_groups' => 1, 'customer_group_id' => 0]);

0 commit comments

Comments
 (0)