Skip to content

Commit

Permalink
Fix attributes usage for models (#76)
Browse files Browse the repository at this point in the history
Fix attributes usage for models
Keep status from Akeneo for simple products
Fix mapped sku usage
  • Loading branch information
dxops committed Aug 23, 2022
1 parent b0a3266 commit 543a98e
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 31 deletions.
11 changes: 7 additions & 4 deletions ImportExport/Processor/ProductVariantProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function ($variantSku) {
$variantSkus
);

$variantSkusUppercase = array_combine($variantSkusUppercase, $variantSkusUppercase);
$variantSkusUppercase = array_combine($variantSkusUppercase, $items);
foreach ($parentProduct->getVariantLinks() as $variantLink) {
$variantProduct = $variantLink->getProduct();
if (!$variantSkusUppercase) {
Expand All @@ -110,12 +110,14 @@ function ($variantSku) {
continue;
}

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

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

foreach ($variantSkusUppercase as $variantSku) {
foreach ($variantSkusUppercase as $variantSku => $variantItem) {
$variantProduct = $productRepository->findOneBySku($variantSku);
if (!$variantProduct instanceof Product) {
$context->incrementErrorEntriesCount();
Expand All @@ -139,7 +141,8 @@ function ($variantSku) {
$variantProduct->addParentVariantLink($variantLink);
$parentProduct->addVariantLink($variantLink);

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

$context->incrementAddCount();

Expand Down
40 changes: 32 additions & 8 deletions ImportExport/Writer/ConfigurableAsyncWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class ConfigurableAsyncWriter implements

private $variants = [];

private $origins = [];

private $models = [];

public function __construct(
MessageProducerInterface $messageProducer,
DoctrineHelper $doctrineHelper,
Expand All @@ -56,6 +60,8 @@ public function __construct(
public function initialize()
{
$this->variants = [];
$this->origins = [];
$this->models = [];

$this->additionalOptionalListenerManager->disableListeners();
$this->optionalListenerManager->disableListeners($this->optionalListenerManager->getListeners());
Expand All @@ -64,39 +70,57 @@ public function initialize()
public function write(array $items)
{
foreach ($items as $item) {
$origin = $item['origin'];
$sku = $item['sku'];

if (!empty($item['family_variant'])) {
if (isset($item['parent'], $this->variants[$sku])) {
$parent = $item['parent'];
foreach (array_keys($this->variants[$sku]) as $sku) {
$this->variants[$parent][$sku] = ['parent' => $parent, 'variant' => $sku];
}
if (isset($item['family_variant'])) {
$this->origins[$origin] = $sku;

if (isset($item['parent'])) {
$this->models[$origin] = $item['parent'];
}

continue;
}

if (empty($item['parent'])) {
if (!isset($item['parent'])) {
continue;
}

$parent = $item['parent'];
if (!array_key_exists($parent, $this->origins)) {
continue;
}

$this->variants[$parent][$sku] = ['parent' => $parent, 'variant' => $sku];
$this->variants[$parent][$origin] = [
'parent' => $this->origins[$parent] ?? $parent,
'variant' => $sku,
'enabled' => $item['enabled'] ?? false,
];
}
}

public function close()
{
$this->variants = [];
$this->origins = [];
$this->models = [];

$this->optionalListenerManager->enableListeners($this->optionalListenerManager->getListeners());
$this->additionalOptionalListenerManager->enableListeners();
}

public function flush()
{
foreach ($this->models as $levelTwo => $levelOne) {
if (array_key_exists($levelTwo, $this->variants)) {
foreach ($this->variants[$levelTwo] as $sku => $item) {
$item['parent'] = $this->origins[$levelOne] ?? $levelOne;
$this->variants[$levelOne][$sku] = $item;
}
}
}

$channelId = $this->stepExecution->getJobExecution()->getExecutionContext()->get('channel');

$chunks = array_chunk($this->variants, self::VARIANTS_BATCH_SIZE, true);
Expand Down
6 changes: 3 additions & 3 deletions Integration/AkeneoSearchBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function getFilters(string $json = null): array
$option['operator'],
$option['value'],
array_merge(
(isset($option['scope']) ? ['scope' => $option['scope']] : []),
(isset($option['locale']) ? ['locale' => $option['locale']] : []),
(isset($option['locales']) ? ['locales' => $option['locales']] : [])
isset($option['scope']) ? ['scope' => $option['scope']] : [],
isset($option['locale']) ? ['locale' => $option['locale']] : [],
isset($option['locales']) ? ['locales' => $option['locales']] : []
)
);
}
Expand Down
24 changes: 13 additions & 11 deletions Integration/AkeneoTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,30 +238,33 @@ public function getProducts(int $pageSize)
);
}

public function getProductsList(int $pageSize): iterable
public function getProductsList(int $pageSize, ?string $family = null): iterable
{
$this->initAttributesList();

$attributeMapping = $this->getAttributeMapping();
$queryParams = [
'scope' => $this->transportEntity->getAkeneoActiveChannel(),
'search' => $this->akeneoSearchBuilder->getFilters($this->transportEntity->getProductFilter()),
'attributes' => key($this->attributes),
'search' => $this->akeneoSearchBuilder->getFilters(
$family ?
json_encode(['family' => [['operator' => 'IN', 'value' => [$family]]]]) :
$this->transportEntity->getProductFilter()
),
'attributes' => $attributeMapping['sku'] ?? reset($attributeMapping),
];

if ($this->transportEntity->getSyncProducts() === SyncProductsDataProvider::PUBLISHED) {
return new ConfigurableProductIterator(
$this->client->getPublishedProductApi()->all($pageSize, $queryParams),
$this->client,
$this->logger,
$this->getAttributeMapping()
$attributeMapping
);
}

return new ConfigurableProductIterator(
$this->client->getProductApi()->all($pageSize, $queryParams),
$this->client,
$this->logger,
$this->getAttributeMapping()
$attributeMapping
);
}

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

public function getProductModelsList(int $pageSize): iterable
{
$this->initAttributesList();

$attributeMapping = $this->getAttributeMapping();
$queryParams = [
'scope' => $this->transportEntity->getAkeneoActiveChannel(),
'search' => $this->akeneoSearchBuilder->getFilters($this->transportEntity->getConfigurableProductFilter()),
'attributes' => key($this->attributes),
'attributes' => $attributeMapping['sku'] ?? reset($attributeMapping),
];

return new ConfigurableProductIterator(
$this->client->getProductModelApi()->all($pageSize, $queryParams),
$this->client,
$this->logger,
$this->getAttributeMapping()
$attributeMapping
);
}

Expand Down
2 changes: 1 addition & 1 deletion Integration/AkeneoTransportInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function getProducts(int $pageSize);
*/
public function getProductModels(int $pageSize);

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

public function getProductModelsList(int $pageSize): iterable;

Expand Down
15 changes: 13 additions & 2 deletions Integration/Connector/ConfigurableProductConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,20 @@ protected function getConnectorSource()
}

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

return $iterator;
$processed = [];

return new \CallbackFilterIterator(
$iterator,
function ($current, $key, $iterator) use (&$processed) {
if (isset($current['family_variant'], $current['family']) && empty($processed[$current['family']])) {
$iterator->append($this->transport->getProductsList(self::PAGE_SIZE, $current['family']));
$processed[$current['family']] = true;
}

return true;
}
);
}
}
9 changes: 8 additions & 1 deletion Integration/Iterator/ConfigurableProductIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public function doCurrent()
}
}

return ['sku' => (string)$sku, 'parent' => $item['parent'] ?? null, 'family_variant' => $item['family_variant'] ?? null];
return [
'sku' => (string)$sku,
'origin' => $item['identifier'] ?? $item['code'],
'parent' => $item['parent'] ?? null,
'family_variant' => $item['family_variant'] ?? null,
'family' => $item['family'] ?? null,
'enabled' => $item['enabled'] ?? false,
];
}
}
2 changes: 1 addition & 1 deletion Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ oro:
product:
label: Product connector
configurable_product:
label: Configurable product connector
label: Product Variants connector
attribute_family:
label: Attribute family connector
attribute:
Expand Down

0 comments on commit 543a98e

Please sign in to comment.