Skip to content

Commit

Permalink
Multiorg product search fix (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
dxops committed Feb 14, 2023
1 parent 863098e commit b9e144f
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 28 deletions.
2 changes: 1 addition & 1 deletion ImportExport/DataConverter/AttributeDataConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions ImportExport/DataConverter/ProductDataConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ protected function getPrimaryUnitPrecision(array $importedRecord): array
'unit' => ['code' => $unit],
'precision' => $precision,
'sell' => true,
'product' => ['sku' => $importedRecord['sku']],
];
}

Expand Down
10 changes: 10 additions & 0 deletions ImportExport/EventListener/OwnerStrategyEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
64 changes: 50 additions & 14 deletions ImportExport/Strategy/ProductImageImportStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'));
}
}
89 changes: 76 additions & 13 deletions ImportExport/Strategy/ProductImportStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand Down
1 change: 1 addition & 0 deletions Resources/config/importexport.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

0 comments on commit b9e144f

Please sign in to comment.