long-blade / shopify-plugin
此包已被弃用且不再维护。未建议替代包。
CakePHP 的 Shopify 插件
v1.2.4
2021-06-10 14:32 UTC
Requires
- cakephp/cakephp: ^4.0
Requires (Dev)
- phpunit/phpunit: ^8.0
README
一个与 Shopify API 交互的 shopify 插件
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
安装 composer 包的推荐方法是
$ composer require long-blade/shopify-plugin
最后,您需要将插件 加载 到项目中!
// src/Application.php in bootstrap() method $this->addPlugin('Shopify');
站点对象
为了与插件交互,您需要创建一个包含最小必要数据的站点对象。
use Shopify\Model\Site; $siteId = 0; // a unique id (it can be the id of the entry in a database table) $site = new Site('hostname', 'apiKey', 'apiPassword', $siteId);
此对象在每个资源类构造函数中被注入。
use Shopify\Model\Site; $site = new Site('hostname', 'apiKey', 'apiPassword', 0); // Then use this object for any resource, for example: use Shopify\Resource\Products; $products = new Products($site);
##资源
###商店
use Shopify\Model\Site; $site = new Site('hostname', 'apiKey', 'apiPassword', 0); use Shopify\Resource\Shop; $shop = new Shop($site); // Then we perform a simple get request by chaining the getResource method $shopInfo = $shop->getResource();
此资源可让您检索有关商店的信息,但不能更新任何信息。
响应将是一个数组。 商店属性
use Shopify\Resource\Products; $productsResource = new Products($site);
如何获取产品信息
//Retrieves a list of products $products = $productsResource->getResource(); //Retrieves a single product $product = $productsResource->getById(0102040506); //Retrieves a list of product variants $product = $productsResource->getVariantsForProduct(0102040506);
如何创建新产品
$productData = [ 'title' => 'This is a product title', 'description' => 'This is a product description' ]; $product = new \Shopify\Model\Product($productData); //Creates a new product by passing the product object created. $productsResource->post($product);
您还可以将产品查询的响应导出为 json 文件,如下所示
// $productsResource = new Products($site); $productsResource->export(['products' => [...]]);
这将把响应对象导出到在 Shopify/config/bootstrap.php
文件中指定的位置的一个 json 文件
[ 'export' => [ 'path' => RESOURCES . 'json' . DS, 'file_type' => 'json', 'lifetime' => 60 * 60 * 3, // The lifetime which a file is consider to be old ], ];
###库存
库存水平代表特定位置上库存项目的可用数量。
$inventory = [ 'location_id' => 987654321, // The ID of the location that the inventory level belongs to. 'inventory_item_id' => 123456789, // The ID of the inventory item that the inventory level belongs to. 'available' => 10 // The quantity of inventory items available for sale. ]; $inventory = new \Shopify\Model\Inventory($inventory); // Create the entity; $inventoryResource = new \Shopify\Resource\InventoryLevels($site); $inventoryResource->setInventory($inventory);
如何设置产品的可用性
$inventory = [ 'location_id' => 987654321, // The ID of the location that the inventory level belongs to. 'inventory_item_id' => 123456789, // The ID of the inventory item that the inventory level belongs to. 'available_adjustment' => 5 // The amount to adjust the available inventory quantity. Send negative values to subtract from the current available quantity. ]; $inventory = new \Shopify\Model\Inventory($inventory); // Create the entity; $inventoryResource = new \Shopify\Resource\InventoryLevels($site); $inventoryResource->adjustInventory($inventory);
如何调整单个位置上库存项目的可用数量5个单位
###订单
$orders = new Shopify\Resource\Orders($site); // All orders $allOrdersWithoutPagination = $orders->getPaginatedResource(); // All orders since Id: $ordersSinsId= $orders->getOrdersSinceId(3692914802743);
订单是客户完成的对商店中一个或多个产品购买请求。
如何检索订单列表
###智能收藏夹
use Shopify\Resource\SmartCollections; $smrtCollections = new SmartCollections($site); // This will return an array containing all the collections a product belongs to. $collection = $smrtCollections->get(['product_id' => '12345678'])->getResource('smart_collections');