Skip to content

Commit 40f600a

Browse files
author
Samuel Gomis
committed
fix(API-1953): fix
1 parent 7eaec8c commit 40f600a

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434

3535
<div class="endpoint-container">
3636
<div class="endpoint-text">REST API endpoint(s):</div>
37-
<a href="/api-reference.html#get_families" class="endpoint-link" target="_blank" rel="noopener noreferrer">family</a>
38-
<a href="/api-reference.html#Familyvariants" class="endpoint-link" target="_blank" rel="noopener noreferrer">family variants</a>
39-
<a href="/api-reference.html#Attribute" class="endpoint-link" target="_blank" rel="noopener noreferrer">attributes</a>
37+
<a href="/api-reference.html#get_products_uuid" class="endpoint-link" target="_blank" rel="noopener noreferrer">products</a>
38+
<a href="/api-reference.html#get_product_models" class="endpoint-link" target="_blank" rel="noopener noreferrer">product models</a>
39+
<a href="/api-reference.html#get_families__family_code__variants__code__" class="endpoint-link" target="_blank" rel="noopener noreferrer">family variants</a>
4040
</div>
4141

4242
<div class="block-requirements">
@@ -62,6 +62,9 @@ In the PIM we handle product models and product variations.
6262

6363
![scheme_variants](../../img/tutorials/how-to-collect-product-variations/scheme_variants.png)
6464

65+
::: tips
66+
Before digging into the code you can find out more about these concepts in our [helpcenter](https://help.akeneo.com/pim/serenity/articles/what-about-products-variants.html#about-products-with-variants).
67+
:::
6568

6669
Here are quick definitions:
6770

@@ -108,31 +111,34 @@ $appToken = 'your_app_token'; // Token provided during oAuth steps
108111
// https://docs.guzzlephp.org/en/stable/overview.html#installation
109112

110113
// Set your client for querying Akeneo API as follows
111-
function getApiClient(): GuzzleHttp\Client
114+
function buildApiClient(): GuzzleHttp\Client
112115
{
113116
$pimUrl = '<PIM_URL>';
114117
$appToken = '<APP_TOKEN>'; // Token provided during oauth steps
115118

119+
// If you haven't done it yet,
120+
// please follow the Guzzle official documentation to install the client
121+
// https://docs.guzzlephp.org/en/stable/overview.html#installation
122+
116123
return new GuzzleHttp\Client([
117124
'base_uri' => $pimUrl,
118125
'headers' => ['Authorization' => 'Bearer ' . $appToken],
119126
]);
120127
}
121-
122128
```
123129

124130
### Use case 1: Collect product variation information - all levels
125131

126132
#### 1. Collect product models
127133
##### 1.1 You are following the App workflow?
128134

129-
In the guided tutorial [**"How to get families and attributes"**](/tutorials/how-to-get-families-and-attributes.html), we have stored a family_code_list. It’s time to use it!
135+
In the guided tutorial [**"How to get families and attributes"**](/tutorials/how-to-get-families-and-attributes.html), we have stored a **family_code_list**. It’s time to use it!
130136

131137
```php [activate:PHP]
132138

133139
function getProductModels(): array
134140
{
135-
$client = getApiClient();
141+
$client = buildApiClient();
136142

137143
$maxProductsPerPage = 100;
138144
$maxFamiliesPerQuery = 3;
@@ -173,7 +179,7 @@ Simply get the attribute type by requesting the API
173179

174180
function getProductModels(): array
175181
{
176-
$client = getApiClient();
182+
$client = buildApiClient();
177183

178184
$maxProductsPerPage = 100;
179185
$scope = 'ecommerce';
@@ -205,7 +211,7 @@ function getProductModels(): array
205211
##### 2.1. Parse and store the product model
206212
Parse and store a product or a product model is definitely the same thing. Please have a how to our guided tutorial [**"How to get product information"**](/tutorials/how-to-collect-products.html).
207213

208-
##### 2.2. Parse and store the product model
214+
##### 2.2. Collect its family variant
209215
###### 2.2.1 You are following the App workflow?
210216

211217
Good news: you already store the family variant in the guided tutorial [**"How to get families and attributes"**](/tutorials/how-to-get-families-and-attributes.html). Go ahead!
@@ -217,7 +223,7 @@ Query the API.
217223

218224
function getFamilyVariants(): array
219225
{
220-
$client = getApiClient();
226+
$client = buildApiClient();
221227

222228
$maxProductsPerPage = 100;
223229
$apiUrl = '/api/rest/v1/families/%s/variants?limit=' . $maxProductsPerPage;
@@ -247,29 +253,31 @@ To get product variants associated to a product model, ask to the API
247253

248254
```php [activate:PHP]
249255

250-
function getProductsUuid(): array
256+
function getProductVariants(): array
251257
{
252-
$client = getApiClient();
258+
$client = buildApiClient();
253259

254260
$maxProductsPerPage = 100;
255261
$maxProductModelsPerQuery = 3;
256262

257-
$productModelCodesChunks = array_chunk(getProductModelCodes(), $maxProductModelsPerQuery);
263+
// Get product model codes from storage
264+
$productModelCodes = getProductModelCodes();
265+
266+
$productModelCodesChunks = array_chunk($productModelCodes, $maxProductModelsPerQuery);
258267

259268
$apiUrl = '/api/rest/v1/products-uuid?'
260-
. 'search={"parent":[{"operator":"=","value":%s}]}'
269+
. 'search={"parent":[{"operator":"IN","value":%s}]}'
261270
. '&limit=' . $maxProductsPerPage;
262271

263-
264-
// Collect product models from paginated API
265-
$productsUuid = [];
272+
// Collect product models from API
273+
$productVariants = [];
266274
foreach ($productModelCodesChunks as $productModelCodes) {
267275
$response = $client->get(sprintf($apiUrl, json_encode($productModelCodes)));
268276
$data = json_decode($response->getBody()->getContents(), true);
269-
$productsUuid[] = $data['_embedded']['items'];
277+
$productVariants[] = $data['_embedded']['items'];
270278
}
271279

272-
$productsUuid = array_merge(...$productsUuid);
280+
return array_merge(...$productVariants);
273281
}
274282

275283
```

0 commit comments

Comments
 (0)