Skip to content

Commit 5f34663

Browse files
committedAug 23, 2022
Fix attributes usage for models (#76)
Fix attributes usage for models Keep status from Akeneo for simple products Fix mapped sku usage
1 parent ee49296 commit 5f34663

8 files changed

+78
-31
lines changed
 

‎ImportExport/Processor/ProductVariantProcessor.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function ($variantSku) {
8989
$variantSkus
9090
);
9191

92-
$variantSkusUppercase = array_combine($variantSkusUppercase, $variantSkusUppercase);
92+
$variantSkusUppercase = array_combine($variantSkusUppercase, $items);
9393
foreach ($parentProduct->getVariantLinks() as $variantLink) {
9494
$variantProduct = $variantLink->getProduct();
9595
if (!$variantSkusUppercase) {
@@ -110,12 +110,14 @@ function ($variantSku) {
110110
continue;
111111
}
112112

113-
$variantProduct->setStatus(Product::STATUS_ENABLED);
113+
$variantItem = $variantSkusUppercase[$variantProduct->getSkuUppercase()];
114+
$status = empty($variantItem['enabled']) ? Product::STATUS_DISABLED : Product::STATUS_ENABLED;
115+
$variantProduct->setStatus($status);
114116

115117
unset($variantSkusUppercase[$variantProduct->getSkuUppercase()]);
116118
}
117119

118-
foreach ($variantSkusUppercase as $variantSku) {
120+
foreach ($variantSkusUppercase as $variantSku => $variantItem) {
119121
$variantProduct = $productRepository->findOneBySku($variantSku);
120122
if (!$variantProduct instanceof Product) {
121123
$context->incrementErrorEntriesCount();
@@ -139,7 +141,8 @@ function ($variantSku) {
139141
$variantProduct->addParentVariantLink($variantLink);
140142
$parentProduct->addVariantLink($variantLink);
141143

142-
$variantProduct->setStatus(Product::STATUS_ENABLED);
144+
$status = empty($variantItem['enabled']) ? Product::STATUS_DISABLED : Product::STATUS_ENABLED;
145+
$variantProduct->setStatus($status);
143146

144147
$context->incrementAddCount();
145148

‎ImportExport/Writer/ConfigurableAsyncWriter.php

+32-8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class ConfigurableAsyncWriter implements
4141

4242
private $variants = [];
4343

44+
private $origins = [];
45+
46+
private $models = [];
47+
4448
public function __construct(
4549
MessageProducerInterface $messageProducer,
4650
DoctrineHelper $doctrineHelper,
@@ -56,6 +60,8 @@ public function __construct(
5660
public function initialize()
5761
{
5862
$this->variants = [];
63+
$this->origins = [];
64+
$this->models = [];
5965

6066
$this->additionalOptionalListenerManager->disableListeners();
6167
$this->optionalListenerManager->disableListeners($this->optionalListenerManager->getListeners());
@@ -64,39 +70,57 @@ public function initialize()
6470
public function write(array $items)
6571
{
6672
foreach ($items as $item) {
73+
$origin = $item['origin'];
6774
$sku = $item['sku'];
6875

69-
if (!empty($item['family_variant'])) {
70-
if (isset($item['parent'], $this->variants[$sku])) {
71-
$parent = $item['parent'];
72-
foreach (array_keys($this->variants[$sku]) as $sku) {
73-
$this->variants[$parent][$sku] = ['parent' => $parent, 'variant' => $sku];
74-
}
76+
if (isset($item['family_variant'])) {
77+
$this->origins[$origin] = $sku;
78+
79+
if (isset($item['parent'])) {
80+
$this->models[$origin] = $item['parent'];
7581
}
7682

7783
continue;
7884
}
7985

80-
if (empty($item['parent'])) {
86+
if (!isset($item['parent'])) {
8187
continue;
8288
}
8389

8490
$parent = $item['parent'];
91+
if (!array_key_exists($parent, $this->origins)) {
92+
continue;
93+
}
8594

86-
$this->variants[$parent][$sku] = ['parent' => $parent, 'variant' => $sku];
95+
$this->variants[$parent][$origin] = [
96+
'parent' => $this->origins[$parent] ?? $parent,
97+
'variant' => $sku,
98+
'enabled' => $item['enabled'] ?? false,
99+
];
87100
}
88101
}
89102

90103
public function close()
91104
{
92105
$this->variants = [];
106+
$this->origins = [];
107+
$this->models = [];
93108

94109
$this->optionalListenerManager->enableListeners($this->optionalListenerManager->getListeners());
95110
$this->additionalOptionalListenerManager->enableListeners();
96111
}
97112

98113
public function flush()
99114
{
115+
foreach ($this->models as $levelTwo => $levelOne) {
116+
if (array_key_exists($levelTwo, $this->variants)) {
117+
foreach ($this->variants[$levelTwo] as $sku => $item) {
118+
$item['parent'] = $this->origins[$levelOne] ?? $levelOne;
119+
$this->variants[$levelOne][$sku] = $item;
120+
}
121+
}
122+
}
123+
100124
$channelId = $this->stepExecution->getJobExecution()->getExecutionContext()->get('channel');
101125

102126
$chunks = array_chunk($this->variants, self::VARIANTS_BATCH_SIZE, true);

‎Integration/AkeneoSearchBuilder.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public function getFilters(string $json = null): array
3535
$option['operator'],
3636
$option['value'],
3737
array_merge(
38-
(isset($option['scope']) ? ['scope' => $option['scope']] : []),
39-
(isset($option['locale']) ? ['locale' => $option['locale']] : []),
40-
(isset($option['locales']) ? ['locales' => $option['locales']] : [])
38+
isset($option['scope']) ? ['scope' => $option['scope']] : [],
39+
isset($option['locale']) ? ['locale' => $option['locale']] : [],
40+
isset($option['locales']) ? ['locales' => $option['locales']] : []
4141
)
4242
);
4343
}

‎Integration/AkeneoTransport.php

+13-11
Original file line numberDiff line numberDiff line change
@@ -238,30 +238,33 @@ public function getProducts(int $pageSize)
238238
);
239239
}
240240

241-
public function getProductsList(int $pageSize): iterable
241+
public function getProductsList(int $pageSize, ?string $family = null): iterable
242242
{
243-
$this->initAttributesList();
244-
243+
$attributeMapping = $this->getAttributeMapping();
245244
$queryParams = [
246245
'scope' => $this->transportEntity->getAkeneoActiveChannel(),
247-
'search' => $this->akeneoSearchBuilder->getFilters($this->transportEntity->getProductFilter()),
248-
'attributes' => key($this->attributes),
246+
'search' => $this->akeneoSearchBuilder->getFilters(
247+
$family ?
248+
json_encode(['family' => [['operator' => 'IN', 'value' => [$family]]]]) :
249+
$this->transportEntity->getProductFilter()
250+
),
251+
'attributes' => $attributeMapping['sku'] ?? reset($attributeMapping),
249252
];
250253

251254
if ($this->transportEntity->getSyncProducts() === SyncProductsDataProvider::PUBLISHED) {
252255
return new ConfigurableProductIterator(
253256
$this->client->getPublishedProductApi()->all($pageSize, $queryParams),
254257
$this->client,
255258
$this->logger,
256-
$this->getAttributeMapping()
259+
$attributeMapping
257260
);
258261
}
259262

260263
return new ConfigurableProductIterator(
261264
$this->client->getProductApi()->all($pageSize, $queryParams),
262265
$this->client,
263266
$this->logger,
264-
$this->getAttributeMapping()
267+
$attributeMapping
265268
);
266269
}
267270

@@ -292,19 +295,18 @@ public function getProductModels(int $pageSize)
292295

293296
public function getProductModelsList(int $pageSize): iterable
294297
{
295-
$this->initAttributesList();
296-
298+
$attributeMapping = $this->getAttributeMapping();
297299
$queryParams = [
298300
'scope' => $this->transportEntity->getAkeneoActiveChannel(),
299301
'search' => $this->akeneoSearchBuilder->getFilters($this->transportEntity->getConfigurableProductFilter()),
300-
'attributes' => key($this->attributes),
302+
'attributes' => $attributeMapping['sku'] ?? reset($attributeMapping),
301303
];
302304

303305
return new ConfigurableProductIterator(
304306
$this->client->getProductModelApi()->all($pageSize, $queryParams),
305307
$this->client,
306308
$this->logger,
307-
$this->getAttributeMapping()
309+
$attributeMapping
308310
);
309311
}
310312

‎Integration/AkeneoTransportInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getProducts(int $pageSize);
5151
*/
5252
public function getProductModels(int $pageSize);
5353

54-
public function getProductsList(int $pageSize): iterable;
54+
public function getProductsList(int $pageSize, ?string $family = null): iterable;
5555

5656
public function getProductModelsList(int $pageSize): iterable;
5757

‎Integration/Connector/ConfigurableProductConnector.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,20 @@ protected function getConnectorSource()
6464
}
6565

6666
$iterator = new \AppendIterator();
67-
$iterator->append($this->transport->getProductsList(self::PAGE_SIZE));
6867
$iterator->append($this->transport->getProductModelsList(self::PAGE_SIZE));
6968

70-
return $iterator;
69+
$processed = [];
70+
71+
return new \CallbackFilterIterator(
72+
$iterator,
73+
function ($current, $key, $iterator) use (&$processed) {
74+
if (isset($current['family_variant'], $current['family']) && empty($processed[$current['family']])) {
75+
$iterator->append($this->transport->getProductsList(self::PAGE_SIZE, $current['family']));
76+
$processed[$current['family']] = true;
77+
}
78+
79+
return true;
80+
}
81+
);
7182
}
7283
}

‎Integration/Iterator/ConfigurableProductIterator.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public function doCurrent()
3333
}
3434
}
3535

36-
return ['sku' => (string)$sku, 'parent' => $item['parent'] ?? null, 'family_variant' => $item['family_variant'] ?? null];
36+
return [
37+
'sku' => (string)$sku,
38+
'origin' => $item['identifier'] ?? $item['code'],
39+
'parent' => $item['parent'] ?? null,
40+
'family_variant' => $item['family_variant'] ?? null,
41+
'family' => $item['family'] ?? null,
42+
'enabled' => $item['enabled'] ?? false,
43+
];
3744
}
3845
}

‎Resources/translations/messages.en.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ oro:
8888
product:
8989
label: Product connector
9090
configurable_product:
91-
label: Configurable product connector
91+
label: Product Variants connector
9292
attribute_family:
9393
label: Attribute family connector
9494
attribute:

0 commit comments

Comments
 (0)
Please sign in to comment.