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..3f951e4e 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 ~~~ @@ -56,6 +56,492 @@ 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, 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 + +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 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 Functions that 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 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");` + +~~~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 that 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. + +**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'); +~~~ + +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 + +**You can use all the features and Methods Below** + Configuration ------------- @@ -90,6 +576,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 00eb60fa..a7d95790 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(null, "v2", "v3"); /** * Configure the API client with the required settings to access @@ -72,14 +81,17 @@ class Client * * Accepts OAuth and (for now!) Basic Auth credentials * + * configure using 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("'client_id' must be provided"); } } @@ -93,7 +105,7 @@ public static function configure($settings) * - store_hash * * @param array $settings - * @throws \Exception + * @throws Exception */ public static function configureOAuth($settings) { @@ -111,6 +123,16 @@ 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); self::$connection = false; } @@ -125,7 +147,8 @@ public static function configureOAuth($settings) * - api_key * * @param array $settings - * @throws \Exception + * @throws Exception + * @deprecated */ public static function configureBasicAuth(array $settings) { @@ -219,11 +242,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; @@ -254,13 +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) { - $response = self::connection()->get(self::$api_path . $path); - - return self::mapCollection($resource, $response); + $temp_version = (!is_null($version) and in_array($version, self::$available_versions))?$version:self::$version; + $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 { + return self::mapCollection($resource, $response); + } } /** @@ -268,30 +299,74 @@ 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) { - $response = self::connection()->get(self::$api_path . $path); + $temp_version = (!is_null($version) and in_array($version, self::$available_versions))?$version:self::$version; + $path = ($resource !== "Resource")?self::getUrl($resource, $temp_version).$path : $path; - return self::mapResource($resource, $response); + $response = self::connection()->get(self::$api_path .$temp_version. $path); + + if (isset($response->data)) { + return self::mapResource($resource, $response->data); + } else { + return self::mapResource($resource, $response); + } + } + + /** + * 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, $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($version, $object->urls)) { + return $object->urls[$version]; + } else { + throw new Exception($version." not available for this resource"); + } + } else { + return ""; + } } /** * Get a count value from the specified endpoint. * * @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) + public static function getCount($path, $resource = "Resource", $version = null) { - $response = self::connection()->get(self::$api_path . $path); + $temp_version = (!is_null($version) and in_array($version, self::$available_versions))?$version:self::$version; + $path = ($resource !== "Resource")?self::getUrl($resource, $temp_version).$path : $path; - if ($response == false || is_string($response)) { - return $response; - } + $response = self::connection()->get(self::$api_path .$temp_version. $path); - return $response->count; + if (self::$version == "v2") { + if ($response == false || is_string($response)) { + return $response; + } + + return $response->count; + } else { + return $response->meta->pagination->total; + } } /** @@ -299,15 +374,19 @@ public static function getCount($path) * * @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) + 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; } - - return self::connection()->post(self::$api_path . $path, $object); + $path = ($resource !== "Resource")?self::getUrl($resource, $temp_version).$path : $path; + return self::connection()->post(self::$api_path .$temp_version. $path, $object); } /** @@ -315,26 +394,35 @@ public static function createResource($path, $object) * * @param string $path api endpoint * @param mixed $object object or XML string to update + * @param string $resource + * @param null $version * @return mixed + * @throws Exception */ - public static function updateResource($path, $object) + 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; } - - return self::connection()->put(self::$api_path . $path, $object); + $path = ($resource !== "Resource")?self::getUrl($resource, $temp_version).$path : $path; + return self::connection()->put(self::$api_path .$temp_version. $path, $object); } /** * Send a delete request to remove the specified resource. * * @param string $path api endpoint + * @param string $resource + * @param null $version * @return mixed + * @throws Exception */ - public static function deleteResource($path) + public static function deleteResource($path, $resource = "Resource", $version = null) { - return self::connection()->delete(self::$api_path . $path); + $temp_version = (!is_null($version) and in_array($version, self::$available_versions))?$version:self::$version; + $path = ($resource !== "Resource")?self::getUrl($resource, $temp_version).$path : $path; + return self::connection()->delete(self::$api_path .$temp_version. $path); } /** @@ -365,7 +453,6 @@ private static function mapCollection($resource, $object) private static function mapCollectionObject($object) { $class = self::$resource; - return new $class($object); } @@ -468,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); + } } /** @@ -514,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); } /** @@ -538,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); + } } /** @@ -550,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); + } } /** @@ -573,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); } /** @@ -596,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); } /** @@ -719,6 +889,7 @@ public static function getOptionValues($filter = array()) * * @param array $filter * @return array + * @throws Exception */ public static function getCategories($filter = array()) { @@ -743,6 +914,7 @@ public static function getCategoriesCount($filter = array()) * * @param int $id category id * @return Resources\Category + * @throws Exception */ public static function getCategory($id) { @@ -797,270 +969,664 @@ public static function deleteAllCategories() * The collection of brands. * * @param array $filter + * @param string $version + * @return array + * @throws Exception + */ + public static function getBrands($filter = array(), $version = null) + { + $filter = Filter::create($filter); + if (in_array($version, self::$available_versions)) { + return self::getCollection($filter->toQuery(), 'Brand', $version); + } 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 getBrands($filter = array()) + public static function getBrandMetafields($id, $filter = array(), $version = null) { $filter = Filter::create($filter); - return self::getCollection('/brands' . $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"); + } } /** * 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) { $filter = Filter::create($filter); - return self::getCount('/brands/count' . $filter->toQuery()); + 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"); + } } /** * 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) + { + if (in_array($version, self::$available_versions)) { + return self::getResource("/".$id, 'Brand', $version); + } 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 getBrand($id) + public static function getBrandMetafield($brand_id, $metafield_id, $filter = array(), $version = null) { - return self::getResource('/brands/' . $id, 'Brand'); + $filter = Filter::create($filter); + 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"); + } } /** * Create a new brand from the given data. * * @param mixed $object + * @param null $version * @return mixed + * @throws Exception */ - public static function createBrand($object) + public static function createBrand($object, $version = null) { - return self::createResource('/brands', $object); + if (in_array($version, self::$available_versions)) { + return self::createResource('', $object, "Brand", $version); + } else { + throw new Exception("'version' not available"); + } } /** - * Update the given brand. + * Create a new brand from the given data. * - * @param int $id brand id + * @param $id * @param mixed $object + * @param null $version * @return mixed + * @throws Exception */ - public static function updateBrand($id, $object) + public static function createBrandMetafield($id, $object, $version = null) { - return self::updateResource('/brands/' . $id, $object); + if (in_array($version, self::$available_versions)) { + return self::createResource('/'.$id.'/metafields', $object, "Brand", $version); + } else { + throw new Exception("'version' not available"); + } } /** - * Delete the given brand. + * Update the given brand. * * @param int $id brand id + * @param mixed $object + * @param null|string $version * @return mixed + * @throws Exception */ - public static function deleteBrand($id) + public static function updateBrand($id, $object, $version = null) { - return self::deleteResource('/brands/' . $id); + if (in_array($version, self::$available_versions)) { + return self::updateResource('/' . $id, $object, "Brand", $version); + } else { + throw new Exception("'version' not available"); + } } /** - * Delete all brands. + * Update single brand Metafield by given brand id and Metafield id. * - * @return mixed + * @param int $brand_id + * @param int $metafield_id + * @param array|object $object + * @param string $version + * @return Resources\Brand + * @throws Exception */ - public static function deleteAllBrands() + public static function updateBrandMetafield($brand_id, $metafield_id, $object, $version = null) { - return self::deleteResource('/brands'); + 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"); + } } /** - * The collection of orders. + * Delete the given brand. * - * @param array $filter - * @return array + * @param int $id brand id + * @param null $version + * @return mixed + * @throws Exception */ - public static function getOrders($filter = array()) + public static function deleteBrand($id, $version = null) { - $filter = Filter::create($filter); - return self::getCollection('/orders' . $filter->toQuery(), 'Order'); + if (in_array($version, self::$available_versions)) { + return self::deleteResource('/' . $id, "Brand", $version); + } else { + throw new Exception("'version' not available"); + } } /** - * The number of orders in the collection. + * Delete single brand Metafield by given brand id and Metafield id. * - * @param array $filter - * @return int + * @param int $brand_id + * @param int $metafield_id + * @param string $version + * @return Resources\Brand + * @throws Exception */ - public static function getOrdersCount($filter = array()) + public static function deleteBrandMetafield($brand_id, $metafield_id, $version = null) { - $filter = Filter::create($filter); - return self::getCount('/orders/count' . $filter->toQuery()); + if (in_array($version, self::$available_versions)) { + return self::deleteResource("/".$brand_id.'/metafields/'.$metafield_id, 'Brand', $version); + } else { + throw new Exception("'version' not available"); + } } /** - * The order count grouped by order status + * Delete all brands. * - * @param array $filter - * @return Resources\OrderStatus + * @param null $version + * @return mixed + * @throws Exception */ - public static function getOrderStatusesWithCounts($filter = array()) + public static function deleteAllBrands($version = null) { - $filter = Filter::create($filter); - $resource = self::getResource('/orders/count' . $filter->toQuery(), "OrderStatus"); - return $resource->statuses; + if (in_array(self::$version, self::$available_versions)) { + return self::deleteResource('', 'Brand', $version); + } else { + throw new Exception("'version' not available"); + } } /** - * A single order. + * Get a cart from cart id. * - * @param int $id order id - * @return Resources\Order + * @param $id + * @param null $version + * @return mixed + * @throws Exception */ - public static function getOrder($id) + public static function getCart($id, $version = null) { - return self::getResource('/orders/' . $id, 'Order'); + if (in_array($version, self::$available_versions)) { + return self::getResource("/".$id, 'Cart', $version); + } else { + throw new Exception("'version' not available"); + } } /** - * @param $orderID + * Create a new cart from the given data. + * + * @param mixed $object + * @param null $version * @return mixed + * @throws Exception */ - public static function getOrderProducts($orderID) + public static function createCart($object, $version = null) { - return self::getCollection('/orders/' . $orderID . '/products', 'OrderProduct'); + if (in_array($version, self::$available_versions)) { + return self::createResource('', $object, "Cart", $version); + } else { + throw new Exception("'version' not available"); + } } /** - * The total number of order products in the collection. + * Create a Line items for existing cart from the given data. * - * @param $orderID + * @param $id + * @param mixed $object * @param array $filter + * @param null $version * @return mixed + * @throws Exception */ - public static function getOrderProductsCount($orderID, $filter = array()) + public static function createCartLineItems($id, $object, $filter = array(), $version = null) { $filter = Filter::create($filter); - return self::getCount('/orders/' . $orderID . '/products/count' . $filter->toQuery()); + if (in_array($version, self::$available_versions)) { + return self::createResource('/'.$id.'/items'.$filter->toQuery(), $object, "Cart", $version); + } else { + throw new Exception("'version' not available"); + } } /** - * Delete the given order (unlike in the Control Panel, this will permanently - * delete the order). + * update cart customer id. * - * @param int $id order id + * @param $cart_id + * @param $customer_id + * @param null $version * @return mixed + * @throws Exception */ - public static function deleteOrder($id) + public static function updateCartCustomerId($cart_id, $customer_id, $version = null) { - return self::deleteResource('/orders/' . $id); + 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"); + } } /** - * Delete all orders. + * 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 deleteAllOrders() + public static function updateCartLineItem($cart_id, $line_item_id, $object, $filter = array(), $version = null) { - return self::deleteResource('/orders'); + $filter = Filter::create($filter); + 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"); + } } /** - * Create an order + * Delete a Cart by Cart Id * - * @param $object + * @param $id + * @param null $version * @return mixed + * @throws Exception */ - public static function createOrder($object) + public static function deleteCart($id, $version = null) { - return self::createResource('/orders', $object); + if (in_array($version, self::$available_versions)) { + return self::deleteResource('/'.$id, "Cart", $version); + } else { + throw new Exception("'version' not available"); + } } /** - * Update the given order. + * Delete a Cart Line Item by Cart Id and Line Item Id * - * @param int $id order id - * @param mixed $object fields to update + * @param $cart_id + * @param $line_item_id + * @param null $version * @return mixed + * @throws Exception */ - public static function updateOrder($id, $object) + public static function deleteCartLineItem($cart_id, $line_item_id, $version = null) { - return self::updateResource('/orders/' . $id, $object); + 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"); + } } /** - * The list of customers. + * Get All Wishlists. * * @param array $filter - * @return array + * @param null $version + * @return mixed + * @throws Exception */ - public static function getCustomers($filter = array()) + public static function getWishlists($filter = array(), $version = null) { $filter = Filter::create($filter); - return self::getCollection('/customers' . $filter->toQuery(), 'Customer'); + if (in_array($version, self::$available_versions)) { + return self::getCollection($filter->toQuery(), 'Wishlist', $version); + } else { + throw new Exception("'version' not available"); + } } /** - * The total number of customers in the collection. + * Get Wishlist by Wishlist Id. * - * @param array $filter - * @return int + * @param $id + * @param null $version + * @return mixed + * @throws Exception */ - public static function getCustomersCount($filter = array()) + public static function getWishlist($id, $version = null) { - $filter = Filter::create($filter); - return self::getCount('/customers/count' . $filter->toQuery()); + if (in_array($version, self::$available_versions)) { + return self::getResource("/".$id, 'Wishlist', $version); + } else { + throw new Exception("'version' not available"); + } } /** - * Bulk delete customers. + * Get Wishlist by Wishlist Id. * - * @param array $filter - * @return array + * @param array|object $object + * @param null $version + * @return mixed + * @throws Exception */ - public static function deleteCustomers($filter = array()) + public static function createWishlist($object, $version = null) { - $filter = Filter::create($filter); - return self::deleteResource('/customers' . $filter->toQuery()); + if (in_array($version, self::$available_versions)) { + return self::createResource("", $object, 'Wishlist', $version); + } else { + throw new Exception("'version' not available"); + } } /** - * A single customer by given id. + * Get Wishlist by Wishlist Id. * - * @param int $id customer id - * @return Resources\Customer + * @param $id + * @param array|object $object + * @param null $version + * @return mixed + * @throws Exception */ - public static function getCustomer($id) + public static function createWishlistItems($id, $object, $version = null) { - return self::getResource('/customers/' . $id, 'Customer'); + if (in_array($version, self::$available_versions)) { + return self::createResource("/".$id."/items", $object, 'Wishlist', $version); + } else { + throw new Exception("'version' not available"); + } } /** - * Create a new customer from the given data. + * Get Wishlist by Wishlist Id. * - * @param mixed $object + * @param $id + * @param array|object $object + * @param null $version * @return mixed + * @throws Exception */ - public static function createCustomer($object) + public static function updateWishlist($id, $object, $version = null) { - return self::createResource('/customers', $object); + if (in_array($version, self::$available_versions)) { + return self::updateResource("/".$id, $object, 'Wishlist', $version); + } else { + throw new Exception("'version' not available"); + } } /** - * Update the given customer. + * Get Wishlist by Wishlist Id. * - * @param int $id customer id - * @param mixed $object + * @param $id + * @param null $version * @return mixed + * @throws Exception */ - public static function updateCustomer($id, $object) + public static function deleteWishlist($id, $version = null) { - return self::updateResource('/customers/' . $id, $object); + if (in_array($version, self::$available_versions)) { + return self::deleteResource("/".$id, 'Wishlist', $version); + } else { + throw new Exception("'version' not available"); + } } /** - * Delete the given customer. + * Get Wishlist by Wishlist Id. * - * @param int $id customer 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. + * + * @param array $filter + * @return array + * @throws Exception + */ + public static function getOrders($filter = array()) + { + $filter = Filter::create($filter); + return self::getCollection('/orders' . $filter->toQuery(), 'Order'); + } + + /** + * The number of orders in the collection. + * + * @param array $filter + * @return int + */ + public static function getOrdersCount($filter = array()) + { + $filter = Filter::create($filter); + return self::getCount('/orders/count' . $filter->toQuery()); + } + + /** + * The order count grouped by order status + * + * @param array $filter + * @return Resources\OrderStatus + * @throws Exception + */ + public static function getOrderStatusesWithCounts($filter = array()) + { + $filter = Filter::create($filter); + $resource = self::getResource('/orders/count' . $filter->toQuery(), "OrderStatus"); + return $resource->statuses; + } + + /** + * A single order. + * + * @param int $id order id + * @return Resources\Order + * @throws Exception + */ + public static function getOrder($id) + { + return self::getResource('/orders/' . $id, 'Order'); + } + + /** + * @param $orderID + * @return mixed + * @throws Exception + */ + public static function getOrderProducts($orderID) + { + return self::getCollection('/orders/' . $orderID . '/products', 'OrderProduct'); + } + + /** + * The total number of order products in the collection. + * + * @param $orderID + * @param array $filter + * @return mixed + */ + public static function getOrderProductsCount($orderID, $filter = array()) + { + $filter = Filter::create($filter); + return self::getCount('/orders/' . $orderID . '/products/count' . $filter->toQuery()); + } + + /** + * Delete the given order (unlike in the Control Panel, this will permanently + * delete the order). + * + * @param int $id order id + * @return mixed + * @throws Exception + */ + public static function deleteOrder($id) + { + return self::deleteResource('/orders/' . $id); + } + + /** + * Delete all orders. + * + * @return mixed + */ + public static function deleteAllOrders() + { + return self::deleteResource('/orders'); + } + + /** + * Create an order + * + * @param $object + * @return mixed + */ + public static function createOrder($object) + { + return self::createResource('/orders', $object); + } + + /** + * Update the given order. + * + * @param int $id order id + * @param mixed $object fields to update + * @return mixed + */ + public static function updateOrder($id, $object) + { + return self::updateResource('/orders/' . $id, $object); + } + + /** + * The list of customers. + * + * @param array $filter + * @return array + * @throws Exception + */ + public static function getCustomers($filter = array()) + { + $filter = Filter::create($filter); + return self::getCollection('/customers' . $filter->toQuery(), 'Customer'); + } + + /** + * The total number of customers in the collection. + * + * @param array $filter + * @return int + */ + public static function getCustomersCount($filter = array()) + { + $filter = Filter::create($filter); + return self::getCount('/customers/count' . $filter->toQuery()); + } + + /** + * Bulk delete customers. + * + * @param array $filter + * @return array + */ + public static function deleteCustomers($filter = array()) + { + $filter = Filter::create($filter); + return self::deleteResource('/customers' . $filter->toQuery()); + } + + /** + * A single customer by given id. + * + * @param int $id customer id + * @return Resources\Customer + */ + public static function getCustomer($id) + { + return self::getResource('/customers/' . $id, 'Customer'); + } + + /** + * Create a new customer from the given data. + * + * @param mixed $object + * @return mixed + */ + public static function createCustomer($object) + { + return self::createResource('/customers', $object); + } + + /** + * Update the given customer. + * + * @param int $id customer id + * @param mixed $object + * @return mixed + */ + public static function updateCustomer($id, $object) + { + return self::updateResource('/customers/' . $id, $object); + } + + /** + * Delete the given customer. + * + * @param int $id customer id * @return mixed */ public static function deleteCustomer($id) @@ -1184,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); } /** @@ -1524,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); } /** @@ -1537,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); } /** @@ -1549,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); } /** @@ -1561,84 +2150,495 @@ 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 getProductBulkPricingRule($product_id, $id, $filter = array(), $version = "v3") + { + $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; + } + + /** + * Create a Bulk Pricing Rule resource by the given product id. + * + * @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) { @@ -1785,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); } /** @@ -1847,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 createProductOption($product_id, $id, $object, $version = null) + { + 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 getProductOption($productId, $productOptionId) + public static function updateProductOption($product_id, $id, $object, $version = null) { - return self::getResource('/products/' . $productId . '/options/' . $productOptionId); + 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); } /** @@ -1872,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, $version = "v2") + { + 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 getProductRule($productId, $productRuleId) + public static function getProductRules($productId, $version = "v2") { - return self::getResource('/products/' . $productId . '/rules/' . $productRuleId); + return self::getResource('/' . $productId . '/rules', 'Rule', $version); } /** @@ -2046,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/Connection.php b/src/Bigcommerce/Api/Connection.php index 1e5f7fd1..548e0685 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) { diff --git a/src/Bigcommerce/Api/Error.php b/src/Bigcommerce/Api/Error.php index aae1d5ab..ed521296 100644 --- a/src/Bigcommerce/Api/Error.php +++ b/src/Bigcommerce/Api/Error.php @@ -11,6 +11,12 @@ public function __construct($message, $code) { if (is_array($message)) { $message = $message[0]->message; + } else { + if (isset($message->title)) { + $message = $message->title; + } else { + $message = "Undefined Error Occurred"; + } } parent::__construct($message, $code); 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 a8b301fe..2f5495b8 100644 --- a/src/Bigcommerce/Api/Resources/Brand.php +++ b/src/Bigcommerce/Api/Resources/Brand.php @@ -15,13 +15,28 @@ 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::updateBrand($this->id, $this->getUpdateFields(), $version); + } + + public function delete($version = null) { - return Client::createBrand($this->getCreateFields()); + return Client::deleteBrand($this->id, $version); } - public function update() + public function toJson() { - return Client::updateBrand($this->id, $this->getUpdateFields()); + return parent::toJson(); } } diff --git a/src/Bigcommerce/Api/Resources/Cart.php b/src/Bigcommerce/Api/Resources/Cart.php new file mode 100644 index 00000000..3a730a31 --- /dev/null +++ b/src/Bigcommerce/Api/Resources/Cart.php @@ -0,0 +1,65 @@ + "/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); + } + + public function toJson() + { + return parent::toJson(); + } + +} \ No newline at end of file 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/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/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/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/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/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/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'); } } 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(); + } +} 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 @@