-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathHasProductPricing.php
More file actions
37 lines (30 loc) · 1.05 KB
/
HasProductPricing.php
File metadata and controls
37 lines (30 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
declare(strict_types=1);
namespace App\Traits;
use App\DTO\PriceData;
use Shopper\Core\Helpers\Price;
trait HasProductPricing
{
public function getPrice(): ?PriceData
{
$currencyCode = current_currency();
$price = $this->prices
->map(function ($price) use ($currencyCode) {
$price->amount = is_no_division_currency($currencyCode)
? $price->amount
: $price->amount * 100;
return $price;
})
->reject(fn ($price) => $price->currency->code !== $currencyCode)
->first();
return $price
? new PriceData(
amount: Price::from($price->amount, $currencyCode),
compare: $price->compare_amount ? Price::from($price->compare_amount, $currencyCode) : null,
percentage: $price->compare_amount > 0
? round((($price->compare_amount - $price->amount) / $price->compare_amount) * 100)
: null
)
: null;
}
}