diff --git a/ImportExport/Processor/AsyncProcessor.php b/ImportExport/Processor/AsyncProcessor.php index c7b03c3f..1c73739d 100644 --- a/ImportExport/Processor/AsyncProcessor.php +++ b/ImportExport/Processor/AsyncProcessor.php @@ -22,8 +22,15 @@ 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'])) { @@ -31,8 +38,8 @@ private function updateVariants(array &$item) } $parent = $item['parent']; + $this->variants[$parent][$sku] = ['parent' => $parent, 'variant' => $sku]; - unset($this->variants[$sku]['']); } public function initialize() @@ -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 = []; } } diff --git a/ImportExport/Processor/ProductVariantProcessor.php b/ImportExport/Processor/ProductVariantProcessor.php index f1191df2..cb3b173d 100644 --- a/ImportExport/Processor/ProductVariantProcessor.php +++ b/ImportExport/Processor/ProductVariantProcessor.php @@ -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); @@ -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; @@ -91,6 +96,8 @@ function ($variantSku) { $parentProduct->removeVariantLink($variantLink); $variantProduct->setStatus(Product::STATUS_DISABLED); $objectManager->remove($variantLink); + $context->incrementDeleteCount(); + continue; } @@ -98,6 +105,8 @@ function ($variantSku) { $parentProduct->removeVariantLink($variantLink); $variantProduct->setStatus(Product::STATUS_DISABLED); $objectManager->remove($variantLink); + $context->incrementDeleteCount(); + continue; } @@ -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); @@ -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(); diff --git a/Resources/translations/validators.en.yml b/Resources/translations/validators.en.yml index a29e218d..18257ab9 100644 --- a/Resources/translations/validators.en.yml +++ b/Resources/translations/validators.en.yml @@ -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.'