Skip to content

Commit

Permalink
Add metafields service, fix orderFields (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
robwittman authored Aug 10, 2019
1 parent fda68c2 commit e0b0bf9
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Enum/Fields/MetafieldFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class MetafieldFields extends AbstractObjectEnum
const DESCIRPTION = 'description';
const ID = 'id';
const KEY = 'key';
const NAMESPACE = 'namespace';
const METAFIELD_NAMESPACE = 'namespace';
const OWNER_ID = 'owner_id';
const OWNER_RESOURCE = 'owner_resource';
const VALUE = 'value';
Expand Down
6 changes: 5 additions & 1 deletion src/Enum/Fields/OrderFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class OrderFields extends AbstractObjectEnum
const CUSTOMER_LOCALE = 'customer_locale';
const DISCOUNT_CODES = 'discount_codes';
const EMAIL = 'email';
const CONTACT_EMAIL = 'contact_email';
const FINANCIAL_STATUS = 'financial_status';
const FULFILLMENTS = 'fulfillments';
const FULFILLMENT_STATUS = 'fulfillment_status';
Expand Down Expand Up @@ -50,6 +51,7 @@ class OrderFields extends AbstractObjectEnum
const TOTAL_DISCOUNTS = 'total_discounts';
const TOTAL_LINE_ITEMS_PRICE = 'total_line_items_price';
const TOTAL_PRICE = 'total_price';
const TOTAL_PRICE_USD = 'total_price_usd';
const TOTAL_TAX = 'total_tax';
const TOTAL_WEIGHT = 'total_weight';
const UPDATED_AT = 'updated_at';
Expand All @@ -64,10 +66,11 @@ public function getFieldTypes()
'browser_ip' => 'string',
'buyer_accepts_marketing' => 'boolean',
'cancel_reason' => 'string',
'cancelled_at' => 'boolean',
'cancelled_at' => 'DateTime',
'cart_token' => 'string',
'client_details' => 'object',
'closed_at' => 'DateTime',
'contact_email' => 'string',
'created_at' => 'DateTime',
'currency' => 'string',
'customer' => 'Customer',
Expand Down Expand Up @@ -105,6 +108,7 @@ public function getFieldTypes()
'total_discounts' => 'string',
'total_line_items_price' => 'string',
'total_price' => 'string',
'total_price_usd' => 'string',
'total_tax' => 'string',
'total_weight' => 'string',
'updated_at' => 'DateTime',
Expand Down
113 changes: 113 additions & 0 deletions src/Service/MetafieldService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Shopify\Service;

use Shopify\Object\Metafield;

class MetafieldService extends AbstractService
{
/**
* Retrieve a list of metafields that belong to a resource
*
* @link https://help.shopify.com/api/reference/metafield#index
* @param array $params
* @param string $ownerResource
* @param string $ownerId
* @return Metafield[]
*/
public function all(array $params = array(), $ownerResource = '', $ownerId = '')
{
if ($ownerResource && $ownerId) {
$endpoint = '/admin/' . $ownerResource . '/' . $ownerId . '/metafields.json';
} else {
$endpoint = '/admin/metafields.json';
}
$response = $this->request($endpoint, 'GET', $params);
return $this->createCollection(Metafield::class, $response['metafields']);
}
/**
* Receive a count of all metafields
*
* @link https://help.shopify.com/api/reference/metafield#count
* @param array $params
* @return integer
*/
public function count(array $params = array())
{
$endpoint = '/admin/metafields/count.json';
$response = $this->request($endpoint, 'GET', $params);
return $response['count'];
}
/**
* Receive a single metafield
*
* @link https://help.shopify.com/api/reference/metafield#show
* @param integer $metafieldId
* @param array $fields
* @return Metafield
*/
public function get($metafieldId, array $fields = array())
{
$params = array();
if (!empty($fields)) {
$params['fields'] = $fields;
}
$endpoint = '/admin/metafields/'.$metafieldId.'.json';
$response = $this->request($endpoint, 'GET', $params);
return $this->createObject(Metafield::class, $response['metafield']);
}
/**
* Create a new metafield for a resource
*
* @link https://help.shopify.com/api/reference/metafield#create
* @param Metafield $metafield
* @param string $ownerResource
* @param string $ownerId
* @return void
*/
public function create(Metafield &$metafield, $ownerResource = '', $ownerId = '')
{
$data = $metafield->exportData();
if ($ownerResource && $ownerId) {
$endpoint = '/admin/' . $ownerResource . '/' . $ownerId . '/metafields.json';
} else {
$endpoint = '/admin/metafields.json';
}
$response = $this->request(
$endpoint, 'POST', array(
'metafield' => $data
)
);
$metafield->setData($response['metafield']);
}
/**
* Modify an existing metafield
*
* @link https://help.shopify.com/api/reference/metafield#update
* @param Metafield $metafield
* @return void
*/
public function update(Metafield &$metafield)
{
$data = $metafield->exportData();
$endpoint = '/admin/metafields/'.$metafield->id.'.json';
$response = $this->request(
$endpoint, 'PUT', array(
'metafield' => $data
)
);
$metafield->setData($response['metafield']);
}
/**
* Remove a metafield
*
* @link https://help.shopify.com/api/reference/metafield#destroy
* @param Metafield $metafield
* @return void
*/
public function delete(Metafield &$metafield)
{
$endpoint = '/admin/metafields/'.$metafield->id.'.json';
$this->request($endpoint, 'DELETE');
}
}

0 comments on commit e0b0bf9

Please sign in to comment.