Skip to content

Commit 82f79fd

Browse files
committed
Add doc
1 parent ec68bc5 commit 82f79fd

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

docs/content/en/frontend/tags.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ From £50.00
5353
£4.99
5454
```
5555

56+
#### product-price hook
57+
58+
This tag provides a [Hook](https://statamic.dev/extending/hooks) to modify the price and currency. For example, if you wanted to format the price using PHP's NumberFormatter:
59+
60+
```php
61+
\StatamicRadPack\Shopify\Tags\Shopify::hook('product-price', function ($payload, $next) {
62+
$formatter = new \NumberFormatter(\Statamic\Facades\Site::current()->locale(), \NumberFormatter::CURRENCY);
63+
64+
$payload->price = $formatter->formatCurrency((float) $payload->price, 'EUR');
65+
66+
return $next($payload);
67+
});
68+
```
69+
5670
## Product Variants
5771

5872
You can interact with the variants in several ways. In the demo theme we output this automatically, but you may want to drill down deeper.

src/Tags/Shopify.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace StatamicRadPack\Shopify\Tags;
44

5+
use NumberFormatter;
56
use Shopify\Clients\Rest;
67
use Statamic\Extensions\Pagination\LengthAwarePaginator;
78
use Statamic\Facades\Entry;
9+
use Statamic\Facades\Site;
810
use Statamic\Facades\User;
911
use Statamic\Support\Arr;
1012
use Statamic\Support\Str;
@@ -63,10 +65,23 @@ public function productPrice()
6365

6466
$price = $pricePluck->sort()->first();
6567

66-
$payload = $this->runHooksWith('product-price', [
68+
$payload = [
6769
'currency' => config('shopify.currency'),
6870
'price' => $price,
69-
]);
71+
];
72+
73+
$this->runHooksWith('shopify-price', $payload);
74+
75+
// try to use currency
76+
if (($currencyCode = config('shopify.currency_code')) && class_exists('NumberFormatter')) {
77+
try {
78+
$formatter = new \NumberFormatter(Site::current()->locale(), \NumberFormatter::CURRENCY);
79+
80+
$price = $formatter->formatCurrency((float) $price, $currencyCode);
81+
82+
$currencyString = ''; // blank this as its included in price
83+
} catch (\Throwable $e) { }
84+
}
7085

7186
if ($pricePluck->count() > 1 && $this->params->get('show_from') === true) {
7287
return __('shopify::messages.display_price_from', ['currency' => $payload->currency, 'price' => $payload->price]);

0 commit comments

Comments
 (0)