Skip to content

Commit

Permalink
AKM-28: Configurable products import issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dxops committed Aug 10, 2021
1 parent efa145a commit 1eeea28
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 38 deletions.
36 changes: 11 additions & 25 deletions ImportExport/Processor/AsyncProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,24 @@ private function updateVariants(array &$item)
{
$sku = $item['sku'];

if (!empty($item['family_variant']) && empty($this->variants[$sku])) {
$this->variants[$sku][''] = ['parent' => $sku, 'variant' => false];
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];
}
}

return;
}

if (empty($item['parent'])) {
return;
}

$parent = $item['parent'];

$this->variants[$parent][$sku] = ['parent' => $parent, 'variant' => $sku];
unset($this->variants[$sku]['']);
}

public function initialize()
Expand All @@ -43,28 +50,7 @@ public function initialize()

public function flush()
{
if (!$this->variants) {
return;
}

$resolvedVariants = [];

foreach ($this->variants as $parent => $variants) {
foreach ($variants as $variantKey => $variant) {
if (array_key_exists($variantKey, $this->variants)) {
foreach ($this->variants[$variantKey] as $resolvedVariantKey => $resolvedVariant) {
$resolvedVariant['parent'] = $parent;
$resolvedVariants[$parent][$resolvedVariantKey] = $resolvedVariant;
}

continue;
}

$resolvedVariants[$parent][$variantKey] = $variant;
}
}

$this->cacheProvider->save('product_variants', $resolvedVariants);
$this->cacheProvider->save('product_variants', $this->variants);
$this->variants = [];
}
}
59 changes: 46 additions & 13 deletions ImportExport/Processor/ProductVariantProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function setStepExecution(StepExecution $stepExecution)
public function process($items)
{
$parentSkus = array_column($items, 'parent');
$variantSkus = array_values(array_filter(array_column($items, 'variant')));
$variantSkus = array_values(array_column($items, 'variant'));

$parentSku = reset($parentSkus);

Expand All @@ -70,8 +70,13 @@ public function process($items)
$parentProduct = $productRepository->findOneBySku($parentSku);
if (!$parentProduct instanceof Product) {
$context->incrementErrorEntriesCount();

$errorMessages = [$this->translator->trans('oro.product.product_by_sku.not_found', [], 'validators')];
$errorMessages = [
$this->translator->trans(
'oro.akeneo.validator.product_by_sku.not_found',
['%sku%' => $parentSku],
'validators'
),
];
$this->strategyHelper->addValidationErrors($errorMessages, $context);

return null;
Expand All @@ -91,13 +96,17 @@ function ($variantSku) {
$parentProduct->removeVariantLink($variantLink);
$variantProduct->setStatus(Product::STATUS_DISABLED);
$objectManager->remove($variantLink);
$context->incrementDeleteCount();

continue;
}

if (!array_key_exists($variantProduct->getSkuUppercase(), $variantSkusUppercase)) {
$parentProduct->removeVariantLink($variantLink);
$variantProduct->setStatus(Product::STATUS_DISABLED);
$objectManager->remove($variantLink);
$context->incrementDeleteCount();

continue;
}

Expand All @@ -106,21 +115,35 @@ function ($variantSku) {
unset($variantSkusUppercase[$variantProduct->getSkuUppercase()]);
}

$variantLinks = [];
foreach ($variantSkusUppercase as $variantSku) {
$variantProduct = $productRepository->findOneBySku($variantSku);
if ($variantProduct instanceof Product) {
$variantLink = new ProductVariantLink();
$variantLink->setProduct($variantProduct);
$variantLink->setParentProduct($parentProduct);
if (!$variantProduct instanceof Product) {
$context->incrementErrorEntriesCount();

$errorMessages = [
$this->translator->trans(
'oro.akeneo.validator.product_by_sku.not_found',
['%sku%' => $variantSku],
'validators'
),
];
$this->strategyHelper->addValidationErrors($errorMessages, $context);

$variantProduct->addParentVariantLink($variantLink);
$parentProduct->addVariantLink($variantLink);
continue;
}

$variantProduct->setStatus(Product::STATUS_ENABLED);
$variantLink = new ProductVariantLink();
$variantLink->setProduct($variantProduct);
$variantLink->setParentProduct($parentProduct);

$variantLinks[$variantProduct->getSku()] = $variantLink;
}
$variantProduct->addParentVariantLink($variantLink);
$parentProduct->addVariantLink($variantLink);

$variantProduct->setStatus(Product::STATUS_ENABLED);

$context->incrementAddCount();

$objectManager->persist($variantLink);
}

$validationErrors = $this->strategyHelper->validateEntity($parentProduct);
Expand All @@ -142,6 +165,16 @@ function ($variantSku) {

if ($parentProduct->getVariantLinks()->isEmpty()) {
$parentProduct->setStatus(Product::STATUS_DISABLED);

$context->incrementErrorEntriesCount();
$errorMessages = [
$this->translator->trans(
'oro.akeneo.validator.product_variants.empty',
['%sku%' => $parentSku],
'validators'
),
];
$this->strategyHelper->addValidationErrors($errorMessages, $context);
}

$context->incrementUpdateCount();
Expand Down
2 changes: 2 additions & 0 deletions Resources/translations/validators.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ oro.akeneo:
message: 'Only letters, numbers and underscores and semi colon for separator are allowed.'
attribute_mapping:
message: 'A single value must be separated with a colon (akeneo_code:oro_attribute_name). Values must be separated with a semi-colon (custom_sku:sku;name:names).'
product_by_sku.not_found: 'Product "%sku%" not found.'
product_variants.empty: 'Product "%sku%" variant products are empty.'

0 comments on commit 1eeea28

Please sign in to comment.