spatie / spatie-price-api
The Price API used at promotional sites for our own products
Requires
- php: ^8.2
- guzzlehttp/guzzle: ^7.2
- illuminate/contracts: ^10.0|^11.0|^12.0|^13.0
- illuminate/http: ^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0|^11.0
- phpunit/phpunit: ^10.5|^11.0
- spatie/phpunit-snapshot-assertions: ^5.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
This package can retrieve prices from the API at spatie.be. It is used at the promotional sites for our own products. Though it is open source, the package is not intended to be used by third parties.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/spatie-price-api
Usage
You can get a pricing information using the Spatie\PriceApi\SpatiePriceApi::getPriceForPurchasable() method.
Fetching prices in the browser
The price depends on the country of the visitor. When prices are fetched on the server, a page can't be cached at the edge. Instead, you can let the browser of the visitor fetch the price from the spatie.be API.
Add the scripts to your layout:
{{ \Spatie\PriceApi\SpatiePriceApi::scripts() }}
This adds two Alpine data components: spatiePrice($purchasableId) and spatieBundlePrice($bundleId).
<div x-data="spatiePrice(20)" x-init="init()"> <template x-if="discount.active"> <p> <span x-text="discount.name"></span> ending in <span x-text="countdown.days"></span> days <span x-text="countdown.hours"></span> hours <span x-text="countdown.minutes"></span> minutes </p> </template> <s x-show="discount.active" x-text="priceWithoutDiscount"></s> <span x-text="couldFetchPrice ? price : '-'">-</span> </div>
These properties are available:
loaded: whether the request to the API has finishedcouldFetchPrice: whether a price could be fetchedpriceandpriceWithoutDiscount: the formatted price, for example149 EURdiscount: an object withactive,percentage,nameandexpiresAt(a unix timestamp)countdown: an object withdays,hours,minutesandsecondsuntil the discount expires, updated every secondresponse: the raw response of the API
Alpine v3 calls init() automatically. On Alpine v2, add x-init="init()". Calling it twice is harmless. Components for the same product share a single request.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.