dan / shopify
Shopify API with Laravel integrations using latest Guzzle.
Installs: 25 248
Dependents: 0
Suggesters: 0
Security: 0
Stars: 24
Watchers: 7
Forks: 9
Open Issues: 1
Type:project
Requires
- php: >=8.0
- ext-json: *
- guzzlehttp/guzzle: ^6.2|^7.0
- illuminate/http: ^8.0|^9.0|^10.0
- nesbot/carbon: ^1.26.3 || ^2.0
Requires (Dev)
- orchestra/testbench: ^6.24
- phpunit/phpunit: ^9.5
- dev-master
- v5.0.0
- v4.0.8
- v4.0.7
- v4.0.6
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v3.0.2
- v3.0.1
- v3.0.0
- v2.9.8
- v2.9.7
- v2.9.5
- v2.9.4
- v2.9.3
- v2.9.1
- v2.9.0
- v2.8.0
- v2.7.3
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.0
- v2.4.0
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.3
- v2.2.2
- v2.2.1
- v2.1.13
- v2.1.12
- v2.1.11
- v2.1.10
- v2.1.9
- v2.1.8
- v2.1.7
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.0
- v1.0.0.11
- v1.0.0.10
- v1.0.0.9
- v1.0.0.8
- v1.0.0.7
- v1.0.0.6
- v1.0.0.5
- v1.0.0.4
- v1.0.0.3
- v1.0.0.2
- v1.0.0.1
- v1.0.0.0
- dev-add_fs_to_406
- dev-add-recurring-application-charges
- dev-diogo/feature/change-http-client
- dev-metafields
This package is auto-updated.
Last update: 2024-10-26 20:43:13 UTC
README
A fluent and object-oriented approach for using the Shopify API.
Supported Objects / Endpoints:
- Asset
- Customer
- Dispute
- Fulfillment
- FulfillmentService
- Image
- Metafield
- Order
- Product
- Risk
- Shop
- Theme
- Variant
- Webhook
Versions
Composer
composer require dan/shopify
Basic Usage
The APIs all function alike, here is an example of usage of the products API.
$api = Dan\Shopify\Shopify::make($shop = 'shop-name.myshopify.com', $token = 'shpua_abc123'); // Shop information $api->shop(); // array dictionary // List of products $api->products->get(); // array of array dictionaries // Attach query parameters to a get request $api->products->get(['created_at_min' => '2023-03-25']); // array of array dictionaries // A specific product $api->products('123456789')->get(); // array dictionary // Get all variants for a product $api->products('123456789')->variants->get(); // array of array dictionaries // Get a specific variant for a specific product $s->api2()->products('123456789')->variants('567891234')->get(); // array dictionary // Append URI string to a get request $api->orders('123456789')->get([], 'risks'); // array dictionary // Create a product. // See https://shopify.dev/docs/api/admin-rest/2023-01/resources/product#post-products $api->products->post(['title' => 'Simple Test']); // array dictionary // Update something specific on a product $api->products('123456789')->put(['title' => 'My title changed.']); // array dictionary
Basic (very basic) GraphQL
The collection and model utilities that are available ->find(...)
and ->findMany(...)
for RESTful endpoints are NOT available for GraphQL.
Some endpoints are only available through Shopify's GraphQL library. This makes me sad because GraphQL is not as readable or intuitive as RESTful APIs, less people understand it, and it's harder to train people on. That said, if you want to jam out with your graphql, there is a client method to assist you.
For example, fetch delivery profiles (only available in GraphQL).
Note: You can safely use the
graphql(...)
helper method without any concern of changing the state on theDan\Shopify\Shopify::class
.
$query = "{ deliveryProfiles (first: 3) { edges { node { id, name, } } } }" $api->graphql($query); // hipster
Using cursors
Shopify doesn't jam with regular old pagination, sigh ...
As of the 2019-10
API version, Shopify has removed per page pagination on their busiest endpoints.
With the deprecation of the per page pagination comes a new cursor based pagination.
You can use the next
method to get paged responses.
Example usage:
// First call to next can have all the usual query params you might want. $api->orders->next(['limit' => 100, 'status' => 'closed'); // Further calls will have all query params preset except for limit. $api->orders->next(['limit' => 100]);
Metafields!
There are multiple endpoints in the Shopify API that have support for metafields.
In effort to support them all, this API has been updated to allow chaining ->metafields
from any endpoint.
This won't always work as not every endpoint supports metafields, and any endpoint that doesn't support metafields will result in a 404
.
Below are examples of all the endpoints that support metafields.
// Get our API $api = Dan\Shopify\Shopify::make($shop, $token); // Store metafields $api->metafields->get(); // Metafields on an Order $api->orders($order_id)->metafields->get(); // Metafields on a Product $api->products($product_id)->metafields->get(); // Metafields on a Variant $api->products($product_id)->variants($variant_id)->metafields->get(); // Metafields on a Customer $api->customers($customer_id)->metafields->get(); // Metafields can also be updated like all other endpoints $api->products($product_id)->metafields($metafield_id)->put($data);
Usage with Laravel
Single Store App
In your config/app.php
Add the following to your providers
array:
Requires for private app (env token) for single store usage of oauth (multiple stores)
Dan\Shopify\Integrations\Laravel\ShopifyServiceProvider::class,
Add the following to your aliases
array:
If your app only interacts with a single store, there is a Facade that may come in handy.
'Shopify' => Dan\Shopify\Integrations\Laravel\ShopifyFacade::class,
For facade usage, replace the following variables in your .env
SHOPIFY_DOMAIN=your-shop-name.myshopify.com SHOPIFY_TOKEN=your-token-here
Optionally replace following variables in your .env
Empty or admin
defaults to oldest supported API, learn more
SHOPIFY_API_BASE="admin/api/2022-07"
Using the Facade gives you Dan\Shopify\Shopify
It will be instantiated with your shop and token you set up in
config/shopify.php
Review the Basic Usage
above, using the Facade is more or less the same, except you're only interacting with the one store in your config.
// Facade same as $api->shop(), but for just the one store. Shopify::shop(); // Facade same as $api->products->get(), but for just the one store. Shopify::products()->get(); // Facade same as $api->products('123456789')->get(), but for just the one store. Shopify::products('123456789')->get();
Oauth Apps
Making a public app using oauth, follow the Shopify docs to make your auth url, and use the following helper to retrieve your access token using the code from your callback.
Get a token for a redirect response.
Shopify::getAppInstallResponse( 'your_app_client_id', 'your_app_client_secret', 'shop_from_request', 'code_from_request' ); // returns (object) ['access_token' => '...', 'scopes' => '...']
Verify App Hmac (works for callback or redirect)
Dan\Shopify\Util::validAppHmac( 'hmac_from_request', 'your_app_client_secret', ['shop' => '...', 'timestamp' => '...', ...] );
Verify App Webhook Hmac
Dan\Shopify\Util::validWebhookHmac( 'hmac_from_request', 'your_app_client_secret', file_get_contents('php://input') );
Contributors
Todo
- Artisan Command to create token
License
MIT.