Skip to content

Commit 537520c

Browse files
authored
Add product-price hook (#286)
1 parent 4b7526f commit 537520c

3 files changed

Lines changed: 62 additions & 6 deletions

File tree

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: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Statamic\Facades\User;
99
use Statamic\Support\Arr;
1010
use Statamic\Support\Str;
11+
use Statamic\Support\Traits\Hookable;
1112
use Statamic\Tags\Concerns\GetsRedirects;
1213
use Statamic\Tags\Concerns\OutputsItems;
1314
use Statamic\Tags\Concerns\QueriesConditions;
@@ -17,7 +18,7 @@
1718

1819
class Shopify extends Tags
1920
{
20-
use GetsRedirects, OutputsItems, QueriesConditions, QueriesOrderBys, RendersForms;
21+
use GetsRedirects, Hookable, OutputsItems, QueriesConditions, QueriesOrderBys, RendersForms;
2122

2223
/**
2324
* @return string|array
@@ -52,8 +53,6 @@ public function productPrice()
5253
return null;
5354
}
5455

55-
$html = '';
56-
5756
// Out of Stock
5857
if (! $this->isInStock($variants)) {
5958
return __('shopify::messages.out_of_stock');
@@ -62,13 +61,18 @@ public function productPrice()
6261
// Lowest Price
6362
$pricePluck = $variants->pluck('price');
6463

65-
$price = $pricePluck->sort()->splice(0, 1)[0];
64+
$price = $pricePluck->sort()->first();
65+
66+
$payload = $this->runHooksWith('product-price', [
67+
'currency' => config('shopify.currency'),
68+
'price' => $price,
69+
]);
6670

6771
if ($pricePluck->count() > 1 && $this->params->get('show_from') === true) {
68-
return __('shopify::messages.display_price_from', ['currency' => config('shopify.currency'), 'price' => $price]);
72+
return __('shopify::messages.display_price_from', ['currency' => $payload->currency, 'price' => $payload->price]);
6973
}
7074

71-
return __('shopify::messages.display_price', ['currency' => config('shopify.currency'), 'price' => $price]);
75+
return __('shopify::messages.display_price', ['currency' => $payload->currency, 'price' => $payload->price]);
7276
}
7377

7478
/**

tests/Unit/TagsTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Shopify\Clients\Rest;
88
use Shopify\Clients\RestResponse;
99
use Statamic\Facades;
10+
use StatamicRadPack\Shopify\Tags\Shopify;
1011
use StatamicRadPack\Shopify\Tests\TestCase;
1112

1213
class TagsTest extends TestCase
@@ -81,6 +82,43 @@ public function outputs_product_price()
8182

8283
}
8384

85+
#[Test]
86+
public function runs_hooks_on_product_price()
87+
{
88+
$product = Facades\Entry::make()->data([
89+
'title' => 'Obi wan',
90+
'vendor' => 'Kenobe',
91+
'slug' => 'obi-wan',
92+
'product_id' => 1,
93+
])
94+
->collection('products');
95+
96+
$product->save();
97+
98+
$variant = Facades\Entry::make()->data([
99+
'title' => 'T-shirt',
100+
'slug' => 'obi-wan-tshirt',
101+
'sku' => 'obi-wan-tshirt',
102+
'product_slug' => 'obi-wan',
103+
'price' => 1999.99,
104+
'inventory_quantity' => 10,
105+
'inventory_policy' => 'deny',
106+
'inventory_management' => 'shopify',
107+
])
108+
->collection('variants');
109+
110+
$variant->save();
111+
112+
Shopify::hook('product-price', function ($payload, $next) {
113+
$payload->price = '10.00';
114+
$payload->currency = '$';
115+
116+
return $next($payload);
117+
});
118+
119+
$this->assertEquals('$10.00', $this->tag('{{ shopify:product_price }}', ['slug' => 'obi-wan']));
120+
}
121+
84122
#[Test]
85123
public function outputs_in_stock()
86124
{

0 commit comments

Comments
 (0)