diff --git a/README.md b/README.md index e823a6c..6590562 100644 --- a/README.md +++ b/README.md @@ -526,7 +526,7 @@ The custom methods are specific to some resources which may not be available for $fo = $client->Order("1234567890")->FulfillmentOrder()->get(); // Requesting assigned fulfillment orders (with status fulfillment_requested) -$shopify->AssignedFulfillmentOrder()->get(["assignment_status" => "fulfillment_requested"]); +$fo = $client->AssignedFulfillmentOrder()->get(["assignment_status" => "fulfillment_requested"]); // Creating a FulfilmentRequest // Follow instructions to get partial fulfilments diff --git a/lib/AssignedFulfillmentOrder.php b/lib/AssignedFulfillmentOrder.php index 108983d..a9d26a0 100644 --- a/lib/AssignedFulfillmentOrder.php +++ b/lib/AssignedFulfillmentOrder.php @@ -13,4 +13,9 @@ class AssignedFulfillmentOrder extends ShopifyResource * @inheritDoc */ protected $resourceKey = 'assigned_fulfillment_order'; + + /** + * @inheritDoc + */ + protected $dataKey = 'fulfillment_orders'; } \ No newline at end of file diff --git a/lib/ShopifyResource.php b/lib/ShopifyResource.php index dd3b2cb..ac926c9 100644 --- a/lib/ShopifyResource.php +++ b/lib/ShopifyResource.php @@ -64,6 +64,13 @@ abstract class ShopifyResource */ protected $childResource = array(); + /** + * If the dataKey is different from the resourceKey (base key name used in API results) + * + * @var boolean + */ + protected $dataKey = null; + /** * If search is enabled for the resource * @@ -309,6 +316,21 @@ protected function getResourcePath() return $this->pluralizeKey(); } + /** + * Get the working dataKey for the resource (if any) + * + * For situations where the resource returns results using a different keyname. + * (e.g. AssignedFulfillmentOrder responds using 'fulfillment_orders') + */ + protected function getDataKey(string $dataKey = null) + { + // Passthrough + if ($dataKey) return $dataKey; + + // Return the dataKey if set + return $this->dataKey ?: ($this->id ? $this->resourceKey : $this->pluralizeKey()); + } + /** * Generate the custom url for api request based on the params and custom action (if any) * @@ -342,10 +364,7 @@ public function get($urlParams = array(), $url = null, $dataKey = null) $response = HttpRequestJson::get($url, $this->httpHeaders); - if (!$dataKey) $dataKey = $this->id ? $this->resourceKey : $this->pluralizeKey(); - - return $this->processResponse($response, $dataKey); - + return $this->processResponse($response, $this->getDataKey($dataKey)); } /** @@ -540,6 +559,8 @@ public function processResponse($responseArray, $dataKey = null) throw new ApiException($message, CurlRequest::$lastHttpCode); } + $dataKey = $this->getDataKey($dataKey); + if ($dataKey && isset($responseArray[$dataKey])) { return $responseArray[$dataKey]; } else {