Skip to content

Commit 70eeeb2

Browse files
authored
Merge pull request #19 from alozytskyi/feature/inventory
Support InventoryItem and InventoryLevel
2 parents fc90ce3 + ec927f5 commit 70eeeb2

File tree

6 files changed

+85
-9
lines changed

6 files changed

+85
-9
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ Some resources are available directly, some resources are only available through
254254
- [Event](https://help.shopify.com/api/reference/event/)
255255
- [FulfillmentService](https://help.shopify.com/api/reference/fulfillmentservice)
256256
- [GiftCard](https://help.shopify.com/api/reference/gift_card) _(Shopify Plus Only)_
257+
- [InventoryItem](https://help.shopify.com/api/reference/inventoryitem)
258+
- [InventoryLevel](https://help.shopify.com/api/reference/inventorylevel)
257259
- [Location](https://help.shopify.com/api/reference/location/) _(read only)_
258260
- [Metafield](https://help.shopify.com/api/reference/metafield)
259261
- [Multipass](https://help.shopify.com/api/reference/multipass) _(Shopify Plus Only, API not available yet)_
@@ -358,6 +360,14 @@ The custom methods are specific to some resources which may not be available for
358360
- [search()](https://help.shopify.com/api/reference/gift_card#search)
359361
Search for gift cards matching supplied query
360362

363+
- InventoryLevel ->
364+
- [adjust($data)](https://help.shopify.com/api/reference/inventorylevel#adjust)
365+
Adjust inventory level.
366+
- [connect($data)](https://help.shopify.com/api/reference/inventorylevel#connect)
367+
Connect an inventory item to a location.
368+
- [set($data)](https://help.shopify.com/api/reference/inventorylevel#set)
369+
Set an inventory level for a single inventory item within a location.
370+
361371
- Order ->
362372
- [close()](https://help.shopify.com/api/reference/order#close)
363373
Close an Order

lib/HttpRequestJson.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,4 @@ protected static function processResponse($response)
139139
return json_decode($response, true);
140140
}
141141

142-
}
142+
}

lib/InventoryItem.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* @author Arsenii Lozytskyi <[email protected]>
4+
* Created at 04/15/18 02:25 PM UTC+03:00
5+
*
6+
* @see https://help.shopify.com/api/reference/inventoryitem
7+
*/
8+
9+
namespace PHPShopify;
10+
11+
class InventoryItem extends ShopifyResource
12+
{
13+
/** @inheritDoc */
14+
protected $resourceKey = 'inventory_item';
15+
}

lib/InventoryLevel.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* @author Arsenii Lozytskyi <[email protected]>
4+
* Created at 04/15/18 02:31 PM UTC+03:00
5+
*
6+
* @see https://help.shopify.com/api/reference/inventorylevel
7+
*/
8+
9+
namespace PHPShopify;
10+
11+
/**
12+
* Class InventoryLevel
13+
*
14+
* @method array adjust($data) Adjust inventory level.
15+
* @method array connect($data) Connect an inventory item to a location.
16+
* @method array set($data) Sets an inventory level for a single inventory item within a location.
17+
*/
18+
class InventoryLevel extends ShopifyResource
19+
{
20+
/** @inheritDoc */
21+
protected $resourceKey = 'inventory_level';
22+
23+
/** @inheritDoc */
24+
protected $customPostActions = [
25+
'adjust',
26+
'connect',
27+
'set',
28+
];
29+
30+
protected $customPostActionsNoWrap = [
31+
'adjust',
32+
'connect',
33+
'set',
34+
];
35+
}

lib/ShopifyResource.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ abstract class ShopifyResource
4848

4949
/**
5050
* List of child Resource names / classes
51-
*
51+
*
5252
* If any array item has an associative key => value pair, value will be considered as the resource name
5353
* (by which it will be called) and key will be the associated class name.
5454
*
@@ -96,6 +96,16 @@ abstract class ShopifyResource
9696
protected $customPutActions = array();
9797
protected $customDeleteActions = array();
9898

99+
/**
100+
* Some actions from inventory levels do not wrap the content of the request
101+
*
102+
* Wrapping such request body leads to errors. This list is used to disable this wrapping
103+
* on certain methods.
104+
*
105+
* @var array
106+
*/
107+
protected $customPostActionsNoWrap = array();
108+
99109
/**
100110
* The ID of the resource
101111
*
@@ -225,7 +235,7 @@ public function __call($name, $arguments)
225235

226236
$url = $this->generateUrl($urlParams, $customAction);
227237

228-
return $this->$httpMethod($dataArray, $url);
238+
return $this->$httpMethod($dataArray, $url, !in_array($name, $this->customPostActionsNoWrap));
229239
}
230240
}
231241

@@ -355,18 +365,22 @@ public function search($query)
355365
/**
356366
* Call POST method to create a new resource
357367
*
358-
* @param array $dataArray Check Shopify API reference of the specific resource for the list of required and optional data elements to be provided
368+
* @param array $dataArray Check Shopify API reference of the specific resource for the list of required and optional data elements to be provided
359369
* @param string $url
360370
*
361-
* @uses HttpRequestJson::post() to send the HTTP request
371+
* @param bool $wrapBody
362372
*
363373
* @return array
374+
* @throws ApiException
375+
* @throws CurlException
376+
* @uses HttpRequestJson::post() to send the HTTP request
377+
*
364378
*/
365-
public function post($dataArray, $url = null)
379+
public function post($dataArray, $url = null, $wrapBody = true)
366380
{
367381
if (!$url) $url = $this->generateUrl();
368382

369-
if (!empty($dataArray)) $dataArray = $this->wrapData($dataArray);
383+
if ($wrapBody && !empty($dataArray)) $dataArray = $this->wrapData($dataArray);
370384

371385
$response = HttpRequestJson::post($url, $dataArray, $this->httpHeaders);
372386

@@ -498,4 +512,4 @@ public function processResponse($responseArray, $dataKey = null)
498512
return $responseArray;
499513
}
500514
}
501-
}
515+
}

lib/ShopifySDK.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ class ShopifySDK
102102
'Event',
103103
'FulfillmentService',
104104
'GiftCard',
105+
'InventoryItem',
106+
'InventoryLevel',
105107
'Location',
106108
'Metafield',
107109
'Multipass',
@@ -296,4 +298,4 @@ public static function checkApiCallLimit($firstCallWait = false)
296298

297299
static::$microtimeOfLastApiCall = microtime(true);
298300
}
299-
}
301+
}

0 commit comments

Comments
 (0)