Skip to content

Commit ea9644e

Browse files
authored
Merge pull request #32 from aligent/issue-31_update_create_product_variant
Add methods and examples to create/update product variant
2 parents 6da00b1 + 93c482c commit ea9644e

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,38 @@ echo "Found {$productsResponse->getPagination()->total} products";
4343
$products = $productsResponse->getProducts();
4444
```
4545

46+
Example of updating a product variant
47+
48+
```php
49+
$api = new BigCommerce\ApiV3\Client($_ENV['hash'], $_ENV['CLIENT_ID'], $_ENV['ACCESS_TOKEN']);
50+
51+
$productVariant = $api->catalog()->product(123)->variant(456)->get()->getProductVariant();
52+
$productVariant->price = '12';
53+
54+
try {
55+
$api->catalog()->product($productVariant->product_id)->variant($productVariant->id)->update($productVariant);
56+
} catch (\Psr\Http\Client\ClientExceptionInterface $exception) {
57+
echo "Unable to update product variant: {$exception->getMessage()}";
58+
}
59+
```
60+
61+
Example of creating a product variant
62+
63+
```php
64+
$api = new BigCommerce\ApiV3\Client($_ENV['hash'], $_ENV['CLIENT_ID'], $_ENV['ACCESS_TOKEN']);
65+
66+
$productVariant = new \BigCommerce\ApiV3\ResourceModels\Catalog\Product\ProductVariant();
67+
$productVariant->product_id = 123;
68+
$productVariant->sku = "SKU-123";
69+
//...
70+
71+
try {
72+
$api->catalog()->product($productVariant->product_id)->variants()->create($productVariant);
73+
} catch (\Psr\Http\Client\ClientExceptionInterface $exception) {
74+
echo "Unable to create product variant: {$exception->getMessage()}";
75+
}
76+
```
77+
4678
## Development
4779

4880
Running tests: `composer run-script test`

src/BigCommerce/Api/Catalog/Products/VariantsApi.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace BigCommerce\ApiV3\Api\Catalog\Products;
44

5-
use BigCommerce\ApiV3\Api\Generic\ResourceApi;
65
use BigCommerce\ApiV3\Api\Catalog\Products\ProductVariant\ProductVariantMetafieldsApi;
6+
use BigCommerce\ApiV3\Api\Generic\ResourceApi;
7+
use BigCommerce\ApiV3\ResourceModels\Catalog\Product\ProductVariant;
78
use BigCommerce\ApiV3\ResponseModels\Product\ProductVariantResponse;
89
use BigCommerce\ApiV3\ResponseModels\Product\ProductVariantsResponse;
910

@@ -38,6 +39,16 @@ public function getAll(array $filters = [], int $page = 1, int $limit = 250): Pr
3839
return new ProductVariantsResponse($this->getAllResources($filters, $page, $limit));
3940
}
4041

42+
public function create(ProductVariant $productVariant): ProductVariantResponse
43+
{
44+
return new ProductVariantResponse($this->createResource($productVariant));
45+
}
46+
47+
public function update(ProductVariant $productVariant): ProductVariantResponse
48+
{
49+
return new ProductVariantResponse($this->updateResource($productVariant));
50+
}
51+
4152
public function metafield(int $metafieldId = null): ProductVariantMetafieldsApi
4253
{
4354
$api = new ProductVariantMetafieldsApi($this->getClient(), $metafieldId, $this->getResourceId());

0 commit comments

Comments
 (0)