forked from aligent/bigcommerce-v3-api-php-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.php
197 lines (173 loc) · 5.08 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace BigCommerce\ApiV3;
use BigCommerce\ApiV3\Api\Carts\CartsApi;
use BigCommerce\ApiV3\Api\Catalog\CatalogApi;
use BigCommerce\ApiV3\Api\Channels\ChannelsApi;
use BigCommerce\ApiV3\Api\Orders\OrdersApi;
use BigCommerce\ApiV3\Api\Customers\CustomersApi;
use BigCommerce\ApiV3\Api\Payments\PaymentsProcessingApi;
use BigCommerce\ApiV3\Api\PriceLists\PriceListsApi;
use BigCommerce\ApiV3\Api\Redirects\RedirectsApi;
use BigCommerce\ApiV3\Api\Scripts\ScriptsApi;
use BigCommerce\ApiV3\Api\Sites\SitesApi;
use BigCommerce\ApiV3\Api\Themes\ThemesApi;
use BigCommerce\ApiV3\Api\Widgets\WidgetsApi;
use BigCommerce\ApiV3\Api\CustomTemplateAssociations\CustomTemplateAssociationsApi;
/**
* The parent API class
*
* ## Usage Examples
*
* Trivial example of updating a product name:
*
* ```php
* $api = new BigCommerce\ApiV3\Client($_ENV['hash'], $_ENV['CLIENT_ID'], $_ENV['ACCESS_TOKEN']);
*
* $product = $api->catalog()->product(123)->get()->getProduct();
* $product->name = 'Updated product name';
* try {
* $api->catalog()->product($product->id)->update($product);
* } catch (\Psr\Http\Client\ClientExceptionInterface $exception) {
* echo "Unable to update product: {$exception->getMessage()}";
* }
* ```
*
* Fetching all visible products (all pages of products):
*
* ```php
* $api = new BigCommerce\ApiV3\Client($_ENV['hash'], $_ENV['CLIENT_ID'], $_ENV['ACCESS_TOKEN']);
*
* $productsResponse = $api->catalog()->products()->getAllPages(['is_visible' => true]);
*
* echo "Found {$productsResponse->getPagination()->total} products";
*
* $products = $productsResponse->getProducts();
* ```
*
* Example of updating a product variant
*
* ```php
* $api = new BigCommerce\ApiV3\Client($_ENV['hash'], $_ENV['CLIENT_ID'], $_ENV['ACCESS_TOKEN']);
*
* $productVariant = $api->catalog()->product(123)->variant(456)->get()->getProductVariant();
* $productVariant->price = '12';
*
* try {
* $api->catalog()->product($productVariant->product_id)->variant($productVariant->id)->update($productVariant);
* } catch (\Psr\Http\Client\ClientExceptionInterface $exception) {
* echo "Unable to update product variant: {$exception->getMessage()}";
* }
* ```
*
* Example of creating a product variant
*
* ```php
* $api = new BigCommerce\ApiV3\Client($_ENV['hash'], $_ENV['CLIENT_ID'], $_ENV['ACCESS_TOKEN']);
*
* $productVariant = new \BigCommerce\ApiV3\ResourceModels\Catalog\Product\ProductVariant();
* $productVariant->product_id = 123;
* $productVariant->sku = "SKU-123";
* //...
*
* try {
* $api->catalog()->product($productVariant->product_id)->variants()->create($productVariant);
* } catch (\Psr\Http\Client\ClientExceptionInterface $exception) {
* echo "Unable to create product variant: {$exception->getMessage()}";
* }
* ```
*
*/
class Client extends BaseApiClient
{
public const API_URI = 'https://api.bigcommerce.com/stores/%s/v3/';
public function catalog(): CatalogApi
{
return new CatalogApi($this);
}
public function customers(): CustomersApi
{
return new CustomersApi($this);
}
public function priceLists(): PriceListsApi
{
return new PriceListsApi($this);
}
public function priceList(int $priceListId): PriceListsApi
{
return new PriceListsApi($this, $priceListId);
}
public function themes(): ThemesApi
{
return new ThemesApi($this);
}
public function theme(string $uuid): ThemesApi
{
$api = $this->themes();
$api->setUuid($uuid);
return $api;
}
public function carts(): CartsApi
{
return new CartsApi($this);
}
public function cart(string $uuid): CartsApi
{
$api = $this->carts();
$api->setUuid($uuid);
return $api;
}
public function order(int $orderId): OrdersApi
{
return new OrdersApi($this, $orderId);
}
public function payments(): PaymentsProcessingApi
{
return new PaymentsProcessingApi($this);
}
public function script(string $uuid): ScriptsApi
{
$api = $this->scripts();
$api->setUuid($uuid);
return $api;
}
public function scripts(): ScriptsApi
{
return new ScriptsApi($this);
}
public function widgets(): WidgetsApi
{
return new WidgetsApi($this);
}
public function content(): WidgetsApi
{
return $this->widgets();
}
public function customTemplateAssociations(): CustomTemplateAssociationsApi
{
return new CustomTemplateAssociationsApi($this);
}
public function channel(int $id): ChannelsApi
{
return new ChannelsApi($this, $id);
}
public function channels(): ChannelsApi
{
return new ChannelsApi($this);
}
public function redirects(): RedirectsApi
{
return new RedirectsApi($this);
}
public function sites(): SitesApi
{
return new SitesApi($this);
}
public function site(int $id): SitesApi
{
return new SitesApi($this, $id);
}
protected function defaultBaseUrl(): string
{
return self::API_URI;
}
}