Please note: the new version using Guzzle 6.2 is actively maintained here
An object-oriented approach towards using the Shopify API.
- CustomCollection
- Discount - Shopify Plus
- Fulfillment - via Order
- FulfillmentEvent - via Order
- Metafield
- Order
- OrderRisk - via Order
- Product
- ProductImage
- ProductVariant
- Webhook
$ composer require dan/shopify-api v0.9.9.*
// Assumes setup of client with access token.
$mgr = ShopifyApi\Manager::init($shop, $token);
$mgr->getProduct($product_id = 123); // returns ShopifyApi/Models/Product
// Alternatively, we may call methods on the API object.
$mgr->api('products')->show($product_id = 123); // returns array
See Facade usages for other methods available.
In your config/app.php
ShopifyApi\Providers\ShopifyServiceProvider::class,
'Shopify' => ShopifyApi\Support\ShopifyFacade::class,
SHOPIFY_DOMAIN=your-shop-name.myshopify.com
SHOPIFY_TOKEN=your-token-here
Methods called on Manager
will cascade down onto Client
via the __call
method.
If you're using Laravel,
Models
will return\Illuminate\Support\Collection
instead ofarray
.
Shopify::getProduct($product_id = 123); // returns ShopifyApi/Models/Product
Shopify::getAllProducts(); // returns Collection|array of ShopifyApi/Models/Product
Shopify::getVariant($variant_id = 456); // returns ShopifyApi/Models/Variant
Shopify::getAllVariants($product_id = 123); // returns Collection|array of ShopifyApi/Models/Variant
Shopify::getOrder($order_id = 789); // returns ShopifyApi/Models/Order
Shopify::getAllOrders(); // returns a Collection|array of ShopifyApi/Models/Order
Shopify::getMetafield($metafield_id = 123); // returns ShopifyApi/Models/Metafield
Shopify::getDiscount($dicount_id = 123); // returns ShopifyApi/Models/Discount
Shopify::getAllDiscounts(); // returns Collection|array of ShopifyApi/Models/Discount
Shopify::getAllWebhooks(); // returns Collection|array of ShopifyApi/Models/Webhook
// Alternatively, we may call methods on the API object.
Shopify::api('products')->show($product_id = 123); // returns array
Shopify::api('products')->all(); // returns array
Shopify::api('products')->count(); // returns int
Shopify::api('variants')->show($variant_id = 456); // returns array
Shopify::api('variants')->product($product_id = 123)->all(); // returns array
Shopify::api('orders')->show($order_id = 123); // returns array
Shopify::api('orders')->all(); // returns array
Shopify::api('orders')->count(); // returns int
Shopify::api('custom_collections')->show($cc_id = 123); // returns array
Shopify::api('custom_collections')->all(); // returns array
Shopify::api('custom_collections')->count(); // returns int
Shopify::api('discounts')->show($discount_id = 123); // returns array
Shopify::api('discounts')->all(); // returns array
Shopify::api('discounts')->count(); // returns int
Shopify::api('webhooks')->show($webhook_id = 123); // returns array
Shopify::api('webhooks')->all(); // returns array
Shopify::api('webhooks')->count(); // returns int
$product = Shopify::getProduct();
$product->setTitle('Burton Custom Freestyle 151');
$product->setBodyHtml('<strong>Good snowboard!<\/strong>');
$product->setVendor('Burton');
$product->setProductType('Snowboard');
$product->setTags(['Barnes & Noble', 'John\'s Fav', '"Big Air"']);
$product->save();
$product = Shopify::getProduct(123);
$product->setTitle('Burton Freestyle 152');
$product->save();
$collection = Shopify::getCustomCollection(123);
$collection->add(456);
or
$collection = Shopify::getCustomCollection(123);
$collection->add([456,789]);
// The 'value_type' property will be determined automatically if omitted
$product->createMetafield('in_stock', 'inventory', ['value' => 123]);
$product->updateMetafield('in_stock', 'inventory', ['value' => 122]);
$product->updateOrCreateMetafield('in_stock', 'inventory', ['value' => 200]);
// Support is included for arrays and objects (json encodable) and null
$product->createMetafield('board_specs', 'criteria', ['value' => new MyJsonSerializableObject()]);
Shopify::getAppInstallResponse(
'your_app_client_id',
'your_app_client_secret',
'shop_from_request',
'code_from_request'
);
// returns (object) ['access_token' => '...', 'scopes' => '...']
ShopifyApi\Util::validAppHmac(
'hmac_from_request',
'your_app_client_secret',
['shop' => '...', 'timestamp' => '...', ...]
);
ShopifyApi\Util::validWebhookHmac(
'hmac_from_request',
'your_app_client_secret',
file_get_contents('php://input')
);
If you want to sprinkle a little caching in, setup a service provider and extend the \ShopifyApi\Providers\ShopifyServiceProvider
.
<?php
namespace App\Providers;
use App;
use ShopifyApi\Manager;
use ShopifyApi\Models\Product;
use ShopifyApi\Providers\ShopifyServiceProvider as BaseServiceProvider;
/**
* Class ShopifyServiceProvider
*/
class ShopifyServiceProvider extends BaseServiceProvider
{
/**
* Register the application services.
*
* @return void
*/
public function register()
{
parent::register();
/** @var Manager $shopify */
$shopify = app('shopify');
$shopify->setApiCache(Product::class, function($client, $params = null) {
// No caching for collections.
if (is_array($params)) {
// Returning falsy will result in the default api behavior.
return null;
}
$key = "shopify_product_".((string) $params);
// Assuming you Cache::put($key, $product->getData()); elsewhere
// If the cache is empty, the resource will be fetched from the api
// as normal.
$data = Cache::get($key);
return $data ? new Product($client, $data) : null;
});
}
}
This repository's structure was modeled after the robust cdaguerre/php-trello-api
.
- Migrate from
guzzle/guzzle
toguzzlehttp/guzzle
, bump version. - Publish files for Laravel setup
- Artisan Command to create token
MIT.