Skip to content

Commit a5c62b9

Browse files
authored
Merge pull request PrestaShop#116 from jolelievre/product-categories
Add product categories details
2 parents a60ecbb + a519278 commit a5c62b9

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/ApiPlatform/Resources/Product/Product.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,28 @@ class Product
217217
#[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer'], 'example' => [1, 3]])]
218218
public array $shopIds;
219219

220+
#[ApiProperty(openapiContext: [
221+
'type' => 'array',
222+
'items' => [
223+
'type' => 'object',
224+
'properties' => [
225+
'categoryId' => ['type' => 'integer'],
226+
'name' => ['type' => 'string'],
227+
'displayName' => ['type' => 'string'],
228+
],
229+
],
230+
'example' => [
231+
[
232+
'categoryId' => 2,
233+
'name' => 'Home',
234+
'displayName' => 'Home',
235+
],
236+
]])
237+
]
238+
public array $categories;
239+
240+
public int $defaultCategoryId;
241+
220242
public const QUERY_MAPPING = [
221243
'[_context][shopConstraint]' => '[shopConstraint]',
222244
'[_context][langId]' => '[displayLanguageId]',
@@ -272,6 +294,11 @@ class Product
272294
'[stockInformation][localizedAvailableLaterLabels]' => '[availableLaterLabels]',
273295
'[stockInformation][location]' => '[location]',
274296
'[stockInformation][availableDate]' => '[availableDate]',
297+
// Transform each field one by one (instead of the whole array) to avoid having an extra id field in the target
298+
'[categoriesInformation][categoriesInformation][@index][id]' => '[categories][@index][categoryId]',
299+
'[categoriesInformation][categoriesInformation][@index][name]' => '[categories][@index][name]',
300+
'[categoriesInformation][categoriesInformation][@index][displayName]' => '[categories][@index][displayName]',
301+
'[categoriesInformation][defaultCategoryId]' => '[defaultCategoryId]',
275302
];
276303

277304
public const UPDATE_MAPPING = [

tests/Integration/ApiPlatform/ProductEndpointTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636

3737
class ProductEndpointTest extends ApiTestCase
3838
{
39+
protected const ROOT_CATEGORY_ID = 2;
40+
protected const ROOT_CATEGORY_NAME = 'Home';
41+
3942
protected static array $defaultProductData = [
4043
'type' => ProductType::TYPE_STANDARD,
4144
'names' => [
@@ -123,6 +126,14 @@ class ProductEndpointTest extends ApiTestCase
123126
'shopIds' => [
124127
1,
125128
],
129+
'categories' => [
130+
[
131+
'categoryId' => self::ROOT_CATEGORY_ID,
132+
'name' => self::ROOT_CATEGORY_NAME,
133+
'displayName' => self::ROOT_CATEGORY_NAME,
134+
],
135+
],
136+
'defaultCategoryId' => self::ROOT_CATEGORY_ID,
126137
];
127138

128139
public static function setUpBeforeClass(): void

tests/Integration/ApiPlatform/ProductMultiShopEndpointTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
class ProductMultiShopEndpointTest extends ApiTestCase
3737
{
38+
protected const ROOT_CATEGORY_ID = 2;
39+
protected const ROOT_CATEGORY_NAME = 'Home';
3840
protected const DEFAULT_SHOP_GROUP_ID = 1;
3941
protected static int $secondShopGroupId;
4042

@@ -129,6 +131,14 @@ class ProductMultiShopEndpointTest extends ApiTestCase
129131
'shopIds' => [
130132
self::DEFAULT_SHOP_ID,
131133
],
134+
'categories' => [
135+
[
136+
'categoryId' => self::ROOT_CATEGORY_ID,
137+
'name' => self::ROOT_CATEGORY_NAME,
138+
'displayName' => self::ROOT_CATEGORY_NAME,
139+
],
140+
],
141+
'defaultCategoryId' => self::ROOT_CATEGORY_ID,
132142
];
133143

134144
public static function setUpBeforeClass(): void

0 commit comments

Comments
 (0)