Skip to content

Commit 5b18a61

Browse files
authored
Merge pull request #104 from aligent/feature/product-option-values-improvements
Feature/product option values improvements
2 parents 4c7e642 + f80eeb1 commit 5b18a61

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

src/BigCommerce/Api/Catalog/Products/ProductOption/ProductOptionValuesApi.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
use BigCommerce\ApiV3\ResponseModels\Product\ProductOptionValueResponse;
88
use BigCommerce\ApiV3\ResponseModels\Product\ProductOptionValuesResponse;
99

10+
/**
11+
* Referred to in the documentation as Product Variant Options Values
12+
*
13+
* https://developer.bigcommerce.com/api-reference/store-management/catalog/product-variant-option-values/createoptionvalue
14+
*
15+
*/
1016
class ProductOptionValuesApi extends ResourceApi
1117
{
1218
private int $productId;

src/BigCommerce/ResponseModels/OptionsResponse.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,16 @@
77

88
class OptionsResponse extends PaginatedResponse
99
{
10-
/**
11-
* @var ProductOption[]
12-
*/
13-
private array $options;
14-
1510
/**
1611
* @return ProductOption[]
1712
*/
1813
public function getOptions(): array
1914
{
20-
return $this->options;
15+
return $this->getData();
2116
}
2217

23-
protected function addData(array $data): void
18+
protected function resourceClass(): string
2419
{
25-
$this->options = array_map(function (\stdClass $o) {
26-
return new ProductOption($o);
27-
}, $data);
20+
return ProductOption::class;
2821
}
2922
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace BigCommerce\Tests\Api\Catalog\Products\ProductOption;
4+
5+
use BigCommerce\ApiV3\ResourceModels\Catalog\Product\ProductOptionValue;
6+
use BigCommerce\Tests\BigCommerceApiTest;
7+
8+
class ProductOptionValuesApiTest extends BigCommerceApiTest
9+
{
10+
public function testHasProductIdAndOptionIdOnCreate(): void
11+
{
12+
$this->setReturnData('catalog__products__1__options__2__values__create.json');
13+
$productId = 1;
14+
$optionId = 2;
15+
16+
$optionValue = new ProductOptionValue();
17+
$optionValue->sort_order = 2;
18+
$optionValue->is_default = true;
19+
$optionValue->label = 'Colors';
20+
$optionValue->value_data = (object)[
21+
"colors" => ["#123c91, #FFFF00, #397a44"]
22+
];
23+
24+
$productOptionValuesApi = $this->getApi()->catalog()->product($productId)->option($optionId)->values();
25+
$this->assertEquals($productId, $productOptionValuesApi->getProductId(), 'Product ID not set correctly');
26+
$this->assertEquals($optionId, $productOptionValuesApi->getParentResourceId(), 'Option ID not set correctly');
27+
$productOptionValuesApi->create($optionValue);
28+
$this->assertEquals("catalog/products/{$productId}/options/{$optionId}/values", $this->getLastRequestPath());
29+
}
30+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"data": {
3+
"id": 44,
4+
"label": "Pick a color",
5+
"sort_order": 9,
6+
"value_data": {
7+
"colors": [
8+
"#123c91, #FFFF00, #397a44"
9+
]
10+
},
11+
"is_default": false
12+
}
13+
}

0 commit comments

Comments
 (0)