Skip to content

Commit f10246f

Browse files
author
Samuel Gomis
committed
feat(API-1962): Add part in guided tutorial "Collect product variations"
1 parent 6c803b7 commit f10246f

File tree

1 file changed

+125
-1
lines changed

1 file changed

+125
-1
lines changed

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

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,128 @@ function getProductVariants(): array
278278

279279
```
280280

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