Skip to content

Commit 2c9a453

Browse files
authored
Merge pull request #48 from aligent/fix/use-params-with-get-product
Fix/use params with get product
2 parents 341ad85 + 2db9a0f commit 2c9a453

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

RELEASE_NOTES.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
### New Features
2+
3+
Allow the use of [parameters in ProductsApi::Get](https://developer.bigcommerce.com/api-reference/store-management/catalog/products/getproductbyid).
4+
5+
Here's an example using PHP 8:
6+
7+
```php
8+
$product = $api->catalog()->product(123)->get(include_fields: ['description', 'sku'])->getProduct();
9+
```
10+
111
### Bug Fix
212

3-
Fix issue with Metafield APIs missing create and update endpoints.
13+
Fix issue with ProductVariant::sku_id not being nullable #47 (thanks @Yorgv)
14+

src/BigCommerce/Api/Catalog/ProductsApi.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,30 @@ class ProductsApi extends ResourceWithBatchUpdateApi
2424
public const FILTER_SKU_IS = 'sku';
2525

2626
public const FILTER_INCLUDE_FIELDS = 'include_fields';
27+
public const FILTER_EXCLUDE_FIELDS = 'exclude_fields';
2728

2829
public const FILTER_INCLUDE = 'include';
2930
public const INCLUDE_MODIFIERS = 'modifiers';
3031

31-
public function get(): ProductResponse
32-
{
33-
return new ProductResponse($this->getResource());
32+
public function get(
33+
?string $include = null,
34+
?array $include_fields = null,
35+
?array $exclude_fields = null
36+
): ProductResponse {
37+
$params = [];
38+
39+
if (!is_null($include)) {
40+
$params[self::FILTER_INCLUDE] = $include;
41+
}
42+
if (!is_null($include_fields)) {
43+
$params[self::FILTER_INCLUDE_FIELDS] = implode(',', $include_fields);
44+
;
45+
}
46+
if (!is_null($exclude_fields)) {
47+
$params[self::FILTER_EXCLUDE_FIELDS] = implode(',', $exclude_fields);
48+
}
49+
50+
return new ProductResponse($this->getResource($params));
3451
}
3552

3653
public function getAll(array $filters = [], int $page = 1, int $limit = 250): ProductsResponse

src/BigCommerce/Api/Generic/GetResource.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
namespace BigCommerce\ApiV3\Api\Generic;
44

55
use BigCommerce\ApiV3\Client;
6+
use GuzzleHttp\RequestOptions;
67
use Psr\Http\Message\ResponseInterface;
78

89
trait GetResource
910
{
1011
abstract public function singleResourceUrl(): string;
1112
abstract public function getClient(): Client;
1213

13-
protected function getResource(): ResponseInterface
14+
protected function getResource(array $queryParams = []): ResponseInterface
1415
{
15-
return $this->getClient()->getRestClient()->get($this->singleResourceUrl());
16+
return $this->getClient()->getRestClient()->get(
17+
$this->singleResourceUrl(),
18+
[
19+
RequestOptions::QUERY => $queryParams,
20+
]
21+
);
1622
}
1723
}

tests/BigCommerce/Api/Catalog/ProductsApiTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public function testCanGetProducts(): void
3030
public function testCanGetProduct(): void
3131
{
3232
$this->setReturnData('catalog__products__174__get.json');
33-
$productResponse = $this->getApi()->catalog()->product(174)->get();
33+
$productResponse = $this->getApi()->catalog()->product(174)->get(null, ['weight', 'width']);
3434
$this->assertEquals(174, $productResponse->getProduct()->id);
35+
$this->assertEquals('include_fields=weight%2Cwidth', $this->getLastRequest()->getUri()->getQuery());
3536
}
3637

3738
public function testCanGetAllPagesForProducts(): void

0 commit comments

Comments
 (0)