Skip to content

Commit e0b0bf9

Browse files
authored
Add metafields service, fix orderFields (#32)
1 parent fda68c2 commit e0b0bf9

File tree

3 files changed

+119
-2
lines changed

3 files changed

+119
-2
lines changed

src/Enum/Fields/MetafieldFields.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class MetafieldFields extends AbstractObjectEnum
88
const DESCIRPTION = 'description';
99
const ID = 'id';
1010
const KEY = 'key';
11-
const NAMESPACE = 'namespace';
11+
const METAFIELD_NAMESPACE = 'namespace';
1212
const OWNER_ID = 'owner_id';
1313
const OWNER_RESOURCE = 'owner_resource';
1414
const VALUE = 'value';

src/Enum/Fields/OrderFields.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class OrderFields extends AbstractObjectEnum
1919
const CUSTOMER_LOCALE = 'customer_locale';
2020
const DISCOUNT_CODES = 'discount_codes';
2121
const EMAIL = 'email';
22+
const CONTACT_EMAIL = 'contact_email';
2223
const FINANCIAL_STATUS = 'financial_status';
2324
const FULFILLMENTS = 'fulfillments';
2425
const FULFILLMENT_STATUS = 'fulfillment_status';
@@ -50,6 +51,7 @@ class OrderFields extends AbstractObjectEnum
5051
const TOTAL_DISCOUNTS = 'total_discounts';
5152
const TOTAL_LINE_ITEMS_PRICE = 'total_line_items_price';
5253
const TOTAL_PRICE = 'total_price';
54+
const TOTAL_PRICE_USD = 'total_price_usd';
5355
const TOTAL_TAX = 'total_tax';
5456
const TOTAL_WEIGHT = 'total_weight';
5557
const UPDATED_AT = 'updated_at';
@@ -64,10 +66,11 @@ public function getFieldTypes()
6466
'browser_ip' => 'string',
6567
'buyer_accepts_marketing' => 'boolean',
6668
'cancel_reason' => 'string',
67-
'cancelled_at' => 'boolean',
69+
'cancelled_at' => 'DateTime',
6870
'cart_token' => 'string',
6971
'client_details' => 'object',
7072
'closed_at' => 'DateTime',
73+
'contact_email' => 'string',
7174
'created_at' => 'DateTime',
7275
'currency' => 'string',
7376
'customer' => 'Customer',
@@ -105,6 +108,7 @@ public function getFieldTypes()
105108
'total_discounts' => 'string',
106109
'total_line_items_price' => 'string',
107110
'total_price' => 'string',
111+
'total_price_usd' => 'string',
108112
'total_tax' => 'string',
109113
'total_weight' => 'string',
110114
'updated_at' => 'DateTime',

src/Service/MetafieldService.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace Shopify\Service;
4+
5+
use Shopify\Object\Metafield;
6+
7+
class MetafieldService extends AbstractService
8+
{
9+
/**
10+
* Retrieve a list of metafields that belong to a resource
11+
*
12+
* @link https://help.shopify.com/api/reference/metafield#index
13+
* @param array $params
14+
* @param string $ownerResource
15+
* @param string $ownerId
16+
* @return Metafield[]
17+
*/
18+
public function all(array $params = array(), $ownerResource = '', $ownerId = '')
19+
{
20+
if ($ownerResource && $ownerId) {
21+
$endpoint = '/admin/' . $ownerResource . '/' . $ownerId . '/metafields.json';
22+
} else {
23+
$endpoint = '/admin/metafields.json';
24+
}
25+
$response = $this->request($endpoint, 'GET', $params);
26+
return $this->createCollection(Metafield::class, $response['metafields']);
27+
}
28+
/**
29+
* Receive a count of all metafields
30+
*
31+
* @link https://help.shopify.com/api/reference/metafield#count
32+
* @param array $params
33+
* @return integer
34+
*/
35+
public function count(array $params = array())
36+
{
37+
$endpoint = '/admin/metafields/count.json';
38+
$response = $this->request($endpoint, 'GET', $params);
39+
return $response['count'];
40+
}
41+
/**
42+
* Receive a single metafield
43+
*
44+
* @link https://help.shopify.com/api/reference/metafield#show
45+
* @param integer $metafieldId
46+
* @param array $fields
47+
* @return Metafield
48+
*/
49+
public function get($metafieldId, array $fields = array())
50+
{
51+
$params = array();
52+
if (!empty($fields)) {
53+
$params['fields'] = $fields;
54+
}
55+
$endpoint = '/admin/metafields/'.$metafieldId.'.json';
56+
$response = $this->request($endpoint, 'GET', $params);
57+
return $this->createObject(Metafield::class, $response['metafield']);
58+
}
59+
/**
60+
* Create a new metafield for a resource
61+
*
62+
* @link https://help.shopify.com/api/reference/metafield#create
63+
* @param Metafield $metafield
64+
* @param string $ownerResource
65+
* @param string $ownerId
66+
* @return void
67+
*/
68+
public function create(Metafield &$metafield, $ownerResource = '', $ownerId = '')
69+
{
70+
$data = $metafield->exportData();
71+
if ($ownerResource && $ownerId) {
72+
$endpoint = '/admin/' . $ownerResource . '/' . $ownerId . '/metafields.json';
73+
} else {
74+
$endpoint = '/admin/metafields.json';
75+
}
76+
$response = $this->request(
77+
$endpoint, 'POST', array(
78+
'metafield' => $data
79+
)
80+
);
81+
$metafield->setData($response['metafield']);
82+
}
83+
/**
84+
* Modify an existing metafield
85+
*
86+
* @link https://help.shopify.com/api/reference/metafield#update
87+
* @param Metafield $metafield
88+
* @return void
89+
*/
90+
public function update(Metafield &$metafield)
91+
{
92+
$data = $metafield->exportData();
93+
$endpoint = '/admin/metafields/'.$metafield->id.'.json';
94+
$response = $this->request(
95+
$endpoint, 'PUT', array(
96+
'metafield' => $data
97+
)
98+
);
99+
$metafield->setData($response['metafield']);
100+
}
101+
/**
102+
* Remove a metafield
103+
*
104+
* @link https://help.shopify.com/api/reference/metafield#destroy
105+
* @param Metafield $metafield
106+
* @return void
107+
*/
108+
public function delete(Metafield &$metafield)
109+
{
110+
$endpoint = '/admin/metafields/'.$metafield->id.'.json';
111+
$this->request($endpoint, 'DELETE');
112+
}
113+
}

0 commit comments

Comments
 (0)