Skip to content

Commit

Permalink
Merge branch 'main' into feature/implement-cart-api
Browse files Browse the repository at this point in the history
Conflicts:
	RELEASE_NOTES.md
  • Loading branch information
jswift committed Apr 5, 2021
2 parents a1d0794 + 2c9a453 commit 4fbaea4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
13 changes: 13 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,16 @@
- Implement the [Cart API](https://developer.bigcommerce.com/api-reference/store-management/carts/cart/createacart).
- Implement the [Cart Items API](https://developer.bigcommerce.com/api-reference/store-management/carts/cart-items/addcartlineitem)
- Implement the [Cart Redirect URLS API](https://developer.bigcommerce.com/api-reference/store-management/carts/cart-redirect-urls/createcartredirecturl)
- Allow the use of [parameters in ProductsApi::Get](https://developer.bigcommerce.com/api-reference/store-management/catalog/products/getproductbyid).

Here's an example using PHP 8:

```php
$product = $api->catalog()->product(123)->get(include_fields: ['description', 'sku'])->getProduct();
```

### Bug Fix

Fix issue with ProductVariant::sku_id not being nullable #47 (thanks @Yorgv)


23 changes: 20 additions & 3 deletions src/BigCommerce/Api/Catalog/ProductsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,30 @@ class ProductsApi extends ResourceWithBatchUpdateApi
public const FILTER_SKU_IS = 'sku';

public const FILTER_INCLUDE_FIELDS = 'include_fields';
public const FILTER_EXCLUDE_FIELDS = 'exclude_fields';

public const FILTER_INCLUDE = 'include';
public const INCLUDE_MODIFIERS = 'modifiers';

public function get(): ProductResponse
{
return new ProductResponse($this->getResource());
public function get(
?string $include = null,
?array $include_fields = null,
?array $exclude_fields = null
): ProductResponse {
$params = [];

if (!is_null($include)) {
$params[self::FILTER_INCLUDE] = $include;
}
if (!is_null($include_fields)) {
$params[self::FILTER_INCLUDE_FIELDS] = implode(',', $include_fields);
;
}
if (!is_null($exclude_fields)) {
$params[self::FILTER_EXCLUDE_FIELDS] = implode(',', $exclude_fields);
}

return new ProductResponse($this->getResource($params));
}

public function getAll(array $filters = [], int $page = 1, int $limit = 250): ProductsResponse
Expand Down
10 changes: 8 additions & 2 deletions src/BigCommerce/Api/Generic/GetResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
namespace BigCommerce\ApiV3\Api\Generic;

use BigCommerce\ApiV3\Client;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\ResponseInterface;

trait GetResource
{
abstract public function singleResourceUrl(): string;
abstract public function getClient(): Client;

protected function getResource(): ResponseInterface
protected function getResource(array $queryParams = []): ResponseInterface
{
return $this->getClient()->getRestClient()->get($this->singleResourceUrl());
return $this->getClient()->getRestClient()->get(
$this->singleResourceUrl(),
[
RequestOptions::QUERY => $queryParams,
]
);
}
}
3 changes: 2 additions & 1 deletion tests/BigCommerce/Api/Catalog/ProductsApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public function testCanGetProducts(): void
public function testCanGetProduct(): void
{
$this->setReturnData('catalog__products__174__get.json');
$productResponse = $this->getApi()->catalog()->product(174)->get();
$productResponse = $this->getApi()->catalog()->product(174)->get(null, ['weight', 'width']);
$this->assertEquals(174, $productResponse->getProduct()->id);
$this->assertEquals('include_fields=weight%2Cwidth', $this->getLastRequest()->getUri()->getQuery());
}

public function testCanGetAllPagesForProducts(): void
Expand Down

0 comments on commit 4fbaea4

Please sign in to comment.