From ffa8209ca31432c82801556f6564bda6a94c708b Mon Sep 17 00:00:00 2001 From: naveenrajbu Date: Fri, 3 Sep 2021 20:30:43 +0530 Subject: [PATCH 01/12] Update Client.php Basic Auth credentials doesn't support anymore --- src/Bigcommerce/Api/Client.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 00eb60fa..5af28b32 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -72,14 +72,17 @@ class Client * * Accepts OAuth and (for now!) Basic Auth credentials * + * configure Basic Auth credentials doesn't support anymore + * * @param array $settings + * @throws Exception */ public static function configure($settings) { if (isset($settings['client_id'])) { self::configureOAuth($settings); } else { - self::configureBasicAuth($settings); + throw new Exception("'store_hash' must be provided"); } } @@ -219,11 +222,12 @@ private static function connection() { if (!self::$connection) { self::$connection = new Connection(); - if (self::$client_id) { - self::$connection->authenticateOauth(self::$client_id, self::$auth_token); - } else { - self::$connection->authenticateBasic(self::$username, self::$api_key); - } + self::$connection->authenticateOauth(self::$client_id, self::$auth_token); +// if (self::$client_id) { +// self::$connection->authenticateOauth(self::$client_id, self::$auth_token); +// } else { +// self::$connection->authenticateBasic(self::$username, self::$api_key); +// } } return self::$connection; From 12197fa005f83053d30436a8aa0bdf289a3ed1d6 Mon Sep 17 00:00:00 2001 From: naveenrajbu Date: Fri, 3 Sep 2021 20:32:58 +0530 Subject: [PATCH 02/12] Update Client.php Basic Auth credentials doesn't support anymore --- src/Bigcommerce/Api/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 5af28b32..272279ed 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -72,7 +72,7 @@ class Client * * Accepts OAuth and (for now!) Basic Auth credentials * - * configure Basic Auth credentials doesn't support anymore + * configure using Basic Auth credentials doesn't support anymore * * @param array $settings * @throws Exception From 43a81a75934148bbbece6acd88c5f148030f4497 Mon Sep 17 00:00:00 2001 From: naveenrajbu Date: Fri, 3 Sep 2021 20:37:45 +0530 Subject: [PATCH 03/12] Update Client.php Basic Auth credentials doesn't support anymore --- src/Bigcommerce/Api/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 272279ed..5bbd294d 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -82,7 +82,7 @@ public static function configure($settings) if (isset($settings['client_id'])) { self::configureOAuth($settings); } else { - throw new Exception("'store_hash' must be provided"); + throw new Exception("'client_id' must be provided"); } } From eb7f4d87a820080331780a9300ae2570195c9b91 Mon Sep 17 00:00:00 2001 From: naveenrajbu Date: Sun, 5 Sep 2021 16:43:04 +0530 Subject: [PATCH 04/12] Updated Structure and added 'v3' for Cart and Catalog/brands. --- .gitignore | 3 +- README.md | 310 ++++++++++++++++ composer.json | 9 +- src/Bigcommerce/Api/Client.php | 446 ++++++++++++++++++++++-- src/Bigcommerce/Api/Connection.php | 2 + src/Bigcommerce/Api/Error.php | 4 + src/Bigcommerce/Api/Resources/Brand.php | 18 +- src/Bigcommerce/Api/Resources/Cart.php | 60 ++++ test/Unit/Api/ClientErrorTest.php | 2 +- test/Unit/Api/ConnectionTest.php | 2 +- test/Unit/Api/ErrorTest.php | 2 +- test/Unit/Api/FilterTest.php | 2 +- test/Unit/Api/NetworkErrorTest.php | 2 +- test/Unit/Api/ServerErrorTest.php | 2 +- 14 files changed, 814 insertions(+), 50 deletions(-) create mode 100644 src/Bigcommerce/Api/Resources/Cart.php diff --git a/.gitignore b/.gitignore index c820964b..84a06467 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ vendor -composer.lock +/composer.lock *.sh script/* @@ -10,3 +10,4 @@ script/* .env /.php_cs /.php_cs.cache +/test.php diff --git a/README.md b/README.md index 78453583..8ab29901 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,315 @@ into the scope with the following namespace declaration: use Bigcommerce\Api\Client as Bigcommerce; ~~~ +V3 Update - *NEW +--------- +This update is on the development with `Backward Compatibility` and can be easily customised on future version releases. Feel free to add more features and create issues. + +`configureBasicAuth` is Completely removed now you can only configure using `auth_token, client_id and store_hash` + +Now you can set the version on Configuration and can be overridden anywhere in the code. +~~~php +Bigcommerce::configure(array( + 'client_id' => 'xxxxxxxxxxxxxxxxxxxxxx', + 'auth_token' => 'xxxxxxxxxxxxxxxxxxxxxxx', + 'store_hash' => 'xxxxxxxxx', + 'version' => 'v3' //optional By Default set as 'v2' +)); + +//If you don't want to set version by default, you can always set it in the method. + +$brands = Bigcommerce::getBrands([],"v3"); + +foreach($brands as $brand){ + echo $brand->name."\n"; +} +~~~ +As of now, Only `Carts, Wishlists and Catlalog\brands support 'v3'` other APIs are still in development will be added here once it is completed, Meanwhile `You can still use 'v2' features without any issues`. + +Set 'v3' by default if you're only using 'v3' APIs + +'v3' methods has `$version` parameter which can be used if you didn't set version 'v3' as default version. + +##Carts(V3) + +you can do almost all the functions in cart. + +**Get Cart by Cart Id**: `getCart($id, $version = null);` +* $id = String Cart Id +* $version = (Optional) String "v2", "v3", .. +~~~php +Bigcommerce::configure(array( + 'client_id' => 'xxxxxxxxxxxxxxxxxxxxxx', + 'auth_token' => 'xxxxxxxxxxxxxxxxxxxxxxx', + 'store_hash' => 'xxxxxxxxx', + 'version' => 'v3' +)); +$cart = Bigcommerce::getCart(); + +echo $cart->id; + +//for this documentation, I'll use the above example +//$version variable available for only methods that use 'v3', you can use older functions as it was without '$version' variable + + +//or + +Bigcommerce::configure(array( + 'client_id' => 'xxxxxxxxxxxxxxxxxxxxxx', + 'auth_token' => 'xxxxxxxxxxxxxxxxxxxxxxx', + 'store_hash' => 'xxxxxxxxx' +)); + +$cart = Bigcommerce::getCart("v3"); + +echo $cart->id; +~~~ +**Create Cart**: `createCart($object, $version = null);` +* $object = Array | Object +* $version = (Optional) String "v2", "v3", .. +~~~php +$cart = array( + "customer_id" => 1, + "line_items" => array( + array( + "quantity" => 1, + "product_id" => 1, + "variant_id" => 2 + ) + ) +); + +Bigcommerce::createCart($cart); //or Bigcommerce::createCart($cart,"v3"); + +//or + +$cart = new Bigcommerce\Api\Resources\Cart(); +$cart->customer_id = 1; +$cart->line_items = array( + array( + "quantity" => 1, + "product_id" => 1, + "variant_id" => 2 + ) +); +$cart->create(); // CartObject->create($version = 'v3'); $version (Optional) +~~~ +**Update Cart**: `updateCartCustomerId($cart_id, $customer_id, $version = null);` + +Note: Only `Customer Id` can be updated by update cart api +* $cart_id = String Cart Id +* $customer_id = Int Customer Id +* $version = (Optional) String "v2", "v3", .. + ~~~php +Bigcommerce::updateCartCustomerId("xxxxxxxxx",1); + + //or + + $cart = Bigcommerce::getCart("xxxxxxxxxxx"); + $cart->update(41) // CartObject->update($customer_id, $version = 'v3'); $version (Optional) + ~~~ +**Delete Cart**: `deleteCart($cart_id, $version = null);` +* $cart_id = String Cart Id +~~~php +Bigcommerce::deleteCart("xxxxxxxxx",1); + + //or + + $cart = Bigcommerce::getCart("xxxxxxxxxxx"); + $cart->delete() // CartObject->delete($version = 'v3'); $version (Optional) + ~~~ + +**Add Cart Items**: `createCartLineItems($id, $object, $filter = array(), $version = null);` +* $id = String Cart Id +* $object = Array|Object +* $filter = (Optional) Array Example ['include'=>'redirect_urls'] +~~~php +$items = array( + "line_items" => array( + array( + "quantity" => 1, + "product_id" => 1, + "variant_id" => 2 + ), + array( + "quantity" => 1, + "product_id" => 2, + "variant_id" => 3 + ) + ) +); + +Bigcommerce::createCartLineItems("xxxxxxxxx",$items); + + //or + + $cart = Bigcommerce::getCart("xxxxxxxxxxx"); + $cart->addItems($items) // CartObject->addItems($items, $filter = array(), $version = 'v3'); $filter, $version (Optional) + ~~~ + +**Update Cart Item**: `updateCartLineItem($cart_id, $line_item_id, $object, $filter = array(), $version = null);` +* $cart_id = String Cart Id +* $line_item_id = String Line Item Id +* $object = Array|Object +* $filter = (Optional) Array Example ['include'=>'redirect_urls'] +~~~php +$item = array( + "line_items" => array( + "quantity" => 1, + "product_id" => 1, + "variant_id" => 2 + ) +); + +Bigcommerce::updateCartLineItem("xxxxxxxxx","xxxxxxxxx",$item); + ~~~ + +**Delete Cart Item**: `deleteCartLineItem($cart_id, $line_item_id, $version = null);` +* $cart_id = String Cart Id +* $line_item_id = String Line Item Id +~~~php +Bigcommerce::deleteCartLineItem("xxxxxxxxx","xxxxxxxxx"); + ~~~ + +##Brands (V2 and V3) +you can use both 'v2' and 'v3' in Brands and I'm trying to do the same for all new versions. + +**Get All Brands**: `getBrands($filter = array(), $version = null);` +* $filter = Array filter options refer Bigcommerce documentation for more. +* $version = (Optional) String "v2", "v3", .. +~~~php +Bigcommerce::configure(array( + 'client_id' => 'xxxxxxxxxxxxxxxxxxxxxx', + 'auth_token' => 'xxxxxxxxxxxxxxxxxxxxxxx', + 'store_hash' => 'xxxxxxxxx' +)); +// By default version will be 'v2' +// API url will be https://api.bigcommerce.com/stores/{store_hash}/v2/brands +$brands = Bigcommerce::getBrands(); + +//or + +Bigcommerce::configure(array( + 'client_id' => 'xxxxxxxxxxxxxxxxxxxxxx', + 'auth_token' => 'xxxxxxxxxxxxxxxxxxxxxxx', + 'store_hash' => 'xxxxxxxxx', + 'version' => 'v3' \\ Optional +)); + +// API url will be https://api.bigcommerce.com/stores/{store_hash}/v3/catalog/brands +$brands = Bigcommerce::getBrands([],"v3"); +~~~ +**Get Brand by Brand Id:** `getBrand($id, $version = null);` +* $id = Int Brand Id. +* $version = (Optional) String "v2", "v3", .. +~~~php +$brand = Bigcommerce::getBrand(1); +//or +$brand = Bigcommerce::getBrand(1,"v3"); + +echo $brand->name; +~~~ +**Create Brand:** `createBrand($object, $version = null);` +* $object = Array|Object API Payload. +* $version = (Optional) String "v2", "v3", .. +~~~php +$brand = array( + "name" => "test" +); +$brand = Bigcommerce::createBrand($brand,'v3'); +//or +$brand = new Bigcommerce\Api\Resources\Brand(); +$brand->name = "test"; +$brand->create(); // BrandObject->create($version = null); $version (Optional) +~~~ +**Update Brand:** `createBrand($id, $object, $version = null);` +* $id = Int Brand Id. +* $object = Array|Object +* $version = (Optional) String "v2", "v3", .. +~~~php +$brand = array( + "name" => "test" +); +$brand = Bigcommerce::updateBrand(1, $brand, 'v3'); +//or +$brand = Bigcommerce::getBrand(1); +$brand->name = "test"; +$brand->update(); // BrandObject->update($version = null); $version (Optional) +~~~ +**Delete Brand:** `deleteBrand($id, $version = null);` +* $id = Int Brand Id. +* $version = (Optional) String "v2", "v3", .. +~~~php +Bigcommerce::deleteBrand(1); +//or +$brand = Bigcommerce::getBrand(1); +$brand->delete(); // BrandObject->delete($version = null); $version (Optional) +~~~ +**Delete All Brand:** `deleteAllBrands($version = null);` +* $version = (Optional) String "v2", "v3", .. +~~~php +Bigcommerce::deleteAllBrands(); +~~~ +**Get All Brand Meta Fields (Only on 'v3'):** `getBrandMetafields($id, $filter = array(), $version = null);` +* $id = Int Brand Id +* $filter = (Optional) Array|Object +* $version = (Optional) String "v2", "v3", .. +~~~php +Bigcommerce::getBrandMetafields(1, array(), 'v3'); +~~~ +**Get Brand Meta Field by Id (Only on 'v3'):** `getBrandMetafield($brand_id, $metafield_id, $filter = array(), $version = null);` +* $brand_id = Int Brand Id +* $metafield_id = Int Brand Meta Field Id +* $filter = (Optional) Array|Object +* $version = (Optional) String "v2", "v3", .. +~~~php +Bigcommerce::getBrandMetafield(1, 1, array(), 'v3'); +~~~ +**Create Brand Meta Field (Only on 'v3'):** `createBrandMetafield($id, $object, $version = null);` +* $id = Int Brand Id +* $object = Array|Object +* $version = (Optional) String "v2", "v3", .. +~~~php +$metaField = array( + "permission_set" => "app_only", + "namespace" => "App Namespace", + "key" => "location_id", + "value" => "Shelf 3, Bin 5", +); + +Bigcommerce::createBrandMetafield(1, $metaField, 'v3'); +~~~ + +**Update Brand Meta Field (Only on 'v3'):** `updateBrandMetafield($brand_id, $metafield_id, $object, $version = null);` +* $brand_id = Int Brand Id +* $metafield_id = Int Brand Meta Field Id +* $object = Array|Object +* $version = (Optional) String "v2", "v3", .. +~~~php +$metaField = array( + "permission_set" => "app_only", + "namespace" => "App Namespace", + "key" => "location_id", + "value" => "Shelf 3", +); + +Bigcommerce::updateBrandMetafield(1, 1, $metaField, 'v3'); +~~~ + +**Delete Brand Meta Field (Only on 'v3'):** `updateBrandMetafield($brand_id, $metafield_id, $version = null);` +* $brand_id = Int Brand Id +* $metafield_id = Int Brand Meta Field Id +* $version = (Optional) String "v2", "v3", .. +~~~php +Bigcommerce::deleteBrandMetafield(1, 1, 'v3'); +~~~ + +That's all for now. I'll update for other APIs Continuously. **Feel free to Pull and Merge for other APIs** + +I'll publish this repo on composer for easy Installation + +**You can use all the features and Methods Below** + Configuration ------------- @@ -90,6 +399,7 @@ Bigcommerce::configure(array( ~~~ ### Basic Auth (deprecated) +**Update - Totally Removed** ~~~php Bigcommerce::configure(array( 'store_url' => 'https://store.mybigcommerce.com', diff --git a/composer.json b/composer.json index 52c15bbb..c1567d14 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "bigcommerce/api", + "name": "naveenrajbu/bigcommerce-api-php-v3", "type": "library", "description": "Enables PHP applications to communicate with the Bigcommerce API.", "keywords": [ @@ -15,6 +15,11 @@ { "name": "Bigcommerce", "homepage": "http://www.bigcommerce.com" + }, + { + "name": "Naveen Raj", + "homepage": "https://github.com/naveenrajbu/", + "email": "nvenrj@gmail.com" } ], "require": { @@ -29,7 +34,7 @@ }, "autoload": { "psr-0": { - "Bigcommerce": "src/" + "Bigcommerce\\Api": "src/" } }, "autoload-dev": { diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 5bbd294d..ec6136cc 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -45,10 +45,18 @@ class Client */ private static $resource; + /** + * version name + * + * @var string + */ + public static $version; + /** * API path prefix to be added to store URL for requests * * @var string + * @deprecated */ private static $path_prefix = '/api/v2'; @@ -62,9 +70,10 @@ class Client private static $store_hash; private static $auth_token; private static $client_secret; - private static $stores_prefix = '/stores/%s/v2'; + private static $stores_prefix = '/stores/%s/'; private static $api_url = 'https://api.bigcommerce.com'; private static $login_url = 'https://login.bigcommerce.com'; + private static $available_versions = array("v2","v3"); /** * Configure the API client with the required settings to access @@ -96,7 +105,7 @@ public static function configure($settings) * - store_hash * * @param array $settings - * @throws \Exception + * @throws Exception */ public static function configureOAuth($settings) { @@ -114,6 +123,8 @@ public static function configureOAuth($settings) self::$client_secret = isset($settings['client_secret']) ? $settings['client_secret'] : null; + self::$version = isset($settings['version']) ? $settings['version'] : "v2"; + self::$api_path = self::$api_url . sprintf(self::$stores_prefix, self::$store_hash); self::$connection = false; } @@ -128,7 +139,8 @@ public static function configureOAuth($settings) * - api_key * * @param array $settings - * @throws \Exception + * @throws Exception + * @deprecated */ public static function configureBasicAuth(array $settings) { @@ -259,12 +271,27 @@ public static function setConnection(Connection $connection = null) * @param string $path api endpoint * @param string $resource resource class to map individual items * @return mixed array|string mapped collection or XML string if useXml is true + * @throws Exception */ public static function getCollection($path, $resource = 'Resource') { - $response = self::connection()->get(self::$api_path . $path); - - return self::mapCollection($resource, $response); + $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; + $response = self::connection()->get(self::$api_path .self::$version. $path); + + if ($response) { + if (self::$version === "v2") { + return self::mapCollection($resource, $response); + } else { + $response = (array)$response; + if (array_key_exists("data", $response)) { + return self::mapCollection($resource, $response["data"]); + } else { + return self::mapCollection($resource, $response); + } + } + } else { + throw new Exception("'response' returns empty, check url and filters if exist"); + } } /** @@ -273,29 +300,75 @@ public static function getCollection($path, $resource = 'Resource') * @param string $path api endpoint * @param string $resource resource class to map individual items * @return mixed Resource|string resource object or XML string if useXml is true + * @throws Exception */ public static function getResource($path, $resource = 'Resource') { - $response = self::connection()->get(self::$api_path . $path); + $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; - return self::mapResource($resource, $response); + $response = self::connection()->get(self::$api_path .self::$version. $path); + + if ($response) { + if (self::$version === "v2") { + return self::mapResource($resource, $response); + } else { + if (isset($response->data)) { + return self::mapResource($resource, $response->data); + } else { + return self::mapResource($resource, $response); + } + } + } else { + throw new Exception("'response' returns empty, check url and filters if exist"); + } + } + + /** + * Get url from Resource Class if exist. + * + * @param string $resource resource class to map individual items + * @return mixed Resource|string resource object or XML string if useXml is true + * @throws Exception + */ + + public static function getUrl($resource) + { + $baseResource = __NAMESPACE__ . '\\' . $resource; + $resource_namespace = (class_exists($baseResource)) ? $baseResource : 'Bigcommerce\\Api\\Resources\\' . $resource; + $object = new $resource_namespace(); + if (isset($object->urls)) { + if (array_key_exists(self::$version, $object->urls)) { + return $object->urls[self::$version]; + } else { + throw new Exception(self::$version." not available for this resource"); + } + } else { + return ""; + } } /** * Get a count value from the specified endpoint. * * @param string $path api endpoint + * @param string $resource * @return mixed int|string count value or XML string if useXml is true */ - public static function getCount($path) + public static function getCount($path, $resource = "Resource") { - $response = self::connection()->get(self::$api_path . $path); + $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; - if ($response == false || is_string($response)) { - return $response; - } + $response = self::connection()->get(self::$api_path .self::$version. $path); + + if (self::$version == "v2") { + if ($response == false || is_string($response)) { + return $response; + } - return $response->count; + return $response->count; + } else { + return $response->meta->pagination->total; + } } /** @@ -303,15 +376,17 @@ public static function getCount($path) * * @param string $path api endpoint * @param mixed $object object or XML string to create + * @param string $resource * @return mixed + * @throws Exception */ - public static function createResource($path, $object) + public static function createResource($path, $object, $resource = "Resource") { if (is_array($object)) { $object = (object)$object; } - - return self::connection()->post(self::$api_path . $path, $object); + $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; + return self::connection()->post(self::$api_path .self::$version. $path, $object); } /** @@ -319,26 +394,32 @@ public static function createResource($path, $object) * * @param string $path api endpoint * @param mixed $object object or XML string to update + * @param string $resource + * @param string $version * @return mixed + * @throws Exception */ - public static function updateResource($path, $object) + public static function updateResource($path, $object, $resource = "Resource") { if (is_array($object)) { $object = (object)$object; } - - return self::connection()->put(self::$api_path . $path, $object); + $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; + return self::connection()->put(self::$api_path .self::$version. $path, $object); } /** * Send a delete request to remove the specified resource. * * @param string $path api endpoint + * @param string $resource * @return mixed + * @throws Exception */ - public static function deleteResource($path) + public static function deleteResource($path, $resource = "Resource") { - return self::connection()->delete(self::$api_path . $path); + $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; + return self::connection()->delete(self::$api_path .self::$version. $path); } /** @@ -369,7 +450,6 @@ private static function mapCollection($resource, $object) private static function mapCollectionObject($object) { $class = self::$resource; - return new $class($object); } @@ -723,6 +803,7 @@ public static function getOptionValues($filter = array()) * * @param array $filter * @return array + * @throws Exception */ public static function getCategories($filter = array()) { @@ -747,6 +828,7 @@ public static function getCategoriesCount($filter = array()) * * @param int $id category id * @return Resources\Category + * @throws Exception */ public static function getCategory($id) { @@ -801,46 +883,135 @@ public static function deleteAllCategories() * The collection of brands. * * @param array $filter + * @param string $version * @return array + * @throws Exception */ - public static function getBrands($filter = array()) + public static function getBrands($filter = array(), $version = null) { + self::$version = (!is_null($version))?$version:self::$version; $filter = Filter::create($filter); - return self::getCollection('/brands' . $filter->toQuery(), 'Brand'); + if (in_array(self::$version, self::$available_versions)) { + return self::getCollection($filter->toQuery(), 'Brand'); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * The collection of Brand Metafields. + * + * @param int $id Brand Id + * @param array $filter + * @param string $version + * @return array + * @throws Exception + */ + public static function getBrandMetafields($id, $filter = array(), $version = null) + { + self::$version = (!is_null($version))?$version:self::$version; + $filter = Filter::create($filter); + if (in_array(self::$version, self::$available_versions)) { + return self::getCollection('/'.$id.'/metafields'.$filter->toQuery(), 'Brand'); + } else { + throw new Exception("'version' not available"); + } } /** * The total number of brands in the collection. * * @param array $filter + * @param null $version * @return int + * @throws Exception */ - public static function getBrandsCount($filter = array()) + public static function getBrandsCount($filter = array(), $version = null) { + self::$version = (!is_null($version))?$version:self::$version; $filter = Filter::create($filter); - return self::getCount('/brands/count' . $filter->toQuery()); + if (in_array(self::$version, self::$available_versions)) { + $path = (self::$version == "v2")?'/count':''; + return self::getCount($path . $filter->toQuery(), "Brand"); + } else { + throw new Exception("'version' not available"); + } } /** * A single brand by given id. * * @param int $id brand id + * @param string $version + * @return Resources\Brand + * @throws Exception + */ + public static function getBrand($id, $version = null) + { + self::$version = (!is_null($version))?$version:self::$version; + if (in_array(self::$version, self::$available_versions)) { + return self::getResource("/".$id, 'Brand'); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * A single brand Metafield by given brnad id and Metafield id. + * + * @param $brand_id + * @param $metafield_id + * @param array $filter + * @param string $version * @return Resources\Brand + * @throws Exception + */ + public static function getBrandMetafield($brand_id, $metafield_id, $filter = array(), $version = null) + { + self::$version = (!is_null($version))?$version:self::$version; + $filter = Filter::create($filter); + if (in_array(self::$version, self::$available_versions)) { + return self::getResource("/".$brand_id.'/metafields/'.$metafield_id.$filter->toQuery(), 'Brand'); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * Create a new brand from the given data. + * + * @param mixed $object + * @param null $version + * @return mixed + * @throws Exception */ - public static function getBrand($id) + public static function createBrand($object, $version = null) { - return self::getResource('/brands/' . $id, 'Brand'); + self::$version = (!is_null($version))?$version:self::$version; + if (in_array(self::$version, self::$available_versions)) { + return self::createResource('', $object, "Brand"); + } else { + throw new Exception("'version' not available"); + } } /** * Create a new brand from the given data. * + * @param $id * @param mixed $object + * @param null $version * @return mixed + * @throws Exception */ - public static function createBrand($object) + public static function createBrandMetafield($id, $object, $version = null) { - return self::createResource('/brands', $object); + self::$version = (!is_null($version))?$version:self::$version; + if (in_array(self::$version, self::$available_versions)) { + return self::createResource('/'.$id.'/metafields', $object, "Brand"); + } else { + throw new Exception("'version' not available"); + } } /** @@ -848,32 +1019,227 @@ public static function createBrand($object) * * @param int $id brand id * @param mixed $object + * @param null|string $version * @return mixed + * @throws Exception + */ + public static function updateBrand($id, $object, $version = null) + { + self::$version = (!is_null($version))?$version:self::$version; + if (in_array(self::$version, self::$available_versions)) { + return self::updateResource('/' . $id, $object, "Brand"); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * Update single brand Metafield by given brand id and Metafield id. + * + * @param int $brand_id + * @param int $metafield_id + * @param array|object $object + * @param string $version + * @return Resources\Brand + * @throws Exception */ - public static function updateBrand($id, $object) + public static function updateBrandMetafield($brand_id, $metafield_id, $object, $version = null) { - return self::updateResource('/brands/' . $id, $object); + self::$version = (!is_null($version))?$version:self::$version; + if (in_array(self::$version, self::$available_versions)) { + return self::updateResource("/".$brand_id.'/metafields/'.$metafield_id, $object, 'Brand'); + } else { + throw new Exception("'version' not available"); + } } /** * Delete the given brand. * * @param int $id brand id + * @param null $version * @return mixed + * @throws Exception */ - public static function deleteBrand($id) + public static function deleteBrand($id, $version = null) { - return self::deleteResource('/brands/' . $id); + self::$version = (!is_null($version))?$version:self::$version; + if (in_array(self::$version, self::$available_versions)) { + return self::deleteResource('/' . $id, "Brand"); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * Delete single brand Metafield by given brand id and Metafield id. + * + * @param int $brand_id + * @param int $metafield_id + * @param string $version + * @return Resources\Brand + * @throws Exception + */ + public static function deleteBrandMetafield($brand_id, $metafield_id, $version = null) + { + self::$version = (!is_null($version))?$version:self::$version; + if (in_array(self::$version, self::$available_versions)) { + return self::deleteResource("/".$brand_id.'/metafields/'.$metafield_id, 'Brand'); + } else { + throw new Exception("'version' not available"); + } } /** * Delete all brands. * + * @param null $version * @return mixed + * @throws Exception */ - public static function deleteAllBrands() + public static function deleteAllBrands($version = null) { - return self::deleteResource('/brands'); + self::$version = (!is_null($version))?$version:self::$version; + if (in_array(self::$version, self::$available_versions)) { + return self::deleteResource(''); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * Get a cart from cart id. + * + * @param $id + * @param null $version + * @return mixed + * @throws Exception + */ + public static function getCart($id, $version = null) + { + self::$version = (!is_null($version))?$version:self::$version; + if (in_array(self::$version, self::$available_versions)) { + return self::getResource("/".$id, 'Cart'); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * Create a new cart from the given data. + * + * @param mixed $object + * @param null $version + * @return mixed + * @throws Exception + */ + public static function createCart($object, $version = null) + { + self::$version = (!is_null($version))?$version:self::$version; + if (in_array(self::$version, self::$available_versions)) { + return self::createResource('', $object, "Cart"); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * Create a Line items for existing cart from the given data. + * + * @param $id + * @param mixed $object + * @param array $filter + * @param null $version + * @return mixed + * @throws Exception + */ + public static function createCartLineItems($id, $object, $filter = array(), $version = null) + { + self::$version = (!is_null($version))?$version:self::$version; + $filter = Filter::create($filter); + if (in_array(self::$version, self::$available_versions)) { + return self::createResource('/'.$id.'/items'.$filter->toQuery(), $object, "Cart"); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * update cart customer id. + * + * @param $cart_id + * @param $customer_id + * @param null $version + * @return mixed + * @throws Exception + */ + public static function updateCartCustomerId($cart_id, $customer_id, $version = null) + { + self::$version = (!is_null($version))?$version:self::$version; + if (in_array(self::$version, self::$available_versions)) { + return self::updateResource("/".$cart_id, array("customer_id"=>$customer_id), 'Cart'); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * Update a Line items for existing cart from the given data. + * + * @param $cart_id + * @param $line_item_id + * @param array|object $object + * @param array $filter + * @param null $version + * @return mixed + * @throws Exception + */ + public static function updateCartLineItem($cart_id, $line_item_id, $object, $filter = array(), $version = null) + { + self::$version = (!is_null($version))?$version:self::$version; + $filter = Filter::create($filter); + if (in_array(self::$version, self::$available_versions)) { + return self::updateResource('/'.$cart_id.'/items/'.$line_item_id.$filter->toQuery(), $object, "Cart"); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * Delete a Cart by Cart Id + * + * @param $id + * @param null $version + * @return mixed + * @throws Exception + */ + public static function deleteCart($id, $version = null) + { + self::$version = (!is_null($version))?$version:self::$version; + if (in_array(self::$version, self::$available_versions)) { + return self::deleteResource('/'.$id, "Cart"); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * Delete a Cart Line Item by Cart Id and Line Item Id + * + * @param $cart_id + * @param $line_item_id + * @param null $version + * @return mixed + * @throws Exception + */ + public static function deleteCartLineItem($cart_id, $line_item_id, $version = null) + { + self::$version = (!is_null($version))?$version:self::$version; + if (in_array(self::$version, self::$available_versions)) { + return self::deleteResource('/'.$cart_id.'/items/'.$line_item_id, "Cart"); + } else { + throw new Exception("'version' not available"); + } } /** @@ -881,6 +1247,7 @@ public static function deleteAllBrands() * * @param array $filter * @return array + * @throws Exception */ public static function getOrders($filter = array()) { @@ -905,6 +1272,7 @@ public static function getOrdersCount($filter = array()) * * @param array $filter * @return Resources\OrderStatus + * @throws Exception */ public static function getOrderStatusesWithCounts($filter = array()) { @@ -918,6 +1286,7 @@ public static function getOrderStatusesWithCounts($filter = array()) * * @param int $id order id * @return Resources\Order + * @throws Exception */ public static function getOrder($id) { @@ -927,6 +1296,7 @@ public static function getOrder($id) /** * @param $orderID * @return mixed + * @throws Exception */ public static function getOrderProducts($orderID) { @@ -952,6 +1322,7 @@ public static function getOrderProductsCount($orderID, $filter = array()) * * @param int $id order id * @return mixed + * @throws Exception */ public static function deleteOrder($id) { @@ -996,6 +1367,7 @@ public static function updateOrder($id, $object) * * @param array $filter * @return array + * @throws Exception */ public static function getCustomers($filter = array()) { diff --git a/src/Bigcommerce/Api/Connection.php b/src/Bigcommerce/Api/Connection.php index 1e5f7fd1..7c737724 100644 --- a/src/Bigcommerce/Api/Connection.php +++ b/src/Bigcommerce/Api/Connection.php @@ -165,6 +165,7 @@ public function failOnError($option = true) * * @param string $username * @param string $password + * @deprecated */ public function authenticateBasic($username, $password) { @@ -285,6 +286,7 @@ private function handleResponse() $status = $this->getStatus(); if ($status >= 400 && $status <= 499) { + $this->failOnError(); if ($this->failOnError) { throw new ClientError($body, $status); } else { diff --git a/src/Bigcommerce/Api/Error.php b/src/Bigcommerce/Api/Error.php index aae1d5ab..9571c1e0 100644 --- a/src/Bigcommerce/Api/Error.php +++ b/src/Bigcommerce/Api/Error.php @@ -11,6 +11,10 @@ public function __construct($message, $code) { if (is_array($message)) { $message = $message[0]->message; + } else { + if (isset($message->title)) { + $message = $message->title; + } } parent::__construct($message, $code); diff --git a/src/Bigcommerce/Api/Resources/Brand.php b/src/Bigcommerce/Api/Resources/Brand.php index a8b301fe..0ac80615 100644 --- a/src/Bigcommerce/Api/Resources/Brand.php +++ b/src/Bigcommerce/Api/Resources/Brand.php @@ -15,13 +15,23 @@ class Brand extends Resource 'id', ); - public function create() + public $urls = array( + "v2" => "/brands", + "v3" => "/catalog/brands" + ); + + public function create($version = null) + { + return Client::createBrand($this->getCreateFields(), $version); + } + + public function update($version = null) { - return Client::createBrand($this->getCreateFields()); + return Client::updateBrand($this->id, $this->getUpdateFields(), $version); } - public function update() + public function delete($version = null) { - return Client::updateBrand($this->id, $this->getUpdateFields()); + return Client::deleteBrand($this->id, $version); } } diff --git a/src/Bigcommerce/Api/Resources/Cart.php b/src/Bigcommerce/Api/Resources/Cart.php new file mode 100644 index 00000000..a39dfa23 --- /dev/null +++ b/src/Bigcommerce/Api/Resources/Cart.php @@ -0,0 +1,60 @@ + "/carts" + ); + + public function create($version = "v3") + { + return Client::createCart($this->getCreateFields(), $version); + } + + /** Note only Customer Id can be updated using Cart Update * + * @param $customer_id + * @param string $version + * @return mixed + * @throws Exception + */ + public function update($customer_id, $version = "v3") + { + return Client::updateCartCustomerId($this->id, $customer_id, $version); + } + + /** Add Line Items to Cart * + * @param $items + * @param array $filter + * @param string $version + * @return mixed + * @throws Exception + */ + public function addItems($items, $filter = array(), $version = "v3") + { + return Client::createCartLineItems($this->id, $items, $filter, $version); + } + + public function delete($version = "v3") + { + return Client::deleteCart($this->id, $version); + } + +} \ No newline at end of file diff --git a/test/Unit/Api/ClientErrorTest.php b/test/Unit/Api/ClientErrorTest.php index 47a22b89..c88a341f 100644 --- a/test/Unit/Api/ClientErrorTest.php +++ b/test/Unit/Api/ClientErrorTest.php @@ -1,5 +1,5 @@ Date: Sun, 5 Sep 2021 20:51:25 +0530 Subject: [PATCH 05/12] Update package, now you can install using composer --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8ab29901..20aeb793 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Bigcommerce API Client ====================== -PHP client for connecting to the Bigcommerce V2 REST API. +PHP client for connecting to the Bigcommerce V2 and V3 REST API. To find out more, visit the official documentation website: http://developer.bigcommerce.com/ @@ -31,10 +31,10 @@ Installation ------------ Use the following Composer command to install the -API client from [the Bigcommerce vendor on Packagist](https://packagist.org/packages/bigcommerce/api): +API client from [the Bigcommerce vendor on Packagist](https://packagist.org/packages/naveenrajbu/bigcommerce-api-php-v3): ~~~shell - $ composer require bigcommerce/api + $ composer require naveenrajbu/bigcommerce-api-php-v3 $ composer update ~~~ @@ -85,8 +85,8 @@ Set 'v3' by default if you're only using 'v3' APIs 'v3' methods has `$version` parameter which can be used if you didn't set version 'v3' as default version. -##Carts(V3) - +Carts(V3) +------------ you can do almost all the functions in cart. **Get Cart by Cart Id**: `getCart($id, $version = null);` From bef1279e36d37e20cd6aee791bb0e131799953a4 Mon Sep 17 00:00:00 2001 From: naveenrajbu Date: Tue, 7 Sep 2021 18:35:45 +0530 Subject: [PATCH 06/12] Bug fixes and Minor Updates --- src/Bigcommerce/Api/Client.php | 172 +++++++++++------------- src/Bigcommerce/Api/Connection.php | 1 - src/Bigcommerce/Api/Error.php | 2 + src/Bigcommerce/Api/Resource.php | 5 + src/Bigcommerce/Api/Resources/Brand.php | 5 + src/Bigcommerce/Api/Resources/Cart.php | 5 + 6 files changed, 95 insertions(+), 95 deletions(-) diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index ec6136cc..266eed0d 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -73,7 +73,7 @@ class Client private static $stores_prefix = '/stores/%s/'; private static $api_url = 'https://api.bigcommerce.com'; private static $login_url = 'https://login.bigcommerce.com'; - private static $available_versions = array("v2","v3"); + private static $available_versions = array(null, "v2", "v3"); /** * Configure the API client with the required settings to access @@ -123,6 +123,14 @@ public static function configureOAuth($settings) self::$client_secret = isset($settings['client_secret']) ? $settings['client_secret'] : null; + if (isset($settings['version'])) { + if (in_array($settings['version'], self::$available_versions)) { + self::$version = (!is_null($settings['version']))? $settings['version']:'v2'; + } else { + throw new Exception("'version' not available"); + } + } + self::$version = isset($settings['version']) ? $settings['version'] : "v2"; self::$api_path = self::$api_url . sprintf(self::$stores_prefix, self::$store_hash); @@ -270,27 +278,20 @@ public static function setConnection(Connection $connection = null) * * @param string $path api endpoint * @param string $resource resource class to map individual items + * @param null $version * @return mixed array|string mapped collection or XML string if useXml is true * @throws Exception */ - public static function getCollection($path, $resource = 'Resource') + public static function getCollection($path, $resource = 'Resource', $version = null) { + $temp_version = (!is_null($version) and in_array($version, self::$available_versions))?$version:self::$version; $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; - $response = self::connection()->get(self::$api_path .self::$version. $path); + $response = self::connection()->get(self::$api_path .$temp_version. $path); - if ($response) { - if (self::$version === "v2") { - return self::mapCollection($resource, $response); - } else { - $response = (array)$response; - if (array_key_exists("data", $response)) { - return self::mapCollection($resource, $response["data"]); - } else { - return self::mapCollection($resource, $response); - } - } + if (isset($response->data)) { + return self::mapCollection($resource, $response->data); } else { - throw new Exception("'response' returns empty, check url and filters if exist"); + return self::mapCollection($resource, $response); } } @@ -299,27 +300,21 @@ public static function getCollection($path, $resource = 'Resource') * * @param string $path api endpoint * @param string $resource resource class to map individual items + * @param null $version * @return mixed Resource|string resource object or XML string if useXml is true * @throws Exception */ - public static function getResource($path, $resource = 'Resource') + public static function getResource($path, $resource = 'Resource', $version = null) { + $temp_version = (!is_null($version) and in_array($version, self::$available_versions))?$version:self::$version; $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; - $response = self::connection()->get(self::$api_path .self::$version. $path); + $response = self::connection()->get(self::$api_path .$temp_version. $path); - if ($response) { - if (self::$version === "v2") { - return self::mapResource($resource, $response); - } else { - if (isset($response->data)) { - return self::mapResource($resource, $response->data); - } else { - return self::mapResource($resource, $response); - } - } + if (isset($response->data)) { + return self::mapResource($resource, $response->data); } else { - throw new Exception("'response' returns empty, check url and filters if exist"); + return self::mapResource($resource, $response); } } @@ -352,13 +347,16 @@ public static function getUrl($resource) * * @param string $path api endpoint * @param string $resource + * @param null $version * @return mixed int|string count value or XML string if useXml is true + * @throws Exception */ - public static function getCount($path, $resource = "Resource") + public static function getCount($path, $resource = "Resource", $version = null) { + $temp_version = (!is_null($version) and in_array($version, self::$available_versions))?$version:self::$version; $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; - $response = self::connection()->get(self::$api_path .self::$version. $path); + $response = self::connection()->get(self::$api_path .$temp_version. $path); if (self::$version == "v2") { if ($response == false || is_string($response)) { @@ -377,16 +375,18 @@ public static function getCount($path, $resource = "Resource") * @param string $path api endpoint * @param mixed $object object or XML string to create * @param string $resource + * @param null $version * @return mixed * @throws Exception */ - public static function createResource($path, $object, $resource = "Resource") + public static function createResource($path, $object, $resource = "Resource", $version = null) { + $temp_version = (!is_null($version) and in_array($version, self::$available_versions))?$version:self::$version; if (is_array($object)) { $object = (object)$object; } $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; - return self::connection()->post(self::$api_path .self::$version. $path, $object); + return self::connection()->post(self::$api_path .$temp_version. $path, $object); } /** @@ -395,17 +395,18 @@ public static function createResource($path, $object, $resource = "Resource") * @param string $path api endpoint * @param mixed $object object or XML string to update * @param string $resource - * @param string $version + * @param null $version * @return mixed * @throws Exception */ - public static function updateResource($path, $object, $resource = "Resource") + public static function updateResource($path, $object, $resource = "Resource", $version = null) { + $temp_version = (!is_null($version) and in_array($version, self::$available_versions))?$version:self::$version; if (is_array($object)) { $object = (object)$object; } $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; - return self::connection()->put(self::$api_path .self::$version. $path, $object); + return self::connection()->put(self::$api_path .$temp_version. $path, $object); } /** @@ -413,13 +414,15 @@ public static function updateResource($path, $object, $resource = "Resource") * * @param string $path api endpoint * @param string $resource + * @param null $version * @return mixed * @throws Exception */ - public static function deleteResource($path, $resource = "Resource") + public static function deleteResource($path, $resource = "Resource", $version = null) { + $temp_version = (!is_null($version) and in_array($version, self::$available_versions))?$version:self::$version; $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; - return self::connection()->delete(self::$api_path .self::$version. $path); + return self::connection()->delete(self::$api_path .$temp_version. $path); } /** @@ -889,10 +892,9 @@ public static function deleteAllCategories() */ public static function getBrands($filter = array(), $version = null) { - self::$version = (!is_null($version))?$version:self::$version; $filter = Filter::create($filter); - if (in_array(self::$version, self::$available_versions)) { - return self::getCollection($filter->toQuery(), 'Brand'); + if (in_array($version, self::$available_versions)) { + return self::getCollection($filter->toQuery(), 'Brand', $version); } else { throw new Exception("'version' not available"); } @@ -909,10 +911,9 @@ public static function getBrands($filter = array(), $version = null) */ public static function getBrandMetafields($id, $filter = array(), $version = null) { - self::$version = (!is_null($version))?$version:self::$version; $filter = Filter::create($filter); - if (in_array(self::$version, self::$available_versions)) { - return self::getCollection('/'.$id.'/metafields'.$filter->toQuery(), 'Brand'); + if (in_array($version, self::$available_versions)) { + return self::getCollection('/'.$id.'/metafields'.$filter->toQuery(), 'Brand', $version); } else { throw new Exception("'version' not available"); } @@ -928,11 +929,10 @@ public static function getBrandMetafields($id, $filter = array(), $version = nul */ public static function getBrandsCount($filter = array(), $version = null) { - self::$version = (!is_null($version))?$version:self::$version; $filter = Filter::create($filter); - if (in_array(self::$version, self::$available_versions)) { - $path = (self::$version == "v2")?'/count':''; - return self::getCount($path . $filter->toQuery(), "Brand"); + if (in_array($version, self::$available_versions)) { + $path = ($version != "v3" and self::$version != "v3")?'/count':''; + return self::getCount($path . $filter->toQuery(), "Brand", $version); } else { throw new Exception("'version' not available"); } @@ -948,9 +948,8 @@ public static function getBrandsCount($filter = array(), $version = null) */ public static function getBrand($id, $version = null) { - self::$version = (!is_null($version))?$version:self::$version; - if (in_array(self::$version, self::$available_versions)) { - return self::getResource("/".$id, 'Brand'); + if (in_array($version, self::$available_versions)) { + return self::getResource("/".$id, 'Brand', $version); } else { throw new Exception("'version' not available"); } @@ -968,10 +967,9 @@ public static function getBrand($id, $version = null) */ public static function getBrandMetafield($brand_id, $metafield_id, $filter = array(), $version = null) { - self::$version = (!is_null($version))?$version:self::$version; $filter = Filter::create($filter); - if (in_array(self::$version, self::$available_versions)) { - return self::getResource("/".$brand_id.'/metafields/'.$metafield_id.$filter->toQuery(), 'Brand'); + if (in_array($version, self::$available_versions)) { + return self::getResource("/".$brand_id.'/metafields/'.$metafield_id.$filter->toQuery(), 'Brand', $version); } else { throw new Exception("'version' not available"); } @@ -987,9 +985,8 @@ public static function getBrandMetafield($brand_id, $metafield_id, $filter = arr */ public static function createBrand($object, $version = null) { - self::$version = (!is_null($version))?$version:self::$version; - if (in_array(self::$version, self::$available_versions)) { - return self::createResource('', $object, "Brand"); + if (in_array($version, self::$available_versions)) { + return self::createResource('', $object, "Brand", $version); } else { throw new Exception("'version' not available"); } @@ -1006,9 +1003,8 @@ public static function createBrand($object, $version = null) */ public static function createBrandMetafield($id, $object, $version = null) { - self::$version = (!is_null($version))?$version:self::$version; - if (in_array(self::$version, self::$available_versions)) { - return self::createResource('/'.$id.'/metafields', $object, "Brand"); + if (in_array($version, self::$available_versions)) { + return self::createResource('/'.$id.'/metafields', $object, "Brand", $version); } else { throw new Exception("'version' not available"); } @@ -1025,9 +1021,8 @@ public static function createBrandMetafield($id, $object, $version = null) */ public static function updateBrand($id, $object, $version = null) { - self::$version = (!is_null($version))?$version:self::$version; - if (in_array(self::$version, self::$available_versions)) { - return self::updateResource('/' . $id, $object, "Brand"); + if (in_array($version, self::$available_versions)) { + return self::updateResource('/' . $id, $object, "Brand", $version); } else { throw new Exception("'version' not available"); } @@ -1045,9 +1040,8 @@ public static function updateBrand($id, $object, $version = null) */ public static function updateBrandMetafield($brand_id, $metafield_id, $object, $version = null) { - self::$version = (!is_null($version))?$version:self::$version; - if (in_array(self::$version, self::$available_versions)) { - return self::updateResource("/".$brand_id.'/metafields/'.$metafield_id, $object, 'Brand'); + if (in_array($version, self::$available_versions)) { + return self::updateResource("/".$brand_id.'/metafields/'.$metafield_id, $object, 'Brand', $version); } else { throw new Exception("'version' not available"); } @@ -1063,9 +1057,8 @@ public static function updateBrandMetafield($brand_id, $metafield_id, $object, $ */ public static function deleteBrand($id, $version = null) { - self::$version = (!is_null($version))?$version:self::$version; - if (in_array(self::$version, self::$available_versions)) { - return self::deleteResource('/' . $id, "Brand"); + if (in_array($version, self::$available_versions)) { + return self::deleteResource('/' . $id, "Brand", $version); } else { throw new Exception("'version' not available"); } @@ -1082,9 +1075,8 @@ public static function deleteBrand($id, $version = null) */ public static function deleteBrandMetafield($brand_id, $metafield_id, $version = null) { - self::$version = (!is_null($version))?$version:self::$version; - if (in_array(self::$version, self::$available_versions)) { - return self::deleteResource("/".$brand_id.'/metafields/'.$metafield_id, 'Brand'); + if (in_array($version, self::$available_versions)) { + return self::deleteResource("/".$brand_id.'/metafields/'.$metafield_id, 'Brand', $version); } else { throw new Exception("'version' not available"); } @@ -1099,9 +1091,8 @@ public static function deleteBrandMetafield($brand_id, $metafield_id, $version = */ public static function deleteAllBrands($version = null) { - self::$version = (!is_null($version))?$version:self::$version; if (in_array(self::$version, self::$available_versions)) { - return self::deleteResource(''); + return self::deleteResource('', 'Brand', $version); } else { throw new Exception("'version' not available"); } @@ -1117,9 +1108,8 @@ public static function deleteAllBrands($version = null) */ public static function getCart($id, $version = null) { - self::$version = (!is_null($version))?$version:self::$version; - if (in_array(self::$version, self::$available_versions)) { - return self::getResource("/".$id, 'Cart'); + if (in_array($version, self::$available_versions)) { + return self::getResource("/".$id, 'Cart', $version); } else { throw new Exception("'version' not available"); } @@ -1135,9 +1125,8 @@ public static function getCart($id, $version = null) */ public static function createCart($object, $version = null) { - self::$version = (!is_null($version))?$version:self::$version; - if (in_array(self::$version, self::$available_versions)) { - return self::createResource('', $object, "Cart"); + if (in_array($version, self::$available_versions)) { + return self::createResource('', $object, "Cart", $version); } else { throw new Exception("'version' not available"); } @@ -1155,10 +1144,9 @@ public static function createCart($object, $version = null) */ public static function createCartLineItems($id, $object, $filter = array(), $version = null) { - self::$version = (!is_null($version))?$version:self::$version; $filter = Filter::create($filter); - if (in_array(self::$version, self::$available_versions)) { - return self::createResource('/'.$id.'/items'.$filter->toQuery(), $object, "Cart"); + if (in_array($version, self::$available_versions)) { + return self::createResource('/'.$id.'/items'.$filter->toQuery(), $object, "Cart", $version); } else { throw new Exception("'version' not available"); } @@ -1175,9 +1163,8 @@ public static function createCartLineItems($id, $object, $filter = array(), $ver */ public static function updateCartCustomerId($cart_id, $customer_id, $version = null) { - self::$version = (!is_null($version))?$version:self::$version; - if (in_array(self::$version, self::$available_versions)) { - return self::updateResource("/".$cart_id, array("customer_id"=>$customer_id), 'Cart'); + if (in_array($version, self::$available_versions)) { + return self::updateResource("/".$cart_id, array("customer_id"=>$customer_id), 'Cart', $version); } else { throw new Exception("'version' not available"); } @@ -1196,10 +1183,9 @@ public static function updateCartCustomerId($cart_id, $customer_id, $version = n */ public static function updateCartLineItem($cart_id, $line_item_id, $object, $filter = array(), $version = null) { - self::$version = (!is_null($version))?$version:self::$version; $filter = Filter::create($filter); - if (in_array(self::$version, self::$available_versions)) { - return self::updateResource('/'.$cart_id.'/items/'.$line_item_id.$filter->toQuery(), $object, "Cart"); + if (in_array($version, self::$available_versions)) { + return self::updateResource('/'.$cart_id.'/items/'.$line_item_id.$filter->toQuery(), $object, "Cart", $version); } else { throw new Exception("'version' not available"); } @@ -1215,9 +1201,8 @@ public static function updateCartLineItem($cart_id, $line_item_id, $object, $fil */ public static function deleteCart($id, $version = null) { - self::$version = (!is_null($version))?$version:self::$version; - if (in_array(self::$version, self::$available_versions)) { - return self::deleteResource('/'.$id, "Cart"); + if (in_array($version, self::$available_versions)) { + return self::deleteResource('/'.$id, "Cart", $version); } else { throw new Exception("'version' not available"); } @@ -1234,9 +1219,8 @@ public static function deleteCart($id, $version = null) */ public static function deleteCartLineItem($cart_id, $line_item_id, $version = null) { - self::$version = (!is_null($version))?$version:self::$version; - if (in_array(self::$version, self::$available_versions)) { - return self::deleteResource('/'.$cart_id.'/items/'.$line_item_id, "Cart"); + if (in_array($version, self::$available_versions)) { + return self::deleteResource('/'.$cart_id.'/items/'.$line_item_id, "Cart", $version); } else { throw new Exception("'version' not available"); } diff --git a/src/Bigcommerce/Api/Connection.php b/src/Bigcommerce/Api/Connection.php index 7c737724..548e0685 100644 --- a/src/Bigcommerce/Api/Connection.php +++ b/src/Bigcommerce/Api/Connection.php @@ -286,7 +286,6 @@ private function handleResponse() $status = $this->getStatus(); if ($status >= 400 && $status <= 499) { - $this->failOnError(); if ($this->failOnError) { throw new ClientError($body, $status); } else { diff --git a/src/Bigcommerce/Api/Error.php b/src/Bigcommerce/Api/Error.php index 9571c1e0..ed521296 100644 --- a/src/Bigcommerce/Api/Error.php +++ b/src/Bigcommerce/Api/Error.php @@ -14,6 +14,8 @@ public function __construct($message, $code) } else { if (isset($message->title)) { $message = $message->title; + } else { + $message = "Undefined Error Occurred"; } } diff --git a/src/Bigcommerce/Api/Resource.php b/src/Bigcommerce/Api/Resource.php index 53f8a857..363dd974 100644 --- a/src/Bigcommerce/Api/Resource.php +++ b/src/Bigcommerce/Api/Resource.php @@ -110,4 +110,9 @@ private function isIgnoredField($field, $value) return false; } + + public function toJson() + { + return json_encode($this->fields, true); + } } diff --git a/src/Bigcommerce/Api/Resources/Brand.php b/src/Bigcommerce/Api/Resources/Brand.php index 0ac80615..2f5495b8 100644 --- a/src/Bigcommerce/Api/Resources/Brand.php +++ b/src/Bigcommerce/Api/Resources/Brand.php @@ -34,4 +34,9 @@ public function delete($version = null) { return Client::deleteBrand($this->id, $version); } + + public function toJson() + { + return parent::toJson(); + } } diff --git a/src/Bigcommerce/Api/Resources/Cart.php b/src/Bigcommerce/Api/Resources/Cart.php index a39dfa23..3a730a31 100644 --- a/src/Bigcommerce/Api/Resources/Cart.php +++ b/src/Bigcommerce/Api/Resources/Cart.php @@ -57,4 +57,9 @@ public function delete($version = "v3") return Client::deleteCart($this->id, $version); } + public function toJson() + { + return parent::toJson(); + } + } \ No newline at end of file From 5ef7235f55857c509c3ffe3d93dc19bfeafea4b8 Mon Sep 17 00:00:00 2001 From: naveenrajbu Date: Sun, 12 Sep 2021 23:29:53 +0530 Subject: [PATCH 07/12] Update Catalog\Products and Wishlists --- README.md | 132 ++- src/Bigcommerce/Api/Client.php | 998 ++++++++++++++++-- src/Bigcommerce/Api/Resources/Option.php | 2 +- src/Bigcommerce/Api/Resources/Product.php | 108 +- .../Api/Resources/ProductCustomField.php | 17 +- .../Api/Resources/ProductImage.php | 17 +- .../Api/Resources/ProductOption.php | 39 +- .../Api/Resources/ProductReview.php | 4 + .../Api/Resources/ProductVideo.php | 29 + src/Bigcommerce/Api/Resources/Rule.php | 8 +- .../Api/Resources/RuleCondition.php | 9 +- src/Bigcommerce/Api/Resources/Sku.php | 8 +- src/Bigcommerce/Api/Resources/SkuOption.php | 8 +- 13 files changed, 1234 insertions(+), 145 deletions(-) diff --git a/README.md b/README.md index 20aeb793..95ebee5e 100644 --- a/README.md +++ b/README.md @@ -79,11 +79,102 @@ foreach($brands as $brand){ echo $brand->name."\n"; } ~~~ -As of now, Only `Carts, Wishlists and Catlalog\brands support 'v3'` other APIs are still in development will be added here once it is completed, Meanwhile `You can still use 'v2' features without any issues`. +As of now, Only `Carts, Wishlists, Catalog\products and Catlalog\brands support 'v3'` other APIs are still in development will be added here once it is completed, Meanwhile `You can still use 'v2' features without any issues`. Set 'v3' by default if you're only using 'v3' APIs -'v3' methods has `$version` parameter which can be used if you didn't set version 'v3' as default version. +All 'v3' methods has `$version` parameter which can be used if you didn't set version 'v3' as default version. + +All the 'Get' Methods has `$filter = array()` if applicable to set query parameters + +Products(V2 and V3) +------------ +you can get products and its variants, options variant metafields. +~~~php +Bigcommerce::configure(array( + 'client_id' => 'xxxxxxxxxxxxxxxxxxxxxx', + 'auth_token' => 'xxxxxxxxxxxxxxxxxxxxxxx', + 'store_hash' => 'xxxxxxxxx', + 'version' => 'v3' //optional By Default set as 'v2' +)); + +$products = Bigcommerce::getProducts(); //getProducts($filter = array(), $version = null); + +foreach($products as $item){ + echo $item->name."\n"; + $videos = $item->videos(); +} + +//Single Product + +$product = Bigcommerce::getProduct(1); + +//To get that product variants + +$variants = $product->variants(); //or Bigcommerce::getProduct(1)->variants(123); + +//To get Meta fields for a Single Variant + +$variant_metafield = Bigcommerce::getProducts(1)->variants(123)->meta_fields(); + +//To delete a Meta field + +$variant_metafield->delete(); //or Bigcommerce::getProduct(1)->variants(123)->meta_fields(1)->delete(); +~~~ +The Product Instance has following member functions + +~~~php +$product = Bigcommerce::getProduct(1); + +// Member Function works only on 'v3' +//All the 'v3' Resource Class has create(), update() and delete() functions + +$product->bulk_pricing_rules(1)->delete(); // Delete bulk pricing id 1 for product id 1 +$bulk = $product->bulk_pricing_rules(); // Retrieve all Bulk Pricing rules for product id 1 +$complex_rules = $product->complex_rules(); // or Bigcommerce::getProductComplexRules($product_id); + +$variants = $product->variants(); // or Bigcommerce::getProductVariants($product_id); +$variant = $product->variant(1); + +//'ProductVariant' Object has meta_fields(), create_image(), create(), update(), delete() functions + +$variant->create_image("https://image.jpg"); // ProductVariantObject->create_image($image_url); + +$variant_metafield = $variant->meta_fields(); // 'ProductVariantMetafield' object has create(), update(), delete() functions +$variant_metafield->delete(); + +// ProductObject->options() works on both 'v2' and 'v3' but ProductObject->options(1)->values() works only on 'v3' +// So, By default ProductObject->options($version = "v3") set to version 'v3' + +$options = $product->options("v2"); +$option_values = $product->options("v3")->values(); +$option_value = $product->options("v3")->values(1); +$option_value->delete(); + +// Member Functions works on both 'v2' and 'v3' + +$brand = $product->brand(); +$image = $product->images(1)->delete(); // or Bigcommerce::getProductImage(1); +$videos = $product->videos(); // or Bigcommerce::getProductVideos(); + +$video = $product->videos(1); // or Bigcommerce::getProductVideo(1); +$video->name = "Updated Video"; // or Bigcommerce::updateProductVideo($product_id, $video_id, $array); +$video->update(); // or $video->delete(); // or Bigcommerce::deleteProductVideo($product_id, $video_id); + +$custom_fields = $product->custom_fields(); // or Bigcommerce::getProductCustomFields($product_id); +$reviews = $product->reviews(); // or Bigcommerce::getProductReviews($product_id); + +// Member Functions works only on 'v2' +// May return empty data since 'v2' has been abandoned by Bigcommerce + +$skus = $product->skus(); +$rules = $product->rules(); +$configurable_rules = $product->configurable_rules(); +$discount_rules = $product->discount_rules(); +$option_set = $product->option_set(); +$tax_class = $product->tax_class(); +~~~ +**Product Modifiers and Meta Fields are still in Development** Carts(V3) ------------ @@ -359,6 +450,43 @@ Bigcommerce::updateBrandMetafield(1, 1, $metaField, 'v3'); Bigcommerce::deleteBrandMetafield(1, 1, 'v3'); ~~~ +Wishlists (Only on 'v3') +----------- +The wishlists has following Functions +~~~php +$wishlists = Bigcommerce::getWishlists(); + +foreach($wishlists as $item){ + echo $item->name; +} + +$items = array( + "items" = array( + array( + "product_id" => 1, + "variant_id" => 2 + ) + ) +); + +Bigcommerce::createWishlist($items); + +//or + +$wishlistObject = new Bigcommerce\Api\Resources\Wishlist(); +$wishlistObject->name = "New List"; +$wishlistObject->items = $items['items']; +$wishlistObject->create(); + +$wishlist = Bigcommerce::getWishlist(1); +$wishlist->name = "Test Wishlist"; +$wishlist->update(); \\ or Bigcommerce::updateWishlist($wishlist_id, $array); + +$wishlist->addItems($items); \\ or Bigcommerce::createWishlistItems($wishlist_id, $items); + +$wishlist->delete(); +~~~ + That's all for now. I'll update for other APIs Continuously. **Feel free to Pull and Merge for other APIs** I'll publish this repo on composer for easy Installation diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 266eed0d..a7d95790 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -285,9 +285,8 @@ public static function setConnection(Connection $connection = null) public static function getCollection($path, $resource = 'Resource', $version = null) { $temp_version = (!is_null($version) and in_array($version, self::$available_versions))?$version:self::$version; - $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; + $path = ($resource !== "Resource")?self::getUrl($resource, $temp_version).$path : $path; $response = self::connection()->get(self::$api_path .$temp_version. $path); - if (isset($response->data)) { return self::mapCollection($resource, $response->data); } else { @@ -307,7 +306,7 @@ public static function getCollection($path, $resource = 'Resource', $version = n public static function getResource($path, $resource = 'Resource', $version = null) { $temp_version = (!is_null($version) and in_array($version, self::$available_versions))?$version:self::$version; - $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; + $path = ($resource !== "Resource")?self::getUrl($resource, $temp_version).$path : $path; $response = self::connection()->get(self::$api_path .$temp_version. $path); @@ -322,20 +321,21 @@ public static function getResource($path, $resource = 'Resource', $version = nul * Get url from Resource Class if exist. * * @param string $resource resource class to map individual items + * @param $version * @return mixed Resource|string resource object or XML string if useXml is true * @throws Exception */ - public static function getUrl($resource) + public static function getUrl($resource, $version) { $baseResource = __NAMESPACE__ . '\\' . $resource; $resource_namespace = (class_exists($baseResource)) ? $baseResource : 'Bigcommerce\\Api\\Resources\\' . $resource; $object = new $resource_namespace(); if (isset($object->urls)) { - if (array_key_exists(self::$version, $object->urls)) { - return $object->urls[self::$version]; + if (array_key_exists($version, $object->urls)) { + return $object->urls[$version]; } else { - throw new Exception(self::$version." not available for this resource"); + throw new Exception($version." not available for this resource"); } } else { return ""; @@ -354,7 +354,7 @@ public static function getUrl($resource) public static function getCount($path, $resource = "Resource", $version = null) { $temp_version = (!is_null($version) and in_array($version, self::$available_versions))?$version:self::$version; - $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; + $path = ($resource !== "Resource")?self::getUrl($resource, $temp_version).$path : $path; $response = self::connection()->get(self::$api_path .$temp_version. $path); @@ -385,7 +385,7 @@ public static function createResource($path, $object, $resource = "Resource", $v if (is_array($object)) { $object = (object)$object; } - $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; + $path = ($resource !== "Resource")?self::getUrl($resource, $temp_version).$path : $path; return self::connection()->post(self::$api_path .$temp_version. $path, $object); } @@ -405,7 +405,7 @@ public static function updateResource($path, $object, $resource = "Resource", $v if (is_array($object)) { $object = (object)$object; } - $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; + $path = ($resource !== "Resource")?self::getUrl($resource, $temp_version).$path : $path; return self::connection()->put(self::$api_path .$temp_version. $path, $object); } @@ -421,7 +421,7 @@ public static function updateResource($path, $object, $resource = "Resource", $v public static function deleteResource($path, $resource = "Resource", $version = null) { $temp_version = (!is_null($version) and in_array($version, self::$available_versions))?$version:self::$version; - $path = ($resource !== "Resource")?self::getUrl($resource).$path : $path; + $path = ($resource !== "Resource")?self::getUrl($resource, $temp_version).$path : $path; return self::connection()->delete(self::$api_path .$temp_version. $path); } @@ -555,45 +555,65 @@ public static function getTime() * Returns the default collection of products. * * @param array $filter + * @param null $version * @return mixed array|string list of products or XML string if useXml is true + * @throws Exception */ - public static function getProducts($filter = array()) + public static function getProducts($filter = array(), $version = null) { $filter = Filter::create($filter); - return self::getCollection('/products' . $filter->toQuery(), 'Product'); + return self::getCollection('' . $filter->toQuery(), 'Product', $version); } /** * Gets collection of images for a product. * * @param int $id product id + * @param array $filter + * @param null $version * @return mixed array|string list of products or XML string if useXml is true + * @throws Exception */ - public static function getProductImages($id) + public static function getProductImages($id, $filter = array(), $version = null) { - return self::getCollection('/products/' . $id . '/images/', 'ProductImage'); + $filter = Filter::create($filter); + return self::getCollection('/' . $id . '/images'.$filter->toQuery(), 'ProductImage', $version); } /** * Gets collection of custom fields for a product. * * @param int $id product ID + * @param null $version * @return array|string list of products or XML string if useXml is true + * @throws Exception */ - public static function getProductCustomFields($id) + public static function getProductCustomFields($id, $version = null) { - return self::getCollection('/products/' . $id . '/custom_fields', 'ProductCustomField'); + $temp_version = (is_null($version))?self::$version:$version; + if ($temp_version === "v2") { + return self::getCollection('/' . $id . '/custom_fields', 'ProductCustomField', $version); + } else { + return self::getCollection('/' . $id . '/custom-fields', 'ProductCustomField', $version); + } } /** * Returns a single custom field by given id - * @param int $product_id product id - * @param int $id custom field id + * @param int $product_id product id + * @param int $id custom field id + * @param null $version * @return Resources\ProductCustomField|bool Returns ProductCustomField if exists, false if not exists + * @throws Exception */ - public static function getProductCustomField($product_id, $id) + public static function getProductCustomField($product_id, $id, $version = null) { - return self::getResource('/products/' . $product_id . '/custom_fields/' . $id, 'ProductCustomField'); + $temp_version = (is_null($version))?self::$version:$version; + if ($temp_version === "v2") { + return self::getResource('/' . $product_id . '/custom_fields/' . $id, 'ProductCustomField', $version); + } else { + return self::getResource('/' . $product_id . '/custom-fields/' . $id, 'ProductCustomField', $version); + } } /** @@ -601,22 +621,31 @@ public static function getProductCustomField($product_id, $id) * * @param int $product_id product id * @param mixed $object fields to create + * @param $version * @return Object Object with `id`, `product_id`, `name` and `text` keys + * @throws Exception */ - public static function createProductCustomField($product_id, $object) + public static function createProductCustomField($product_id, $object, $version) { - return self::createResource('/products/' . $product_id . '/custom_fields', $object); + $temp_version = (is_null($version))?self::$version:$version; + if ($temp_version === "v2") { + return self::createResource('/' . $product_id . '/custom_fields', $object, 'ProductCustomField', $version); + } else { + return self::createResource('/' . $product_id . '/custom-fields', $object, 'ProductCustomField', $version); + } } /** * Gets collection of reviews for a product. * * @param $id + * @param null $version * @return mixed + * @throws Exception */ - public static function getProductReviews($id) + public static function getProductReviews($id, $version = null) { - return self::getCollection('/products/' . $id . '/reviews/', 'ProductReview'); + return self::getCollection('/' . $id . '/reviews/', 'ProductReview', $version); } /** @@ -625,11 +654,18 @@ public static function getProductReviews($id) * @param int $product_id product id * @param int $id custom field id * @param mixed $object custom field to update + * @param null $version * @return mixed + * @throws Exception */ - public static function updateProductCustomField($product_id, $id, $object) + public static function updateProductCustomField($product_id, $id, $object, $version = null) { - return self::updateResource('/products/' . $product_id . '/custom_fields/' . $id, $object); + $temp_version = (is_null($version))?self::$version:$version; + if ($temp_version === "v2") { + return self::updateResource('/' . $product_id . '/custom_fields/' . $id, $object, 'ProductCustomField', $version); + } else { + return self::updateResource('/' . $product_id . '/custom-fields/' . $id, $object, 'ProductCustomField', $version); + } } /** @@ -637,11 +673,18 @@ public static function updateProductCustomField($product_id, $id, $object) * * @param int $product_id product id * @param int $id custom field id + * @param null $version * @return mixed + * @throws Exception */ - public static function deleteProductCustomField($product_id, $id) + public static function deleteProductCustomField($product_id, $id, $version = null) { - return self::deleteResource('/products/' . $product_id . '/custom_fields/' . $id); + $temp_version = (is_null($version))?self::$version:$version; + if ($temp_version === "v2") { + return self::deleteResource('/' . $product_id . '/custom_fields/' . $id, 'ProductCustomField', $version); + } else { + return self::deleteResource('/' . $product_id . '/custom-fields/' . $id, 'ProductCustomField', $version); + } } /** @@ -660,22 +703,30 @@ public static function getProductsCount($filter = array()) * Returns a single product resource by the given id. * * @param int $id product id + * @param null $version * @return Resources\Product|string + * @throws Exception */ - public static function getProduct($id) + public static function getProduct($id, $version = null) { - return self::getResource('/products/' . $id, 'Product'); + if (in_array($version, self::$available_versions)) { + return self::getResource('/' . $id, 'Product', $version); + } else { + throw new Exception("'version' not available"); + } } /** * Create a new product. * * @param mixed $object fields to create + * @param null $version * @return mixed + * @throws Exception */ - public static function createProduct($object) + public static function createProduct($object, $version = null) { - return self::createResource('/products', $object); + return self::createResource('', $object, 'Product', $version); } /** @@ -683,32 +734,64 @@ public static function createProduct($object) * * @param int $id product id * @param mixed $object fields to update + * @param $version + * @return mixed + * @throws Exception + */ + public static function updateProduct($id, $object, $version = null) + { + return self::updateResource('/' . $id, $object, 'Product', $version); + } + + /** + * Update the product Batch. + * + * @param int $id product id + * @param mixed $object fields to update + * @param $version * @return mixed + * @throws Exception */ - public static function updateProduct($id, $object) + public static function updateProducts($object, $version = null) { - return self::updateResource('/products/' . $id, $object); + return self::updateResource('', $object, 'Product', $version); } /** * Delete the given product. * * @param int $id product id + * @param null $version + * @return mixed + * @throws Exception + */ + public static function deleteProduct($id, $version = null) + { + return self::deleteResource('/' . $id, 'Product', $version); + } + + /** + * Delete All products. + * + * @param null $version * @return mixed + * @throws Exception */ - public static function deleteProduct($id) + public static function deleteProducts($version = null) { - return self::deleteResource('/products/' . $id); + return self::deleteResource('', 'Product', $version); } /** * Delete all products. * + * @param null $version * @return mixed + * @throws Exception */ - public static function deleteAllProducts() + public static function deleteAllProducts($version = null) { - return self::deleteResource('/products'); + return self::deleteResource('', 'Product', $version); } /** @@ -1226,6 +1309,129 @@ public static function deleteCartLineItem($cart_id, $line_item_id, $version = nu } } + /** + * Get All Wishlists. + * + * @param array $filter + * @param null $version + * @return mixed + * @throws Exception + */ + public static function getWishlists($filter = array(), $version = null) + { + $filter = Filter::create($filter); + if (in_array($version, self::$available_versions)) { + return self::getCollection($filter->toQuery(), 'Wishlist', $version); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * Get Wishlist by Wishlist Id. + * + * @param $id + * @param null $version + * @return mixed + * @throws Exception + */ + public static function getWishlist($id, $version = null) + { + if (in_array($version, self::$available_versions)) { + return self::getResource("/".$id, 'Wishlist', $version); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * Get Wishlist by Wishlist Id. + * + * @param array|object $object + * @param null $version + * @return mixed + * @throws Exception + */ + public static function createWishlist($object, $version = null) + { + if (in_array($version, self::$available_versions)) { + return self::createResource("", $object, 'Wishlist', $version); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * Get Wishlist by Wishlist Id. + * + * @param $id + * @param array|object $object + * @param null $version + * @return mixed + * @throws Exception + */ + public static function createWishlistItems($id, $object, $version = null) + { + if (in_array($version, self::$available_versions)) { + return self::createResource("/".$id."/items", $object, 'Wishlist', $version); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * Get Wishlist by Wishlist Id. + * + * @param $id + * @param array|object $object + * @param null $version + * @return mixed + * @throws Exception + */ + public static function updateWishlist($id, $object, $version = null) + { + if (in_array($version, self::$available_versions)) { + return self::updateResource("/".$id, $object, 'Wishlist', $version); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * Get Wishlist by Wishlist Id. + * + * @param $id + * @param null $version + * @return mixed + * @throws Exception + */ + public static function deleteWishlist($id, $version = null) + { + if (in_array($version, self::$available_versions)) { + return self::deleteResource("/".$id, 'Wishlist', $version); + } else { + throw new Exception("'version' not available"); + } + } + + /** + * Get Wishlist by Wishlist Id. + * + * @param $wishlist_id + * @param $item_id + * @param null $version + * @return mixed + * @throws Exception + */ + public static function deleteWishlistItem($wishlist_id, $item_id, $version = null) + { + if (in_array($version, self::$available_versions)) { + return self::deleteResource("/".$wishlist_id."/items/".$item_id, 'Wishlist', $version); + } else { + throw new Exception("'version' not available"); + } + } + /** * The collection of orders. * @@ -1544,12 +1750,29 @@ public static function getOrderStatuses() * Get collection of product skus * * @param array $filter + * @param null $version + * @return mixed + * @throws Exception + */ + public static function getSkus($filter = array(), $version = null) + { + $filter = Filter::create($filter); + return self::getCollection('/skus' . $filter->toQuery(), 'Sku', $version); + } + + /** + * Get collection of product skus + * + * @param $id + * @param array $filter + * @param null $version * @return mixed + * @throws Exception */ - public static function getSkus($filter = array()) + public static function getProductSkus($id, $filter = array(), $version = null) { $filter = Filter::create($filter); - return self::getCollection('/products/skus' . $filter->toQuery(), 'Sku'); + return self::getCollection('/'.$id.'/skus' . $filter->toQuery(), 'Sku', $version); } /** @@ -1884,11 +2107,13 @@ public static function getCurrencies($filter = array()) * * @param string $productId * @param mixed $object + * @param null $version * @return mixed + * @throws Exception */ - public static function createProductImage($productId, $object) + public static function createProductImage($productId, $object, $version = null) { - return self::createResource('/products/' . $productId . '/images', $object); + return self::createResource('/' . $productId . '/images', $object, 'ProductImage', $version); } /** @@ -1897,11 +2122,13 @@ public static function createProductImage($productId, $object) * @param string $productId * @param string $imageId * @param mixed $object + * @param null $version * @return mixed + * @throws Exception */ - public static function updateProductImage($productId, $imageId, $object) + public static function updateProductImage($productId, $imageId, $object, $version = null) { - return self::updateResource('/products/' . $productId . '/images/' . $imageId, $object); + return self::updateResource('/' . $productId . '/images/' . $imageId, $object, 'ProductImage', $version); } /** @@ -1909,11 +2136,13 @@ public static function updateProductImage($productId, $imageId, $object) * * @param int $productId * @param int $imageId + * @param null $version * @return Resources\ProductImage|string + * @throws Exception */ - public static function getProductImage($productId, $imageId) + public static function getProductImage($productId, $imageId, $version = null) { - return self::getResource('/products/' . $productId . '/images/' . $imageId, 'ProductImage'); + return self::getResource('/' . $productId . '/images/' . $imageId, 'ProductImage', $version); } /** @@ -1921,94 +2150,505 @@ public static function getProductImage($productId, $imageId) * * @param int $productId * @param int $imageId + * @param null $version * @return mixed + * @throws Exception */ - public static function deleteProductImage($productId, $imageId) + public static function deleteProductImage($productId, $imageId, $version = null) { - return self::deleteResource('/products/' . $productId . '/images/' . $imageId); + return self::deleteResource('/' . $productId . '/images/' . $imageId, 'ProductImage', $version); } /** - * Get all content pages + * Returns a product videos resource by the given product id. * - * @return mixed + * @param $id + * @param null $version + * @return Resources\ProductImage|string + * @throws Exception */ - public static function getPages() + public static function getProductVideos($id, $filter = array(), $version = null) { - return self::getCollection('/pages', 'Page'); + $filter = Filter::create($filter); + return self::getCollection('/' . $id . '/videos'.$filter->toQuery(), 'ProductVideo', $version); } /** - * Get single content pages + * Create a product videos resource by the given product id. * - * @param int $pageId - * @return mixed + * @param $id + * @param $object + * @param null $version + * @return Resources\ProductImage|string + * @throws Exception */ - public static function getPage($pageId) + public static function createProductVideo($id, $object, $version = null) { - return self::getResource('/pages/' . $pageId, 'Page'); + return self::createResource('/' . $id . '/videos', $object, 'ProductVideo', $version); } /** - * Create a new content pages + * Returns a product videos resource by the given product id. * - * @param $object - * @return mixed + * @param $product_id + * @param $id + * @param null $version + * @return Resources\ProductImage|string + * @throws Exception */ - public static function createPage($object) + public static function getProductVideo($product_id, $id, $version = null) { - return self::createResource('/pages', $object); + return self::getResource('/' . $product_id . '/videos/'.$id, 'ProductVideo', $version); } /** - * Update an existing content page + * Update a product videos resource by the given product id. * - * @param int $pageId + * @param $product_id + * @param $id * @param $object - * @return mixed + * @param null $version + * @return Resources\ProductImage|string + * @throws Exception */ - public static function updatePage($pageId, $object) + public static function updateProductVideo($product_id, $id, $object, $version = null) { - return self::updateResource('/pages/' . $pageId, $object); + return self::updateResource('/' . $product_id . '/videos/'.$id, $object, 'ProductVideo', $version); } /** - * Delete an existing content page + * Delete a product videos resource by the given product id. * - * @param int $pageId - * @return mixed + * @param $product_id + * @param $id + * @param null $version + * @return Resources\ProductImage|string + * @throws Exception */ - public static function deletePage($pageId) + public static function deleteProductVideo($product_id, $id, $version = null) { - return self::deleteResource('/pages/' . $pageId); + return self::deleteResource('/' . $product_id . '/videos/'.$id, 'ProductVideo', $version); } /** - * Create a Gift Certificate + * Returns a product bulk pricing rules resource by the given product id. * - * @param array $object - * @return mixed + * @param $id + * @param array $filter + * @param string $version + * @return Resources\ProductBulkPricingRule|string + * @throws Exception */ - public static function createGiftCertificate($object) + public static function getProductBulkPricingRules($id, $filter = array(), $version = "v3") { - return self::createResource('/gift_certificates', $object); + $filter = Filter::create($filter); + $temp_object = self::getCollection('/' . $id . '/bulk-pricing-rules'.$filter->toQuery(), 'ProductBulkPricingRule', $version); + if (gettype($temp_object) == "object") { + foreach ($temp_object as $obj) { + $obj->product_id = $id; + } + } + return $temp_object; } /** - * Get a Gift Certificate + * Returns a product bulk pricing rule resource by the given product id. * - * @param int $giftCertificateId - * @return mixed + * @param $product_id + * @param $id + * @param array $filter + * @param string $version + * @return Resources\ProductBulkPricingRule|string + * @throws Exception */ - public static function getGiftCertificate($giftCertificateId) + public static function getProductBulkPricingRule($product_id, $id, $filter = array(), $version = "v3") { - return self::getResource('/gift_certificates/' . $giftCertificateId); + $filter = Filter::create($filter); + $temp_object = self::getResource('/' . $product_id . '/bulk-pricing-rules/'.$id.$filter->toQuery(), 'ProductBulkPricingRule', $version); + if (gettype($temp_object) == "object") { + $temp_object->product_id = $product_id; + } + return $temp_object; } /** - * Return the collection of all gift certificates. + * Create a Bulk Pricing Rule resource by the given product id. * - * @param array $filter + * @param $id + * @param $object + * @param array $filter + * @param null $version + * @return Resources\ProductImage|string + * @throws Exception + */ + public static function createProductBulkPricingRule($id, $object, $filter = array(), $version = "v3") + { + $filter = Filter::create($filter); + return self::createResource('/' . $id . '/bulk-pricing-rules'.$filter->toQuery(), $object, 'ProductBulkPricingRule', $version); + } + + /** + * Update a Bulk Pricing Rule resource by the given product id. + * + * @param $product_id + * @param $id + * @param $object + * @param string $version + * @return Resources\ProductImage|string + * @throws Exception + */ + public static function updateProductBulkPricingRule($product_id, $id, $object, $version = "v3") + { + return self::updateResource('/' . $product_id . '/bulk-pricing-rules/'.$id, $object, 'ProductBulkPricingRule', $version); + } + + /** + * Update a Bulk Pricing Rule resource by the given product id. + * + * @param $product_id + * @param $id + * @param string $version + * @return Resources\ProductImage|string + * @throws Exception + */ + public static function deleteProductBulkPricingRule($product_id, $id, $version = "v3") + { + return self::deleteResource('/' . $product_id . '/bulk-pricing-rules/'.$id, 'ProductBulkPricingRule', $version); + } + + /** + * Returns a product bulk pricing rules resource by the given product id. + * + * @param $id + * @param array $filter + * @param string $version + * @return Resources\ProductBulkPricingRule|string + * @throws Exception + */ + public static function getProductComplexRules($id, $filter = array(), $version = "v3") + { + $filter = Filter::create($filter); + return self::getCollection('/' . $id . '/complex-rules'.$filter->toQuery(), 'ProductComplexRule', $version); + } + + /** + * Returns a product bulk pricing rule resource by the given product id. + * + * @param $product_id + * @param $id + * @param array $filter + * @param string $version + * @return Resources\ProductBulkPricingRule|string + * @throws Exception + */ + public static function getProductComplexRule($product_id, $id, $filter = array(), $version = "v3") + { + $filter = Filter::create($filter); + return self::getResource('/' . $product_id . '/complex-rules/'.$id.$filter->toQuery(), 'ProductComplexRule', $version); + } + + /** + * Create a Complex Rule resource by the given product id. + * + * @param $id + * @param $object + * @param array $filter + * @param string $version + * @return Resources\ProductImage|string + * @throws Exception + */ + public static function createProductComplexRule($id, $object, $version = "v3") + { + return self::createResource('/' . $id . '/complex-rules', $object, 'ProductComplexRule', $version); + } + + /** + * Update a Complex Rule resource by the given product id. + * + * @param $product_id + * @param $id + * @param $object + * @param string $version + * @return Resources\ProductImage|string + * @throws Exception + */ + public static function updateProductComplexRule($product_id, $id, $object, $version = "v3") + { + return self::updateResource('/' . $product_id . '/complex-rules/'.$id, $object, 'ProductComplexRule', $version); + } + + /** + * Update a Complex Rule resource by the given product id. + * + * @param $product_id + * @param $id + * @param string $version + * @return Resources\ProductImage|string + * @throws Exception + */ + public static function deleteProductComplexRule($product_id, $id, $version = "v3") + { + return self::deleteResource('/' . $product_id . '/complex-rules/'.$id, 'ProductComplexRule', $version); + } + + /** + * Returns a product variants resource by the given product id. + * + * @param $id + * @param array $filter + * @param string $version + * @return Resources\ProductBulkPricingRule|string + * @throws Exception + */ + public static function getProductVariants($id, $filter = array(), $version = "v3") + { + $filter = Filter::create($filter); + return self::getCollection('/' . $id . '/variants'.$filter->toQuery(), 'ProductVariant', $version); + } + + /** + * Returns a product variant resource by the given product id. + * + * @param $product_id + * @param $id + * @param array $filter + * @param string $version + * @return Resources\ProductBulkPricingRule|string + * @throws Exception + */ + public static function getProductVariant($product_id, $id, $filter = array(), $version = "v3") + { + $filter = Filter::create($filter); + return self::getResource('/' . $product_id . '/variants/'.$id.$filter->toQuery(), 'ProductVariant', $version); + } + + /** + * Returns a product variant resource by the given product id. + * + * @param $product_id + * @param $object + * @param string $version + * @return Resources\ProductBulkPricingRule|string + * @throws Exception + */ + public static function createProductVariant($product_id, $object, $version = "v3") + { + return self::createResource('/' . $product_id . '/variants', $object, 'ProductVariant', $version); + } + + /** + * Returns a product variant resource by the given product id. + * + * @param $product_id + * @param $id + * @param $image_url + * @param string $version + * @return Resources\ProductBulkPricingRule|string + * @throws Exception + */ + public static function createProductVariantImage($product_id, $id, $image_url, $version = "v3") + { + return self::createResource('/' . $product_id . '/variants/'.$id.'/image', array("image_url" => $image_url), 'ProductVariant', $version); + } + + /** + * Update a product variant resource by the given product id. + * + * @param $product_id + * @param $id + * @param $object + * @param string $version + * @return Resources\ProductBulkPricingRule|string + * @throws Exception + */ + public static function updateProductVariant($product_id, $id, $object, $version = "v3") + { + return self::updateResource('/' . $product_id . '/variants/'.$id, $object, 'ProductVariant', $version); + } + + /** + * Delete a product variant resource by the given product id. + * + * @param $product_id + * @param $id + * @param string $version + * @return Resources\ProductBulkPricingRule|string + * @throws Exception + */ + public static function deleteProductVariant($product_id, $id, $version = "v3") + { + return self::deleteResource('/' . $product_id . '/variants/'.$id, 'ProductVariant', $version); + } + + /** + * Returns a product variants resource by the given product id. + * + * @param $product_id + * @param $variant_id + * @param array $filter + * @param string $version + * @return Resources\ProductBulkPricingRule|string + * @throws Exception + */ + public static function getProductVariantMetafields($product_id, $variant_id, $filter = array(), $version = "v3") + { + $filter = Filter::create($filter); + $temp_object = self::getCollection('/' . $product_id . '/variants/'.$variant_id.'/metafields'.$filter->toQuery(), 'ProductVariantMetafield', $version); + if (gettype($temp_object) == "object") { + foreach ($temp_object as $obj) { + $obj->product_id = $product_id; + } + } + return $temp_object; + } + + /** + * Returns a product variant resource by the given product id. + * + * @param $product_id + * @param $id + * @param array $filter + * @param string $version + * @return Resources\ProductBulkPricingRule|string + * @throws Exception + */ + public static function getProductVariantMetafield($product_id, $variant_id, $id, $filter = array(), $version = "v3") + { + $filter = Filter::create($filter); + $temp_object = self::getResource('/' . $product_id . '/variants/'.$variant_id.'/metafields/'.$id.$filter->toQuery(), 'ProductVariantMetafield', $version); + if (gettype($temp_object) == "object") { + $temp_object->product_id = $product_id; + } + return $temp_object; + } + + /** + * Returns a product variant resource by the given product id. + * + * @param $product_id + * @param $variant_id + * @param $object + * @param string $version + * @return Resources\ProductBulkPricingRule|string + * @throws Exception + */ + public static function createProductVariantMetafield($product_id, $variant_id, $object, $version = "v3") + { + return self::createResource('/' . $product_id . '/variants/'.$variant_id.'/metafields', $object, 'ProductVariantMetafield', $version); + } + + /** + * Returns a product variant resource by the given product id. + * + * @param $product_id + * @param $variant_id + * @param $id + * @param $object + * @param string $version + * @return Resources\ProductBulkPricingRule|string + * @throws Exception + */ + public static function updateProductVariantMetafield($product_id, $variant_id, $id, $object, $version = "v3") + { + return self::updateResource('/' . $product_id . '/variants/'.$variant_id.'/metafields/'.$id, $object, 'ProductVariantMetafield', $version); + } + + /** + * Returns a product variant resource by the given product id. + * + * @param $product_id + * @param $variant_id + * @param $id + * @param string $version + * @return Resources\ProductBulkPricingRule|string + * @throws Exception + */ + public static function deleteProductVariantMetafield($product_id, $variant_id, $id, $version = "v3") + { + return self::deleteResource('/' . $product_id . '/variants/'.$variant_id.'/metafields/'.$id, 'ProductVariantMetafield', $version); + } + + /** + * Get all content pages + * + * @return mixed + */ + public static function getPages() + { + return self::getCollection('/pages', 'Page'); + } + + /** + * Get single content pages + * + * @param int $pageId + * @return mixed + */ + public static function getPage($pageId) + { + return self::getResource('/pages/' . $pageId, 'Page'); + } + + /** + * Create a new content pages + * + * @param $object + * @return mixed + */ + public static function createPage($object) + { + return self::createResource('/pages', $object); + } + + /** + * Update an existing content page + * + * @param int $pageId + * @param $object + * @return mixed + */ + public static function updatePage($pageId, $object) + { + return self::updateResource('/pages/' . $pageId, $object); + } + + /** + * Delete an existing content page + * + * @param int $pageId + * @return mixed + */ + public static function deletePage($pageId) + { + return self::deleteResource('/pages/' . $pageId); + } + + /** + * Create a Gift Certificate + * + * @param array $object + * @return mixed + */ + public static function createGiftCertificate($object) + { + return self::createResource('/gift_certificates', $object); + } + + /** + * Get a Gift Certificate + * + * @param int $giftCertificateId + * @return mixed + */ + public static function getGiftCertificate($giftCertificateId) + { + return self::getResource('/gift_certificates/' . $giftCertificateId); + } + + /** + * Return the collection of all gift certificates. + * + * @param array $filter * @return mixed */ public static function getGiftCertificates($filter = array()) @@ -2145,11 +2785,13 @@ public static function createCustomerAddress($customerID, $object) * * @param int $productID * @param array $object + * @param null $version * @return mixed + * @throws Exception */ - public static function createProductRule($productID, $object) + public static function createProductRule($productID, $object, $version = "v2") { - return self::createResource('/products/' . $productID . '/rules', $object); + return self::createResource('/' . $productID . '/rules', $object, 'Rule', $version); } /** @@ -2207,24 +2849,167 @@ public static function deleteAllOptions() /** * Return the collection of all option values for a given option. * - * @param int $productId + * @param $id + * @param array $filter + * @param null $version * @return mixed + * @throws Exception */ - public static function getProductOptions($productId) + public static function getProductOptions($id, $filter = array(), $version = null) { - return self::getCollection('/products/' . $productId . '/options'); + $filter = Filter::create($filter); + return self::getCollection('/' . $id . '/options'.$filter->toQuery(), 'ProductOption', $version); } /** * Return the collection of all option values for a given option. * - * @param int $productId - * @param int $productOptionId + * @param $product_id + * @param $id + * @param array $filter + * @param null $version * @return mixed + * @throws Exception + */ + public static function getProductOption($product_id, $id, $filter = array(), $version = null) + { + $filter = Filter::create($filter); + return self::getResource('/' . $product_id . '/options/' . $id.$filter->toQuery(), 'ProductOption', $version); + } + + /** + * Return the collection of all option values for a given option. + * + * @param $product_id + * @param $id + * @param $object + * @param null $version + * @return mixed + * @throws Exception */ - public static function getProductOption($productId, $productOptionId) + public static function createProductOption($product_id, $id, $object, $version = null) { - return self::getResource('/products/' . $productId . '/options/' . $productOptionId); + return self::createResource('/' . $product_id . '/options/' . $id, $object, 'ProductOption', $version); + } + + /** + * Return the collection of all option values for a given option. + * + * @param $product_id + * @param $id + * @param $object + * @param null $version + * @return mixed + * @throws Exception + */ + public static function updateProductOption($product_id, $id, $object, $version = null) + { + return self::updateResource('/' . $product_id . '/options/' . $id, $object, 'ProductOption', $version); + } + + /** + * Return the collection of all option values for a given option. + * + * @param $product_id + * @param $id + * @param null $version + * @return mixed + * @throws Exception + */ + public static function deleteProductOption($product_id, $id, $version = null) + { + return self::deleteResource('/' . $product_id . '/options/' . $id, 'ProductOption', $version); + } + + /** + * Return the collection of all option values for a given option. + * + * @param $product_id + * @param $option_id + * @param array $filter + * @param null $version + * @return mixed + * @throws Exception + */ + public static function getProductOptionValues($product_id, $option_id, $filter = array(), $version = null) + { + $filter = Filter::create($filter); + $temp_object = self::getCollection('/' . $product_id . '/options/'.$option_id.'/values'.$filter->toQuery(), 'ProductOptionValue', $version); + if (gettype($temp_object) == "object") { + foreach ($temp_object as $obj) { + $obj->product_id = $product_id; + $obj->option_id = $option_id; + } + } + return $temp_object; + } + + /** + * Return the collection of all option values for a given option. + * + * @param $product_id + * @param $option_id + * @param $id + * @param array $filter + * @param null $version + * @return mixed + * @throws Exception + */ + public static function getProductOptionValue($product_id, $option_id, $id, $filter = array(), $version = null) + { + $filter = Filter::create($filter); + $temp_object = self::getResource('/' . $product_id . '/options/'.$option_id.'/values/'.$id.$filter->toQuery(), 'ProductOptionValue', $version); + if (gettype($temp_object) == "object") { + $temp_object->product_id = $product_id; + $temp_object->option_id = $option_id; + } + return $temp_object; + } + + /** + * Return the collection of all option values for a given option. + * + * @param $product_id + * @param $id + * @param $object + * @param null $version + * @return mixed + * @throws Exception + */ + public static function createProductOptionValue($product_id, $option_id, $object, $version = null) + { + return self::createResource('/' . $product_id . '/options/' . $option_id.'/values', $object, 'ProductOptionValue', $version); + } + + /** + * Return the collection of all option values for a given option. + * + * @param $product_id + * @param $option_id + * @param $id + * @param $object + * @param null $version + * @return mixed + * @throws Exception + */ + public static function updateProductOptionValue($product_id, $option_id, $id, $object, $version = null) + { + return self::updateResource('/' . $product_id . '/options/' . $option_id.'/values/'.$id, $object, 'ProductOptionValue', $version); + } + + /** + * Return the collection of all option values for a given option. + * + * @param $product_id + * @param $option_id + * @param $id + * @param null $version + * @return mixed + * @throws Exception + */ + public static function deleteProductOptionValue($product_id, $option_id, $id, $version = null) + { + return self::deleteResource('/' . $product_id . '/options/' . $option_id.'/values/'.$id, 'ProductOptionValue', $version); } /** @@ -2232,11 +3017,26 @@ public static function getProductOption($productId, $productOptionId) * * @param int $productId * @param int $productRuleId + * @param null $version * @return mixed + * @throws Exception */ - public static function getProductRule($productId, $productRuleId) + public static function getProductRule($productId, $productRuleId, $version = "v2") { - return self::getResource('/products/' . $productId . '/rules/' . $productRuleId); + return self::getResource('/' . $productId . '/rules/' . $productRuleId, 'Rule', $version); + } + + /** + * Return the collection of all option values for a given option. + * + * @param int $productId + * @param null $version + * @return mixed + * @throws Exception + */ + public static function getProductRules($productId, $version = "v2") + { + return self::getResource('/' . $productId . '/rules', 'Rule', $version); } /** @@ -2406,11 +3206,13 @@ public static function deleteShippingMethod($zoneId, $methodId) * * @param $productId * @param array $filter + * @param string $version * @return mixed + * @throws Exception */ - public static function getSkusByProduct($productId, $filter = array()) + public static function getSkusByProduct($productId, $filter = array(), $version = "v2") { $filter = Filter::create($filter); - return self::getCollection('/products/'.$productId.'/skus' . $filter->toQuery(), 'Sku'); + return self::getCollection('/'.$productId.'/skus' . $filter->toQuery(), 'Sku', $version); } } diff --git a/src/Bigcommerce/Api/Resources/Option.php b/src/Bigcommerce/Api/Resources/Option.php index 4c4b76b9..cb2bd89b 100644 --- a/src/Bigcommerce/Api/Resources/Option.php +++ b/src/Bigcommerce/Api/Resources/Option.php @@ -12,6 +12,6 @@ class Option extends Resource { public function values() { - return Client::getCollection($this->fields->values->resource, 'OptionValue'); + return Client::getCollection($this->fields->values->resource, 'OptionValue', "v2"); } } diff --git a/src/Bigcommerce/Api/Resources/Product.php b/src/Bigcommerce/Api/Resources/Product.php index 3c698249..e1bc4e38 100644 --- a/src/Bigcommerce/Api/Resources/Product.php +++ b/src/Bigcommerce/Api/Resources/Product.php @@ -40,73 +40,135 @@ class Product extends Resource 'tax_class', ); + public $urls = array( + "v2" => "/products", + "v3" => "/catalog/products" + ); + protected $ignoreIfZero = array( 'tax_class_id', ); - public function brand() + protected function getProductId() + { + return $this->id; + } + + public function brand($version = null) { - return Client::getResource($this->fields->brand->resource, 'Brand'); + return Client::getBrand($this->brand_id, $version); } - public function images() + public function images($id = null, $filter = array(), $version = null) { - return Client::getCollection($this->fields->images->resource, 'ProductImage'); + if (is_null($id)) { + return Client::getProductImages($this->id, $filter, $version); + } else { + return Client::getProductImage($this->id, $id, $version); + } } - public function skus() + public function skus($filter = array(), $version = "v2") { - return Client::getCollection($this->fields->skus->resource, 'Sku'); + return Client::getProductSkus($this->id, $filter, $version); } - public function rules() + public function rules($id, $version = "v2") { - return Client::getCollection($this->fields->rules->resource, 'Rule'); + if (is_null($id)) { + return Client::getProductRules($this->id, $version); + } else { + return Client::getProductRule($this->id, $id, $version); + } } - public function videos() + public function videos($id = null, $filter = array(), $version = null) { - return Client::getCollection($this->fields->videos->resource, 'ProductVideo'); + if (is_null($id)) { + return Client::getProductVideos($this->id, $filter, $version); + } else { + return Client::getProductVideo($this->id, $id, $version); + } } - public function custom_fields() + public function bulk_pricing_rules($id = null, $filter = array(), $version = 'v3') { - return Client::getCollection($this->fields->custom_fields->resource, 'ProductCustomField'); + if (is_null($id)) { + return Client::getProductBulkPricingRules($this->id, $filter, $version); + } else { + return Client::getProductBulkPricingRule($this->id, $id, $filter, $version); + } + } + + public function complex_rules($id = null, $filter = array(), $version = 'v3') + { + if (is_null($id)) { + return Client::getProductComplexRules($this->id, $filter, $version); + } else { + return Client::getProductComplexRule($this->id, $id, $filter, $version); + } + } + + public function variants($id = null, $filter = array(), $version = 'v3') + { + if (is_null($id)) { + return Client::getProductVariants($this->id, $filter, $version); + } else { + return Client::getProductVariant($this->id, $id, $filter, $version); + } + } + + public function custom_fields($id = null, $version = null) + { + if (is_null($id)) { + return Client::getProductCustomFields($this->id, $version); + } else { + return Client::getProductCustomField($this->id, $id, $version); + } } public function configurable_fields() { - return Client::getCollection($this->fields->configurable_fields->resource, 'ProductConfigurableField'); + return Client::getCollection($this->fields->configurable_fields->resource, 'ProductConfigurableField', "v2"); } public function discount_rules() { - return Client::getCollection($this->fields->discount_rules->resource, 'DiscountRule'); + return Client::getCollection($this->fields->discount_rules->resource, 'DiscountRule', "v2"); } public function option_set() { - return Client::getResource($this->fields->option_set->resource, 'OptionSet'); + return Client::getResource($this->fields->option_set->resource, 'OptionSet', "v2"); + } + + public function options($id, $filter = array(), $version = "v3") + { + if (is_null($id)) { + return Client::getProductOptions($this->id, $filter, $version); + } else { + return Client::getProductOption($this->id, $id, $filter, $version); + } } - public function options() + public function reviews($version = null) { - return Client::getCollection('/products/' . $this->id . '/options', 'ProductOption'); + return Client::getProductReviews('/' . $this->id . '/reviews', $version); } - public function create() + public function create($version = null) { - return Client::createProduct($this->getCreateFields()); + return Client::createProduct($this->getCreateFields(), $version); } - public function update() + public function update($version = null) { - return Client::updateProduct($this->id, $this->getUpdateFields()); + return Client::updateProduct($this->id, $this->getUpdateFields(), $version); } - public function delete() + public function delete($version = null) { - return Client::deleteProduct($this->id); + return Client::deleteProduct($this->id, $version); } public function tax_class() diff --git a/src/Bigcommerce/Api/Resources/ProductCustomField.php b/src/Bigcommerce/Api/Resources/ProductCustomField.php index be663eb0..a0126c6e 100644 --- a/src/Bigcommerce/Api/Resources/ProductCustomField.php +++ b/src/Bigcommerce/Api/Resources/ProductCustomField.php @@ -20,18 +20,23 @@ class ProductCustomField extends Resource 'product_id' ); - public function create() + public $urls = array( + "v2" => "/products", + "v3" => "/catalog/products" + ); + + public function create($version = null) { - return Client::createResource('/products/' . $this->fields->product_id . '/customfields', $this->getCreateFields()); + return Client::createProductCustomField($this->fields->product_id, $this->getCreateFields(), $version); } - public function update() + public function update($version = null) { - Client::updateResource('/products/' . $this->fields->product_id . '/customfields/' . $this->id, $this->getUpdateFields()); + Client::updateProductCustomField($this->fields->product_id, $this->id, $this->getUpdateFields(), $version); } - public function delete() + public function delete($version = null) { - Client::deleteResource('/products/' . $this->fields->product_id . '/customfields/' . $this->id); + Client::deleteProductCustomField($this->fields->product_id, $this->id, $version); } } diff --git a/src/Bigcommerce/Api/Resources/ProductImage.php b/src/Bigcommerce/Api/Resources/ProductImage.php index a589629b..eb76674d 100644 --- a/src/Bigcommerce/Api/Resources/ProductImage.php +++ b/src/Bigcommerce/Api/Resources/ProductImage.php @@ -22,18 +22,23 @@ class ProductImage extends Resource 'product_id', ); - public function create() + public $urls = array( + "v2" => "/products", + "v3" => "/catalog/products" + ); + + public function create($version = null) { - return Client::createProductImage($this->product_id, $this->getCreateFields()); + return Client::createProductImage($this->product_id, $this->getCreateFields(), $version); } - public function update() + public function update($version = null) { - return Client::updateProductImage($this->product_id, $this->id, $this->getUpdateFields()); + return Client::updateProductImage($this->product_id, $this->id, $this->getUpdateFields(), $version); } - public function delete() + public function delete($version = null) { - return Client::deleteProductImage($this->product_id, $this->id); + return Client::deleteProductImage($this->product_id, $this->id, $version); } } diff --git a/src/Bigcommerce/Api/Resources/ProductOption.php b/src/Bigcommerce/Api/Resources/ProductOption.php index 9af59913..4c4bfb5c 100644 --- a/src/Bigcommerce/Api/Resources/ProductOption.php +++ b/src/Bigcommerce/Api/Resources/ProductOption.php @@ -10,12 +10,49 @@ */ class ProductOption extends Resource { + protected $ignoreOnCreate = array( + 'id' + ); + + protected $ignoreOnUpdate = array( + 'id' + ); + protected $fieldMap = array( 'option' => 'option_id' ); + public $urls = array( + "v2" => "/products", + "v3" => "/catalog/products" + ); + public function option() { - return Client::getResource('/options/' . $this->fields->option_id, 'Option'); + return Client::getResource('/options/' . $this->fields->option_id, 'Option', "v2"); + } + + public function values($id = null, $filter = array(), $version = "v3") + { + if (is_null($id)) { + return Client::getProductOptionValues($this->product_id, $this->id, $filter, $version); + } else { + return Client::getProductOptionValue($this->product_id, $this->id, $id, $filter, $version); + } + } + + public function create($version = "v3") + { + return Client::createProductOption($this->product_id, $this->getCreateFields(), $version); + } + + public function update($version = "v3") + { + return Client::updateProductOption($this->product_id, $this->id, $this->getUpdateFields(), $version); + } + + public function delete($version = "v3") + { + return Client::deleteProductOption($this->product_id, $this->id, $version); } } diff --git a/src/Bigcommerce/Api/Resources/ProductReview.php b/src/Bigcommerce/Api/Resources/ProductReview.php index 2170b301..5e6fa957 100644 --- a/src/Bigcommerce/Api/Resources/ProductReview.php +++ b/src/Bigcommerce/Api/Resources/ProductReview.php @@ -10,4 +10,8 @@ */ class ProductReview extends Resource { + public $urls = array( + "v2" => "/products", + "v3" => "/catalog/products" + ); } diff --git a/src/Bigcommerce/Api/Resources/ProductVideo.php b/src/Bigcommerce/Api/Resources/ProductVideo.php index d5d5b955..3d2c947a 100644 --- a/src/Bigcommerce/Api/Resources/ProductVideo.php +++ b/src/Bigcommerce/Api/Resources/ProductVideo.php @@ -10,4 +10,33 @@ */ class ProductVideo extends Resource { + protected $ignoreOnCreate = array( + 'product_id', + ); + + protected $ignoreOnUpdate = array( + 'id', + 'product_id', + 'length' + ); + + public $urls = array( + "v2" => "/products", + "v3" => "/catalog/products" + ); + + public function create($version = null) + { + return Client::createProductVideo($this->product_id, $this->getCreateFields(), $version); + } + + public function update($version = null) + { + return Client::updateProductVideo($this->product_id, $this->id, $this->getUpdateFields(), $version); + } + + public function delete($version = null) + { + return Client::deleteProductVideo($this->product_id, $this->id, $version); + } } diff --git a/src/Bigcommerce/Api/Resources/Rule.php b/src/Bigcommerce/Api/Resources/Rule.php index 2455dcdb..f134bf15 100644 --- a/src/Bigcommerce/Api/Resources/Rule.php +++ b/src/Bigcommerce/Api/Resources/Rule.php @@ -20,6 +20,10 @@ class Rule extends Resource 'product_id', ); + public $urls = array( + "v2" => "/products" + ); + public function conditions() { $conditions = Client::getCollection($this->fields->conditions->resource, 'RuleCondition'); @@ -33,11 +37,11 @@ public function conditions() public function create() { - return Client::createResource('/products/' . $this->fields->product_id . '/rules', $this->getCreateFields()); + return Client::createResource('/' . $this->fields->product_id . '/rules', $this->getCreateFields(), 'Rule', 'v2'); } public function update() { - Client::updateResource('/products/' . $this->fields->product_id . '/rules/' . $this->fields->id, $this->getUpdateFields()); + Client::updateResource('/' . $this->fields->product_id . '/rules/' . $this->fields->id, $this->getUpdateFields(), 'Rule', 'v2'); } } diff --git a/src/Bigcommerce/Api/Resources/RuleCondition.php b/src/Bigcommerce/Api/Resources/RuleCondition.php index 9ba5c954..b4b82ae0 100644 --- a/src/Bigcommerce/Api/Resources/RuleCondition.php +++ b/src/Bigcommerce/Api/Resources/RuleCondition.php @@ -21,13 +21,18 @@ class RuleCondition extends Resource public $product_id; + public $urls = array( + "v2" => "/products" + ); + + public function create() { - return Client::createResource('/products/' . $this->product_id . '/rules/' . $this->fields->rule_id . '/conditions', $this->getCreateFields()); + return Client::createResource('/' . $this->product_id . '/rules/' . $this->fields->rule_id . '/conditions', $this->getCreateFields(), 'RuleCondition', "v2"); } public function update() { - Client::updateResource('/products/' . $this->product_id . '/rules/' . $this->fields->rule_id . '/conditions/' . $this->id, $this->getUpdateFields()); + Client::updateResource('/' . $this->product_id . '/rules/' . $this->fields->rule_id . '/conditions/' . $this->id, $this->getUpdateFields(), 'RuleCondition', 'v3'); } } diff --git a/src/Bigcommerce/Api/Resources/Sku.php b/src/Bigcommerce/Api/Resources/Sku.php index 358b6e64..e31ffb44 100644 --- a/src/Bigcommerce/Api/Resources/Sku.php +++ b/src/Bigcommerce/Api/Resources/Sku.php @@ -19,6 +19,10 @@ class Sku extends Resource 'product_id', ); + public $urls = array( + "v2" => "/products" + ); + public function options() { $options = array(); @@ -36,11 +40,11 @@ public function options() public function create() { - return Client::createResource('/products/' . $this->product_id . '/skus', $this->getCreateFields()); + return Client::createResource('/' . $this->product_id . '/skus', $this->getCreateFields(), 'Sku', 'v2'); } public function update() { - Client::updateResource('/products/' . $this->product_id . '/skus/' . $this->id, $this->getUpdateFields()); + Client::updateResource('/' . $this->product_id . '/skus/' . $this->id, $this->getUpdateFields(), 'Sku', 'v2'); } } diff --git a/src/Bigcommerce/Api/Resources/SkuOption.php b/src/Bigcommerce/Api/Resources/SkuOption.php index 88ab7524..d4dd583d 100644 --- a/src/Bigcommerce/Api/Resources/SkuOption.php +++ b/src/Bigcommerce/Api/Resources/SkuOption.php @@ -19,13 +19,17 @@ class SkuOption extends Resource 'sku_id', ); + public $urls = array( + "v2" => "/products" + ); + public function create() { - return Client::createResource('/products/' . $this->fields->product_id . '/skus/' . $this->fields->sku_id . '/options', $this->getCreateFields()); + return Client::createResource('/' . $this->fields->product_id . '/skus/' . $this->fields->sku_id . '/options', $this->getCreateFields(), 'SkuOption', 'v2'); } public function update() { - Client::updateResource('/products/' . $this->fields->product_id . '/skus/' . $this->fields->sku_id . '/options/' . $this->id, $this->getUpdateFields()); + Client::updateResource('/' . $this->fields->product_id . '/skus/' . $this->fields->sku_id . '/options/' . $this->id, $this->getUpdateFields(), 'SkuOption', 'v2'); } } From 016b594ef3806aef53a417806539b6fa0082b043 Mon Sep 17 00:00:00 2001 From: naveenrajbu Date: Sun, 12 Sep 2021 23:31:29 +0530 Subject: [PATCH 08/12] Update Catalog\Products and Wishlists Files added --- .../Api/Resources/ProductBulkPricingRule.php | 39 ++++++++++++ .../Api/Resources/ProductComplexRule.php | 37 +++++++++++ .../Api/Resources/ProductOptionValue.php | 42 +++++++++++++ .../Api/Resources/ProductVariant.php | 62 +++++++++++++++++++ .../Api/Resources/ProductVariantMetafield.php | 40 ++++++++++++ src/Bigcommerce/Api/Resources/Wishlist.php | 47 ++++++++++++++ 6 files changed, 267 insertions(+) create mode 100644 src/Bigcommerce/Api/Resources/ProductBulkPricingRule.php create mode 100644 src/Bigcommerce/Api/Resources/ProductComplexRule.php create mode 100644 src/Bigcommerce/Api/Resources/ProductOptionValue.php create mode 100644 src/Bigcommerce/Api/Resources/ProductVariant.php create mode 100644 src/Bigcommerce/Api/Resources/ProductVariantMetafield.php create mode 100644 src/Bigcommerce/Api/Resources/Wishlist.php diff --git a/src/Bigcommerce/Api/Resources/ProductBulkPricingRule.php b/src/Bigcommerce/Api/Resources/ProductBulkPricingRule.php new file mode 100644 index 00000000..2306c659 --- /dev/null +++ b/src/Bigcommerce/Api/Resources/ProductBulkPricingRule.php @@ -0,0 +1,39 @@ + "/catalog/products" + ); + + public function create($filter = array(), $version = "v3") + { + return Client::createProductBulkPricingRule($this->product_id, $this->getCreateFields(), $filter, $version); + } + + public function update($version = "v3") + { + return Client::updateProductBulkPricingRule($this->product_id, $this->id, $this->getUpdateFields(), $version); + } + + public function delete($version = "v3") + { + return Client::deleteProductBulkPricingRule($this->product_id, $this->id, $version); + } +} diff --git a/src/Bigcommerce/Api/Resources/ProductComplexRule.php b/src/Bigcommerce/Api/Resources/ProductComplexRule.php new file mode 100644 index 00000000..3d1fa895 --- /dev/null +++ b/src/Bigcommerce/Api/Resources/ProductComplexRule.php @@ -0,0 +1,37 @@ + "/catalog/products" + ); + + public function create($version = "v3") + { + return Client::createProductComplexRule($this->product_id, $this->getCreateFields(), $version); + } + + public function update($version = "v3") + { + return Client::updateProductComplexRule($this->product_id, $this->id, $this->getUpdateFields(), $version); + } + + public function delete($version = "v3") + { + return Client::deleteProductComplexRule($this->product_id, $this->id, $version); + } +} \ No newline at end of file diff --git a/src/Bigcommerce/Api/Resources/ProductOptionValue.php b/src/Bigcommerce/Api/Resources/ProductOptionValue.php new file mode 100644 index 00000000..20901e1f --- /dev/null +++ b/src/Bigcommerce/Api/Resources/ProductOptionValue.php @@ -0,0 +1,42 @@ + "/catalog/products" + ); + + public function create($version = "v3") + { + return Client::createProductOptionValue($this->product_id, $this->option_id, $this->getCreateFields(), $version); + } + + public function update($version = "v3") + { + return Client::updateProductOptionValue($this->product_id, $this->option_id, $this->id, $this->getUpdateFields(), $version); + } + + public function delete($version = "v3") + { + return Client::deleteProductOptionValue($this->product_id, $this->option_id, $this->id, $version); + } +} diff --git a/src/Bigcommerce/Api/Resources/ProductVariant.php b/src/Bigcommerce/Api/Resources/ProductVariant.php new file mode 100644 index 00000000..7d105352 --- /dev/null +++ b/src/Bigcommerce/Api/Resources/ProductVariant.php @@ -0,0 +1,62 @@ + "/catalog/products" + ); + + public function meta_fields($id = null, $filter = array(), $version = "v3") + { + if (is_null($id)) { + return Client::getProductVariantMetafields($this->product_id, $this->id, $filter, $version); + } else { + return Client::getProductVariantMetafield($this->product_id, $this->id, $id, $filter, $version); + } + } + + public function create($filter = array(), $version = "v3") + { + return Client::createProductVariant($this->product_id, $this->getCreateFields(), $version); + } + + public function create_image($image_url, $version = "v3") + { + return Client::createProductVariantImage($this->product_id, $this->id, $image_url, $version); + } + + public function update($version = "v3") + { + return Client::updateProductVariant($this->product_id, $this->id, $this->getUpdateFields(), $version); + } + + public function delete($version = "v3") + { + return Client::deleteProductVariant($this->product_id, $this->id, $version); + } +} diff --git a/src/Bigcommerce/Api/Resources/ProductVariantMetafield.php b/src/Bigcommerce/Api/Resources/ProductVariantMetafield.php new file mode 100644 index 00000000..4bdd748f --- /dev/null +++ b/src/Bigcommerce/Api/Resources/ProductVariantMetafield.php @@ -0,0 +1,40 @@ + "/catalog/products" + ); + + public function create($version = "v3") + { + return Client::createProductVariantMetafield($this->product_id, $this->resource_id, $this->getCreateFields(), $version); + } + + public function update($version = "v3") + { + return Client::updateProductVariantMetafield($this->product_id, $this->resource_id, $this->id, $this->getUpdateFields(), $version); + } + + public function delete($version = "v3") + { + return Client::deleteProductVariantMetafield($this->product_id, $this->resource_id, $this->id, $version); + } + +} \ No newline at end of file diff --git a/src/Bigcommerce/Api/Resources/Wishlist.php b/src/Bigcommerce/Api/Resources/Wishlist.php new file mode 100644 index 00000000..4f4d5aca --- /dev/null +++ b/src/Bigcommerce/Api/Resources/Wishlist.php @@ -0,0 +1,47 @@ + "/wishlists" + ); + + public function create($version = "v3") + { + return Client::createWishlist($this->getCreateFields(), $version); + } + + public function update($version = "v3") + { + return Client::updateWishlist($this->id, $this->getUpdateFields(), $version); + } + + public function delete($version = "v3") + { + return Client::deleteWishlist($this->id, $version); + } + + public function addItems($object, $version = "v3") + { + return Client::createWishlistItems($this->id, $object, $version); + } + + public function toJson() + { + return parent::toJson(); + } +} From 6d2290d720456fcb261ea6a44ab13c783b56bec0 Mon Sep 17 00:00:00 2001 From: Naveen Raj <49157455+naveenrajbu@users.noreply.github.com> Date: Mon, 13 Sep 2021 08:20:48 +0530 Subject: [PATCH 09/12] Update README.md --- README.md | 228 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 138 insertions(+), 90 deletions(-) diff --git a/README.md b/README.md index 95ebee5e..ba512061 100644 --- a/README.md +++ b/README.md @@ -87,95 +87,6 @@ All 'v3' methods has `$version` parameter which can be used if you didn't set ve All the 'Get' Methods has `$filter = array()` if applicable to set query parameters -Products(V2 and V3) ------------- -you can get products and its variants, options variant metafields. -~~~php -Bigcommerce::configure(array( - 'client_id' => 'xxxxxxxxxxxxxxxxxxxxxx', - 'auth_token' => 'xxxxxxxxxxxxxxxxxxxxxxx', - 'store_hash' => 'xxxxxxxxx', - 'version' => 'v3' //optional By Default set as 'v2' -)); - -$products = Bigcommerce::getProducts(); //getProducts($filter = array(), $version = null); - -foreach($products as $item){ - echo $item->name."\n"; - $videos = $item->videos(); -} - -//Single Product - -$product = Bigcommerce::getProduct(1); - -//To get that product variants - -$variants = $product->variants(); //or Bigcommerce::getProduct(1)->variants(123); - -//To get Meta fields for a Single Variant - -$variant_metafield = Bigcommerce::getProducts(1)->variants(123)->meta_fields(); - -//To delete a Meta field - -$variant_metafield->delete(); //or Bigcommerce::getProduct(1)->variants(123)->meta_fields(1)->delete(); -~~~ -The Product Instance has following member functions - -~~~php -$product = Bigcommerce::getProduct(1); - -// Member Function works only on 'v3' -//All the 'v3' Resource Class has create(), update() and delete() functions - -$product->bulk_pricing_rules(1)->delete(); // Delete bulk pricing id 1 for product id 1 -$bulk = $product->bulk_pricing_rules(); // Retrieve all Bulk Pricing rules for product id 1 -$complex_rules = $product->complex_rules(); // or Bigcommerce::getProductComplexRules($product_id); - -$variants = $product->variants(); // or Bigcommerce::getProductVariants($product_id); -$variant = $product->variant(1); - -//'ProductVariant' Object has meta_fields(), create_image(), create(), update(), delete() functions - -$variant->create_image("https://image.jpg"); // ProductVariantObject->create_image($image_url); - -$variant_metafield = $variant->meta_fields(); // 'ProductVariantMetafield' object has create(), update(), delete() functions -$variant_metafield->delete(); - -// ProductObject->options() works on both 'v2' and 'v3' but ProductObject->options(1)->values() works only on 'v3' -// So, By default ProductObject->options($version = "v3") set to version 'v3' - -$options = $product->options("v2"); -$option_values = $product->options("v3")->values(); -$option_value = $product->options("v3")->values(1); -$option_value->delete(); - -// Member Functions works on both 'v2' and 'v3' - -$brand = $product->brand(); -$image = $product->images(1)->delete(); // or Bigcommerce::getProductImage(1); -$videos = $product->videos(); // or Bigcommerce::getProductVideos(); - -$video = $product->videos(1); // or Bigcommerce::getProductVideo(1); -$video->name = "Updated Video"; // or Bigcommerce::updateProductVideo($product_id, $video_id, $array); -$video->update(); // or $video->delete(); // or Bigcommerce::deleteProductVideo($product_id, $video_id); - -$custom_fields = $product->custom_fields(); // or Bigcommerce::getProductCustomFields($product_id); -$reviews = $product->reviews(); // or Bigcommerce::getProductReviews($product_id); - -// Member Functions works only on 'v2' -// May return empty data since 'v2' has been abandoned by Bigcommerce - -$skus = $product->skus(); -$rules = $product->rules(); -$configurable_rules = $product->configurable_rules(); -$discount_rules = $product->discount_rules(); -$option_set = $product->option_set(); -$tax_class = $product->tax_class(); -~~~ -**Product Modifiers and Meta Fields are still in Development** - Carts(V3) ------------ you can do almost all the functions in cart. @@ -316,8 +227,145 @@ Bigcommerce::updateCartLineItem("xxxxxxxxx","xxxxxxxxx",$item); ~~~php Bigcommerce::deleteCartLineItem("xxxxxxxxx","xxxxxxxxx"); ~~~ + + Products(V2 and V3) +------------ +you can get products and its variants, options variant metafields. +~~~php +Bigcommerce::configure(array( + 'client_id' => 'xxxxxxxxxxxxxxxxxxxxxx', + 'auth_token' => 'xxxxxxxxxxxxxxxxxxxxxxx', + 'store_hash' => 'xxxxxxxxx', + 'version' => 'v3' //optional By Default set as 'v2' +)); + +$products = Bigcommerce::getProducts(); //getProducts($filter = array(), $version = null); + +foreach($products as $item){ + echo $item->name."\n"; + $videos = $item->videos(); +} + +// Single Product +// 'Product' Object has create(), update(), delete() functions and more explained in below examples + +$product = Bigcommerce::getProduct(1); +$product->name = "Test 1"; +$product->update(); + +$product->delete(); + + +$variants = $product->variants(); +//or +Bigcommerce::getProduct(1)->variants(123); + + +//To get Meta fields for a Single Variant +$variant_metafield = Bigcommerce::getProducts(1)->variants(123)->meta_fields(); + + +$variant_metafield->delete(); +//or +Bigcommerce::getProduct(1)->variants(123)->meta_fields(1)->delete(); +~~~ +The Product Instance has following member functions + +~~~php +$product = Bigcommerce::getProduct(1); + +/* Member Function works only on 'v3' */ +// All the 'v3' Resource Class has create(), update() and delete() functions + + +// Delete bulk pricing id 1 for product id 1 +$product->bulk_pricing_rules(1)->delete(); + + +// 'ProductBulkPricingRule' Object has create(), update(), delete() functions + +// Retrieve all Bulk Pricing rules for product id 1 +$bulk = $product->bulk_pricing_rules(); + + +// 'ProductComplexRule' Object has create(), update(), delete() functions + +$complex_rules = $product->complex_rules(); +// or Bigcommerce::getProductComplexRules($product_id); + + +$variants = $product->variants(); +// or Bigcommerce::getProductVariants($product_id); + + +$variant = $product->variant(1); + +//'ProductVariant' Object has meta_fields(), create_image(), create(), update(), delete() functions + +$variant->create_image("https://image.jpg"); +// ProductVariantObject->create_image($image_url); + + +// 'ProductVariantMetafield' object has create(), update(), delete() functions + +$variant_metafield = $variant->meta_fields(); +$variant_metafield->delete(); + + +// ProductObject->options() works on both 'v2' and 'v3' but ProductObject->options(1)->values() works only on 'v3' +// So, By default ProductObject->options($version = "v3") set to version 'v3' + +$options = $product->options("v2"); +$option_values = $product->options("v3")->values(); +$option_value = $product->options("v3")->values(1); +$option_value->delete(); + + +/* Member Functions works on both 'v2' and 'v3' */ + +$brand = $product->brand(); + +$image = $product->images(1)->delete(); +// or Bigcommerce::getProductImage(1); + + +$videos = $product->videos(); +// or Bigcommerce::getProductVideos(); + +$video = $product->videos(1); +// or Bigcommerce::getProductVideo(1); + +$video->name = "Updated Video"; +// or Bigcommerce::updateProductVideo($product_id, $video_id, $array); + +$video->update(); // or $video->delete(); +// or Bigcommerce::deleteProductVideo($product_id, $video_id); + + +$custom_fields = $product->custom_fields(); +// or Bigcommerce::getProductCustomFields($product_id); + + +$reviews = $product->reviews(); +// or Bigcommerce::getProductReviews($product_id); + + +/* Member Functions works only on 'v2' */ +// May return empty data since 'v2' has been abandoned by Bigcommerce + + +$skus = $product->skus(); +$rules = $product->rules(); +$configurable_rules = $product->configurable_rules(); +$discount_rules = $product->discount_rules(); +$option_set = $product->option_set(); +$tax_class = $product->tax_class(); +~~~ +**Product Modifiers and Product Meta Fields are still in Development** + -##Brands (V2 and V3) +Brands (V2 and V3) +---------------------- you can use both 'v2' and 'v3' in Brands and I'm trying to do the same for all new versions. **Get All Brands**: `getBrands($filter = array(), $version = null);` From 13741fc344520141dae8403271e613dc64f899a2 Mon Sep 17 00:00:00 2001 From: Naveen Raj <49157455+naveenrajbu@users.noreply.github.com> Date: Mon, 13 Sep 2021 08:38:58 +0530 Subject: [PATCH 10/12] Update Product README --- README.md | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index ba512061..fa369d09 100644 --- a/README.md +++ b/README.md @@ -230,7 +230,8 @@ Bigcommerce::deleteCartLineItem("xxxxxxxxx","xxxxxxxxx"); Products(V2 and V3) ------------ -you can get products and its variants, options variant metafields. +You can do all the functions of Products and More... + ~~~php Bigcommerce::configure(array( 'client_id' => 'xxxxxxxxxxxxxxxxxxxxxx', @@ -269,27 +270,22 @@ $variant_metafield->delete(); //or Bigcommerce::getProduct(1)->variants(123)->meta_fields(1)->delete(); ~~~ -The Product Instance has following member functions +**The 'Product' Object has following member functions** +**Member Function works only on 'v3'** +All the 'v3' Resource Class has `create(), update() and delete()` functions ~~~php $product = Bigcommerce::getProduct(1); -/* Member Function works only on 'v3' */ -// All the 'v3' Resource Class has create(), update() and delete() functions - // Delete bulk pricing id 1 for product id 1 $product->bulk_pricing_rules(1)->delete(); -// 'ProductBulkPricingRule' Object has create(), update(), delete() functions - // Retrieve all Bulk Pricing rules for product id 1 $bulk = $product->bulk_pricing_rules(); -// 'ProductComplexRule' Object has create(), update(), delete() functions - $complex_rules = $product->complex_rules(); // or Bigcommerce::getProductComplexRules($product_id); @@ -319,9 +315,14 @@ $options = $product->options("v2"); $option_values = $product->options("v3")->values(); $option_value = $product->options("v3")->values(1); $option_value->delete(); +~~~ +**Member Functions works on both 'v2' and 'v3'** +Below are the function that works on both `v2` and `v3` versions +you can override the default version by setting it in functions like: `Bigcommerce::getProduct(1)->brand("v3");` -/* Member Functions works on both 'v2' and 'v3' */ +~~~php +$product = Bigcommerce::getProduct(1); $brand = $product->brand(); @@ -348,11 +349,12 @@ $custom_fields = $product->custom_fields(); $reviews = $product->reviews(); // or Bigcommerce::getProductReviews($product_id); +~~~ - -/* Member Functions works only on 'v2' */ -// May return empty data since 'v2' has been abandoned by Bigcommerce - +**Member Functions works only on 'v2'** +Some functions may return empty data since 'v2' has been abandoned by Bigcommerce +~~~php +$product = Bigcommerce::getProduct(1); $skus = $product->skus(); $rules = $product->rules(); From 64dd208626d3681c1151493d4e43e99f6158e742 Mon Sep 17 00:00:00 2001 From: Naveen Raj <49157455+naveenrajbu@users.noreply.github.com> Date: Mon, 13 Sep 2021 11:34:35 +0530 Subject: [PATCH 11/12] Update README.md --- README.md | 275 +++++++++++++++++++++++++++--------------------------- 1 file changed, 137 insertions(+), 138 deletions(-) diff --git a/README.md b/README.md index fa369d09..1f8a9752 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,143 @@ All 'v3' methods has `$version` parameter which can be used if you didn't set ve All the 'Get' Methods has `$filter = array()` if applicable to set query parameters +Products(V2 and V3) +------------ +You can do all the functions of Products and More... + +~~~php +Bigcommerce::configure(array( + 'client_id' => 'xxxxxxxxxxxxxxxxxxxxxx', + 'auth_token' => 'xxxxxxxxxxxxxxxxxxxxxxx', + 'store_hash' => 'xxxxxxxxx', + 'version' => 'v3' //optional By Default set as 'v2' +)); + +$products = Bigcommerce::getProducts(); //getProducts($filter = array(), $version = null); + +foreach($products as $item){ + echo $item->name."\n"; + $videos = $item->videos(); +} + +// Single Product +// 'Product' Object has create(), update(), delete() functions and more explained in below examples + +$product = Bigcommerce::getProduct(1); +$product->name = "Test 1"; +$product->update(); + +$product->delete(); + + +$variants = $product->variants(); +//or +Bigcommerce::getProduct(1)->variants(123); + + +//To get Meta fields for a Single Variant +$variant_metafield = Bigcommerce::getProducts(1)->variants(123)->meta_fields(); + + +$variant_metafield->delete(); +//or +Bigcommerce::getProduct(1)->variants(123)->meta_fields(1)->delete(); +~~~ +**The 'Product' Object has following member functions** + +**Member Function works only on 'v3'** +All the 'v3' Resource Class has `create(), update() and delete()` functions +~~~php +$product = Bigcommerce::getProduct(1); + + +// Delete bulk pricing id 1 for product id 1 +$product->bulk_pricing_rules(1)->delete(); + + +// Retrieve all Bulk Pricing rules for product id 1 +$bulk = $product->bulk_pricing_rules(); + + +$complex_rules = $product->complex_rules(); +// or Bigcommerce::getProductComplexRules($product_id); + + +$variants = $product->variants(); +// or Bigcommerce::getProductVariants($product_id); + + +$variant = $product->variant(1); + +//'ProductVariant' Object has meta_fields(), create_image(), create(), update(), delete() functions + +$variant->create_image("https://image.jpg"); +// ProductVariantObject->create_image($image_url); + + +// 'ProductVariantMetafield' object has create(), update(), delete() functions + +$variant_metafield = $variant->meta_fields(); +$variant_metafield->delete(); + + +// ProductObject->options() works on both 'v2' and 'v3' but ProductObject->options(1)->values() works only on 'v3' +// So, By default ProductObject->options($version = "v3") set to version 'v3' + +$options = $product->options("v2"); +$option_values = $product->options("v3")->values(); +$option_value = $product->options("v3")->values(1); +$option_value->delete(); +~~~ + +**Member Functions works on both 'v2' and 'v3'** +Below are the function that works on both `v2` and `v3` versions +you can override the default version by setting it in functions like: `Bigcommerce::getProduct(1)->brand("v3");` + +~~~php +$product = Bigcommerce::getProduct(1); + +$brand = $product->brand(); + +$image = $product->images(1)->delete(); +// or Bigcommerce::getProductImage(1); + + +$videos = $product->videos(); +// or Bigcommerce::getProductVideos(); + +$video = $product->videos(1); +// or Bigcommerce::getProductVideo(1); + +$video->name = "Updated Video"; +// or Bigcommerce::updateProductVideo($product_id, $video_id, $array); + +$video->update(); // or $video->delete(); +// or Bigcommerce::deleteProductVideo($product_id, $video_id); + + +$custom_fields = $product->custom_fields(); +// or Bigcommerce::getProductCustomFields($product_id); + + +$reviews = $product->reviews(); +// or Bigcommerce::getProductReviews($product_id); +~~~ + +**Member Functions works only on 'v2'** +Some functions may return empty data since 'v2' has been abandoned by Bigcommerce +~~~php +$product = Bigcommerce::getProduct(1); + +$skus = $product->skus(); +$rules = $product->rules(); +$configurable_rules = $product->configurable_rules(); +$discount_rules = $product->discount_rules(); +$option_set = $product->option_set(); +$tax_class = $product->tax_class(); +~~~ +**Product Modifiers and Product Meta Fields are still in Development** + Carts(V3) ------------ you can do almost all the functions in cart. @@ -228,144 +365,6 @@ Bigcommerce::updateCartLineItem("xxxxxxxxx","xxxxxxxxx",$item); Bigcommerce::deleteCartLineItem("xxxxxxxxx","xxxxxxxxx"); ~~~ - Products(V2 and V3) ------------- -You can do all the functions of Products and More... - -~~~php -Bigcommerce::configure(array( - 'client_id' => 'xxxxxxxxxxxxxxxxxxxxxx', - 'auth_token' => 'xxxxxxxxxxxxxxxxxxxxxxx', - 'store_hash' => 'xxxxxxxxx', - 'version' => 'v3' //optional By Default set as 'v2' -)); - -$products = Bigcommerce::getProducts(); //getProducts($filter = array(), $version = null); - -foreach($products as $item){ - echo $item->name."\n"; - $videos = $item->videos(); -} - -// Single Product -// 'Product' Object has create(), update(), delete() functions and more explained in below examples - -$product = Bigcommerce::getProduct(1); -$product->name = "Test 1"; -$product->update(); - -$product->delete(); - - -$variants = $product->variants(); -//or -Bigcommerce::getProduct(1)->variants(123); - - -//To get Meta fields for a Single Variant -$variant_metafield = Bigcommerce::getProducts(1)->variants(123)->meta_fields(); - - -$variant_metafield->delete(); -//or -Bigcommerce::getProduct(1)->variants(123)->meta_fields(1)->delete(); -~~~ -**The 'Product' Object has following member functions** - -**Member Function works only on 'v3'** -All the 'v3' Resource Class has `create(), update() and delete()` functions -~~~php -$product = Bigcommerce::getProduct(1); - - -// Delete bulk pricing id 1 for product id 1 -$product->bulk_pricing_rules(1)->delete(); - - -// Retrieve all Bulk Pricing rules for product id 1 -$bulk = $product->bulk_pricing_rules(); - - -$complex_rules = $product->complex_rules(); -// or Bigcommerce::getProductComplexRules($product_id); - - -$variants = $product->variants(); -// or Bigcommerce::getProductVariants($product_id); - - -$variant = $product->variant(1); - -//'ProductVariant' Object has meta_fields(), create_image(), create(), update(), delete() functions - -$variant->create_image("https://image.jpg"); -// ProductVariantObject->create_image($image_url); - - -// 'ProductVariantMetafield' object has create(), update(), delete() functions - -$variant_metafield = $variant->meta_fields(); -$variant_metafield->delete(); - - -// ProductObject->options() works on both 'v2' and 'v3' but ProductObject->options(1)->values() works only on 'v3' -// So, By default ProductObject->options($version = "v3") set to version 'v3' - -$options = $product->options("v2"); -$option_values = $product->options("v3")->values(); -$option_value = $product->options("v3")->values(1); -$option_value->delete(); -~~~ - -**Member Functions works on both 'v2' and 'v3'** -Below are the function that works on both `v2` and `v3` versions -you can override the default version by setting it in functions like: `Bigcommerce::getProduct(1)->brand("v3");` - -~~~php -$product = Bigcommerce::getProduct(1); - -$brand = $product->brand(); - -$image = $product->images(1)->delete(); -// or Bigcommerce::getProductImage(1); - - -$videos = $product->videos(); -// or Bigcommerce::getProductVideos(); - -$video = $product->videos(1); -// or Bigcommerce::getProductVideo(1); - -$video->name = "Updated Video"; -// or Bigcommerce::updateProductVideo($product_id, $video_id, $array); - -$video->update(); // or $video->delete(); -// or Bigcommerce::deleteProductVideo($product_id, $video_id); - - -$custom_fields = $product->custom_fields(); -// or Bigcommerce::getProductCustomFields($product_id); - - -$reviews = $product->reviews(); -// or Bigcommerce::getProductReviews($product_id); -~~~ - -**Member Functions works only on 'v2'** -Some functions may return empty data since 'v2' has been abandoned by Bigcommerce -~~~php -$product = Bigcommerce::getProduct(1); - -$skus = $product->skus(); -$rules = $product->rules(); -$configurable_rules = $product->configurable_rules(); -$discount_rules = $product->discount_rules(); -$option_set = $product->option_set(); -$tax_class = $product->tax_class(); -~~~ -**Product Modifiers and Product Meta Fields are still in Development** - - Brands (V2 and V3) ---------------------- you can use both 'v2' and 'v3' in Brands and I'm trying to do the same for all new versions. From f667a7f7484ca7bddad8b6530ccbe5b43f2c36e0 Mon Sep 17 00:00:00 2001 From: Naveen Raj <49157455+naveenrajbu@users.noreply.github.com> Date: Mon, 13 Sep 2021 11:47:02 +0530 Subject: [PATCH 12/12] Update README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1f8a9752..3f951e4e 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ Bigcommerce::getProduct(1)->variants(123)->meta_fields(1)->delete(); ~~~ **The 'Product' Object has following member functions** -**Member Function works only on 'v3'** +**Member Functions that works only on 'v3'** All the 'v3' Resource Class has `create(), update() and delete()` functions ~~~php $product = Bigcommerce::getProduct(1); @@ -176,7 +176,7 @@ $option_value = $product->options("v3")->values(1); $option_value->delete(); ~~~ -**Member Functions works on both 'v2' and 'v3'** +**Member Functions that works on both 'v2' and 'v3'** Below are the function that works on both `v2` and `v3` versions you can override the default version by setting it in functions like: `Bigcommerce::getProduct(1)->brand("v3");` @@ -210,7 +210,7 @@ $reviews = $product->reviews(); // or Bigcommerce::getProductReviews($product_id); ~~~ -**Member Functions works only on 'v2'** +**Member Functions that works only on 'v2'** Some functions may return empty data since 'v2' has been abandoned by Bigcommerce ~~~php $product = Bigcommerce::getProduct(1);