diff --git a/ImportExport/DataConverter/AttributeDataConverter.php b/ImportExport/DataConverter/AttributeDataConverter.php index 151f05f8..cf20d12b 100644 --- a/ImportExport/DataConverter/AttributeDataConverter.php +++ b/ImportExport/DataConverter/AttributeDataConverter.php @@ -114,7 +114,7 @@ private function setEnumOptions(array &$importedRecord): void foreach ($importedRecord['options'] as $key => &$option) { $optionKey = sprintf('enum.enum_options.%d.priority', $key); - $importedRecord[$optionKey] = $option['sort_order']; + $importedRecord[$optionKey] = $option['sort_order'] ?? $key; $optionKey = sprintf('enum.enum_options.%d.id', $key); $importedRecord[$optionKey] = Generator::generateLabel($option['code']); $optionKey = sprintf('enum.enum_options.%d.label', $key); diff --git a/ImportExport/DataConverter/ProductDataConverter.php b/ImportExport/DataConverter/ProductDataConverter.php index 43256f4d..c786fbaa 100644 --- a/ImportExport/DataConverter/ProductDataConverter.php +++ b/ImportExport/DataConverter/ProductDataConverter.php @@ -541,6 +541,7 @@ protected function getPrimaryUnitPrecision(array $importedRecord): array 'unit' => ['code' => $unit], 'precision' => $precision, 'sell' => true, + 'product' => ['sku' => $importedRecord['sku']], ]; } diff --git a/ImportExport/EventListener/OwnerStrategyEventListener.php b/ImportExport/EventListener/OwnerStrategyEventListener.php index 42719a35..d3648f5f 100644 --- a/ImportExport/EventListener/OwnerStrategyEventListener.php +++ b/ImportExport/EventListener/OwnerStrategyEventListener.php @@ -35,6 +35,16 @@ public function onProcessBefore(StrategyEvent $event) $this->defaultOwnerHelper->populateChannelOwner($event->getEntity(), $channel); } + public function onProcessAfter(StrategyEvent $event) + { + $channel = $this->getChannel($event->getContext()); + if (!$channel) { + return; + } + + $this->defaultOwnerHelper->populateChannelOwner($event->getEntity(), $channel); + } + protected function getChannel(ContextInterface $context) { if (!$this->channel && $context->getOption('channel')) { diff --git a/ImportExport/Strategy/ProductImageImportStrategy.php b/ImportExport/Strategy/ProductImageImportStrategy.php index 903d1771..aa04d581 100644 --- a/ImportExport/Strategy/ProductImageImportStrategy.php +++ b/ImportExport/Strategy/ProductImageImportStrategy.php @@ -4,6 +4,7 @@ use Oro\Bundle\BatchBundle\Item\Support\ClosableInterface; use Oro\Bundle\ImportExportBundle\Strategy\Import\ConfigurableAddOrReplaceStrategy; +use Oro\Bundle\IntegrationBundle\Entity\Channel; use Oro\Bundle\ProductBundle\Entity\Product; use Oro\Bundle\ProductBundle\Entity\ProductImage; @@ -83,31 +84,66 @@ protected function isFieldExcluded($entityName, $fieldName, $itemData = null) protected function findExistingEntity($entity, array $searchContext = []) { - if ($entity instanceof Product && array_key_exists($entity->getSku(), $this->existingProducts)) { - return $this->existingProducts[$entity->getSku()]; - } + if ($entity instanceof Product) { + if (array_key_exists($entity->getSku(), $this->existingProducts)) { + return $this->existingProducts[$entity->getSku()]; + } - $entity = parent::findExistingEntity($entity, $searchContext); + $entity = $this->doctrineHelper->getEntityRepository($entity)->findByCaseInsensitive( + [ + 'sku' => $entity->getSku(), + 'organization' => $entity->getOrganization() ?: $this->getChannel()->getOrganization(), + ] + ); + if (is_array($entity)) { + $entity = array_shift($entity); + if ($entity instanceof Product) { + $this->existingProducts[$entity->getSku()] = $entity; + + return $entity; + } + + return null; + } - if ($entity instanceof Product) { - $this->existingProducts[$entity->getSku()] = $entity; + return $entity; } - return $entity; + return parent::findExistingEntity($entity, $searchContext); } protected function findExistingEntityByIdentityFields($entity, array $searchContext = []) { - if ($entity instanceof Product && array_key_exists($entity->getSku(), $this->existingProducts)) { - return $this->existingProducts[$entity->getSku()]; - } + if ($entity instanceof Product) { + if (array_key_exists($entity->getSku(), $this->existingProducts)) { + return $this->existingProducts[$entity->getSku()]; + } - $entity = parent::findExistingEntityByIdentityFields($entity, $searchContext); + $entity = $this->doctrineHelper->getEntityRepository($entity)->findByCaseInsensitive( + [ + 'sku' => $entity->getSku(), + 'organization' => $entity->getOrganization() ?: $this->getChannel()->getOrganization(), + ] + ); + if (is_array($entity)) { + $entity = array_shift($entity); + if ($entity instanceof Product) { + $this->existingProducts[$entity->getSku()] = $entity; + + return $entity; + } + + return null; + } - if ($entity instanceof Product) { - $this->existingProducts[$entity->getSku()] = $entity; + return $entity; } - return $entity; + return parent::findExistingEntityByIdentityFields($entity, $searchContext); + } + + private function getChannel() + { + return $this->doctrineHelper->getEntityReference(Channel::class, $this->context->getOption('channel')); } } diff --git a/ImportExport/Strategy/ProductImportStrategy.php b/ImportExport/Strategy/ProductImportStrategy.php index 56d6dc75..3ade6b17 100644 --- a/ImportExport/Strategy/ProductImportStrategy.php +++ b/ImportExport/Strategy/ProductImportStrategy.php @@ -6,6 +6,7 @@ use Oro\Bundle\EntityExtendBundle\Entity\AbstractEnumValue; use Oro\Bundle\EntityExtendBundle\Tools\ExtendHelper; use Oro\Bundle\ProductBundle\Entity\Product; +use Oro\Bundle\ProductBundle\Entity\ProductUnitPrecision; use Oro\Bundle\ProductBundle\ImportExport\Strategy\ProductStrategy; /** @@ -42,7 +43,7 @@ protected function beforeProcessEntity($entity) if ($entity->isConfigurable()) { /** @var Product $existingProduct */ $existingProduct = $this->findExistingEntity($entity); - if ($existingProduct) { + if ($existingProduct instanceof Product) { $entity->setStatus($existingProduct->getStatus()); } } @@ -94,17 +95,48 @@ protected function populateOwner(Product $entity) protected function findExistingEntity($entity, array $searchContext = []) { - if ($entity instanceof Product && array_key_exists($entity->getSku(), $this->existingProducts)) { - return $this->existingProducts[$entity->getSku()]; + if ($entity instanceof Product) { + if (array_key_exists($entity->getSku(), $this->existingProducts)) { + return $this->existingProducts[$entity->getSku()]; + } + + $entity = $this->doctrineHelper->getEntityRepository($entity)->findByCaseInsensitive( + [ + 'sku' => $entity->getSku(), + 'organization' => $entity->getOrganization() ?: $this->getChannel()->getOrganization(), + ] + ); + if (is_array($entity)) { + $entity = array_shift($entity); + if ($entity instanceof Product) { + $this->existingProducts[$entity->getSku()] = $entity; + + return $entity; + } + + return null; + } + + return $entity; } - $entity = $this->findExistingEntityTrait($entity, $searchContext); + if ($entity instanceof ProductUnitPrecision) { + /** @var Product $product */ + $product = $this->findExistingEntity($entity->getProduct()) ?? $entity->getProduct(); - if ($entity instanceof Product) { - $this->existingProducts[$entity->getSku()] = $entity; + /** @var ProductUnitPrecision $precision */ + foreach ($product->getUnitPrecisions() as $precision) { + if ($precision->getProductUnitCode() === $entity->getProductUnitCode()) { + $entity->getProduct()->setPrimaryUnitPrecision($precision); + + return $precision; + } + } + + return $product->getPrimaryUnitPrecision(); } - return $entity; + return $this->findExistingEntityTrait($entity, $searchContext); } public function getExistingEntity(object $entity, array $searchContext = []): ?object @@ -114,17 +146,48 @@ public function getExistingEntity(object $entity, array $searchContext = []): ?o protected function findExistingEntityByIdentityFields($entity, array $searchContext = []) { - if ($entity instanceof Product && array_key_exists($entity->getSku(), $this->existingProducts)) { - return $this->existingProducts[$entity->getSku()]; + if ($entity instanceof Product) { + if (array_key_exists($entity->getSku(), $this->existingProducts)) { + return $this->existingProducts[$entity->getSku()]; + } + + $entity = $this->doctrineHelper->getEntityRepository($entity)->findByCaseInsensitive( + [ + 'sku' => $entity->getSku(), + 'organization' => $entity->getOrganization() ?: $this->getChannel()->getOrganization(), + ] + ); + if (is_array($entity)) { + $entity = array_shift($entity); + if ($entity instanceof Product) { + $this->existingProducts[$entity->getSku()] = $entity; + + return $entity; + } + + return null; + } + + return $entity; } - $entity = $this->findExistingEntityByIdentityFieldsTrait($entity, $searchContext); + if ($entity instanceof ProductUnitPrecision) { + /** @var Product $product */ + $product = $this->findExistingEntity($entity->getProduct()) ?? $entity->getProduct(); - if ($entity instanceof Product) { - $this->existingProducts[$entity->getSku()] = $entity; + /** @var ProductUnitPrecision $precision */ + foreach ($product->getUnitPrecisions() as $precision) { + if ($precision->getProductUnitCode() === $entity->getProductUnitCode()) { + $entity->getProduct()->setPrimaryUnitPrecision($precision); + + return $precision; + } + } + + return $product->getPrimaryUnitPrecision(); } - return $entity; + return $this->findExistingEntityByIdentityFieldsTrait($entity, $searchContext); } /** diff --git a/Resources/config/importexport.yml b/Resources/config/importexport.yml index 398226c9..5a833163 100644 --- a/Resources/config/importexport.yml +++ b/Resources/config/importexport.yml @@ -442,5 +442,6 @@ services: - '@oro_entity.doctrine_helper' - '@oro_akeneo.importexport.strategy.default_owner_helper' tags: + - { name: kernel.event_listener, event: oro_importexport.strategy.process_after, method: onProcessAfter, priority: 256 } - { name: kernel.event_listener, event: oro_importexport.strategy.process_before, method: onProcessBefore, priority: 256 } - { name: doctrine.event_listener, event: onClear }