Skip to content

Commit de278ab

Browse files
author
Samuel Gomis
committed
Merge branch 'API-1962' into develop
2 parents 3af68d8 + ba85183 commit de278ab

File tree

1 file changed

+130
-2
lines changed

1 file changed

+130
-2
lines changed

content/tutorials/guides/how-to-collect-product-variations.md

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ In this tutorial, we will introduce you to the two use cases you may encounter f
9797
- **Use case 2**: Collect all product variation information on 1 level only)
9898

9999
::: info
100-
We noticed that many e-commerce solutions understand product variation on only one level. This means that for Akeneo a special recollection of the variations must be done to have them all on the same level.
100+
**Use case 2**
101+
<br>
102+
We noticed that many e-commerce solutions understand product variation on only one level. This means that for Akeneo **a special recollection of the variations must be done to have them all on the same level.**
103+
<br>
104+
Stay tuned! Our teams are currently working on this specific use case and should release it very soon.
101105
:::
102106

103107
### 0 - Initialization
@@ -278,4 +282,128 @@ function getProductVariants(): array
278282

279283
```
280284

281-
Again, treat each product like a simple product. Please refer to the guided tutorial <a href="/tutorials/how-to-get-families-and-attributes.html" target="_blank" rel="noopener noreferrer">How to get families and attributes</a>
285+
Again, treat each product like a simple product. Please refer to the guided tutorial <a href="/tutorials/how-to-get-families-and-attributes.html" target="_blank" rel="noopener noreferrer">How to get families and attributes</a>
286+
287+
### Use case 2: Collect product variation information - set it all on 1 level
288+
289+
#### 1. Collect product models
290+
291+
##### 1.1 You are following the App workflow?
292+
293+
In the guided tutorial <a href="/tutorials/how-to-get-families-and-attributes.html" target="_blank" rel="noopener noreferrer">How to get families and attributes</a>, we have stored a **family_code_list**. It’s time to use it!
294+
295+
```php [activate:PHP]
296+
297+
function getProductModels(): array
298+
{
299+
$client = buildApiClient();
300+
301+
$maxProductsPerPage = 100;
302+
$maxFamiliesPerQuery = 3;
303+
$scope = 'ecommerce';
304+
305+
// Get family codes and locales from storage
306+
$familyCodes = getFamilyCodes();
307+
$locales = getLocales('fr');
308+
309+
$familyCodeChunks = array_chunk($familyCodes, $maxFamiliesPerQuery);
310+
311+
$apiUrl = '/api/rest/v1/product-models?'
312+
. 'locales=%s'
313+
. '&scope=%s'
314+
. '&search={"family":[{"operator":"IN","value":%s}],"parent":[{"operator":"EMPTY"}]}'
315+
. '&limit=' . $maxProductsPerPage;
316+
317+
318+
// Collect product models from API
319+
$productModels = [];
320+
foreach ($familyCodeChunks as $familyCodes) {
321+
$response = $client->get(sprintf($apiUrl, $locales, $scope, json_encode($familyCodes)));
322+
$data = json_decode($response->getBody()->getContents(), true);
323+
$productModels[] = $data['_embedded']['items'];
324+
}
325+
326+
$productModels = array_merge(...$productModels);
327+
328+
// Get variants from storage
329+
$variants = getVariants();
330+
foreach ($productModels as $key => $productModel) {
331+
foreach ($variants as $variant) {
332+
if ($productModel['family_variant'] === $variant['code']) {
333+
// extract all variations level
334+
$axes = array_column($variant['variant_attribute_sets'], 'axes');
335+
// build flat axes
336+
$axes = array_column($axes, 0);
337+
$productModels[$key]['axes'] = $axes;
338+
}
339+
}
340+
}
341+
342+
saveProductModels($productModels);
343+
}
344+
345+
346+
```
347+
348+
##### 1.2 - You are not following the App workflow?
349+
350+
::: warning
351+
Make sure to get the list of your family variants before continuing like in <a href="/tutorials/how-to-get-families-and-attributes.html" target="_blank" rel="noopener noreferrer">How to get families and attributes</a>
352+
:::
353+
354+
```php [activate:PHP]
355+
356+
function getProductModels(): array
357+
{
358+
$client = buildApiClient();
359+
360+
$maxProductsPerPage = 100;
361+
$scope = 'ecommerce';
362+
363+
$apiUrl = '/api/rest/v1/product-models?'
364+
. '&scope=%s'
365+
. '&search={"parent":[{"operator":"EMPTY"}]}'
366+
. '&limit=' . $maxProductsPerPage;
367+
368+
// Collect product models from paginated API
369+
$response = $client->get(sprintf($apiUrl, $scope));
370+
$data = json_decode($response->getBody()->getContents(), true);
371+
372+
$productModels = [];
373+
$productModels[] = $data['_embedded']['items'];
374+
while (array_key_exists('next', $data['_links'])) {
375+
$response = $client->get($data['_links']['next']['href']);
376+
$data = json_decode($response->getBody()->getContents(), true);
377+
$productModels[] = $data['_embedded']['items'];
378+
}
379+
380+
$productModels = array_merge(...$productModels);
381+
382+
// Get variants from storage
383+
$variants = getVariants();
384+
foreach ($productModels as $key => $productModel) {
385+
foreach ($variants as $variant) {
386+
if ($productModel['family_variant'] === $variant['code']) {
387+
// extract all variations level
388+
$axes = array_column($variant['variant_attribute_sets'], 'axes');
389+
// build flat axes
390+
$axes = array_column($axes, 0);
391+
$productModels[$key]['axes'] = $axes;
392+
}
393+
}
394+
}
395+
396+
saveProductModels($productModels);
397+
}
398+
399+
```
400+
401+
#### 2. Process product model
402+
403+
##### 2.1. Parse and store the product model
404+
405+
Parse and store the product model like in [**2.1. Parse and store the product model**](/tutorials/how-to-collect-product-variations.html#21-parse-and-store-the-product-model)
406+
407+
##### 2.2. Collect its product variants
408+
409+
Collect product variants the same way than in [**2.2. Collect its product variants**](/tutorials/how-to-collect-product-variations.html#22-collect-its-product-variants)

0 commit comments

Comments
 (0)