vendor/shopware/core/Content/ImportExport/Event/Subscriber/ProductVariantsSubscriber.php line 61

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\ImportExport\Event\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Content\ImportExport\Event\ImportExportAfterImportRecordEvent;
  5. use Shopware\Core\Content\ImportExport\Exception\ProcessingException;
  6. use Shopware\Core\Content\Product\Aggregate\ProductConfiguratorSetting\ProductConfiguratorSettingDefinition;
  7. use Shopware\Core\Content\Product\ProductDefinition;
  8. use Shopware\Core\Framework\Api\Sync\SyncBehavior;
  9. use Shopware\Core\Framework\Api\Sync\SyncOperation;
  10. use Shopware\Core\Framework\Api\Sync\SyncServiceInterface;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Shopware\Core\Framework\Feature;
  17. use Shopware\Core\Framework\Uuid\Uuid;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class ProductVariantsSubscriber implements EventSubscriberInterface
  20. {
  21.     private SyncServiceInterface $syncService;
  22.     private Connection $connection;
  23.     private EntityRepositoryInterface $groupRepository;
  24.     private EntityRepositoryInterface $optionRepository;
  25.     private array $groupIdCache = [];
  26.     private array $optionIdCache = [];
  27.     /**
  28.      * @internal
  29.      */
  30.     public function __construct(
  31.         SyncServiceInterface $syncService,
  32.         Connection $connection,
  33.         EntityRepositoryInterface $groupRepository,
  34.         EntityRepositoryInterface $optionRepository
  35.     ) {
  36.         $this->syncService $syncService;
  37.         $this->connection $connection;
  38.         $this->groupRepository $groupRepository;
  39.         $this->optionRepository $optionRepository;
  40.     }
  41.     /**
  42.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  43.      */
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             ImportExportAfterImportRecordEvent::class => 'onAfterImportRecord',
  48.         ];
  49.     }
  50.     public function onAfterImportRecord(ImportExportAfterImportRecordEvent $event): void
  51.     {
  52.         $row $event->getRow();
  53.         $entityName $event->getConfig()->get('sourceEntity');
  54.         $entityWrittenEvents $event->getResult()->getEvents();
  55.         if ($entityName !== ProductDefinition::ENTITY_NAME || empty($row['variants']) || !$entityWrittenEvents) {
  56.             return;
  57.         }
  58.         $variants $this->parseVariantString($row['variants']);
  59.         $entityWrittenEvent $entityWrittenEvents->filter(function ($event) {
  60.             return $event instanceof EntityWrittenEvent && $event->getEntityName() === ProductDefinition::ENTITY_NAME;
  61.         })->first();
  62.         if (!$entityWrittenEvent instanceof EntityWrittenEvent) {
  63.             return;
  64.         }
  65.         $writeResults $entityWrittenEvent->getWriteResults();
  66.         if (empty($writeResults)) {
  67.             return;
  68.         }
  69.         $parentId $writeResults[0]->getPrimaryKey();
  70.         $parentPayload $writeResults[0]->getPayload();
  71.         if (!\is_string($parentId)) {
  72.             return;
  73.         }
  74.         $payload $this->getCombinationsPayload($variants$parentId$parentPayload['productNumber']);
  75.         $variantIds array_column($payload'id');
  76.         $this->connection->executeStatement(
  77.             'DELETE FROM `product_option` WHERE `product_id` IN (:ids);',
  78.             ['ids' => Uuid::fromHexToBytesList($variantIds)],
  79.             ['ids' => Connection::PARAM_STR_ARRAY]
  80.         );
  81.         $configuratorSettingPayload $this->getProductConfiguratorSettingPayload($payload$parentId);
  82.         $this->connection->executeStatement(
  83.             'DELETE FROM `product_configurator_setting` WHERE `product_id` = :parentId AND `id` NOT IN (:ids);',
  84.             [
  85.                 'parentId' => Uuid::fromHexToBytes($parentId),
  86.                 'ids' => Uuid::fromHexToBytesList(array_column($configuratorSettingPayload'id')),
  87.             ],
  88.             ['ids' => Connection::PARAM_STR_ARRAY]
  89.         );
  90.         if (Feature::isActive('FEATURE_NEXT_15815')) {
  91.             $behavior = new SyncBehavior();
  92.         } else {
  93.             $behavior = new SyncBehavior(truetrue);
  94.         }
  95.         $result $this->syncService->sync([
  96.             new SyncOperation(
  97.                 'write',
  98.                 ProductDefinition::ENTITY_NAME,
  99.                 SyncOperation::ACTION_UPSERT,
  100.                 $payload
  101.             ),
  102.             new SyncOperation(
  103.                 'write',
  104.                 ProductConfiguratorSettingDefinition::ENTITY_NAME,
  105.                 SyncOperation::ACTION_UPSERT,
  106.                 $configuratorSettingPayload
  107.             ),
  108.         ], Context::createDefaultContext(), $behavior);
  109.         if (Feature::isActive('FEATURE_NEXT_15815')) {
  110.             // @internal (flag:FEATURE_NEXT_15815) - remove code below, "isSuccess" function will be removed, simply return because sync service would throw an exception in error case
  111.             return;
  112.         }
  113.         if (!$result->isSuccess()) {
  114.             $operation $result->get('write');
  115.             throw new ProcessingException(sprintf(
  116.                 'Failed writing variants for %s with errors: %s',
  117.                 $parentPayload['productNumber'],
  118.                 $operation json_encode(array_column($operation->getResult(), 'errors')) : ''
  119.             ));
  120.         }
  121.     }
  122.     /**
  123.      * convert "size: m, l, xl" to ["size|m", "size|l", "size|xl"]
  124.      */
  125.     private function parseVariantString(string $variantsString): array
  126.     {
  127.         $result = [];
  128.         $groups explode('|'$variantsString);
  129.         foreach ($groups as $group) {
  130.             $groupOptions explode(':'$group);
  131.             if (\count($groupOptions) !== 2) {
  132.                 $this->throwExceptionFailedParsingVariants($variantsString);
  133.             }
  134.             $groupName trim($groupOptions[0]);
  135.             $options array_filter(array_map('trim'explode(','$groupOptions[1])));
  136.             if (empty($groupName) || empty($options)) {
  137.                 $this->throwExceptionFailedParsingVariants($variantsString);
  138.             }
  139.             $options array_map(function ($option) use ($groupName) {
  140.                 return sprintf('%s|%s'$groupName$option);
  141.             }, $options);
  142.             $result[] = $options;
  143.         }
  144.         return $result;
  145.     }
  146.     private function throwExceptionFailedParsingVariants(string $variantsString): void
  147.     {
  148.         throw new ProcessingException(sprintf(
  149.             'Failed parsing variants from string "%s", valid format is: "size: L, XL, | color: Green, White"',
  150.             $variantsString
  151.         ));
  152.     }
  153.     private function getCombinationsPayload(array $variantsstring $parentIdstring $productNumber): array
  154.     {
  155.         $combinations $this->getCombinations($variants);
  156.         $payload = [];
  157.         foreach ($combinations as $key => $combination) {
  158.             $options = [];
  159.             foreach ($combination as $option) {
  160.                 list($group$option) = explode('|'$option);
  161.                 $optionId $this->getOptionId($group$option);
  162.                 $groupId $this->getGroupId($group);
  163.                 $options[] = [
  164.                     'id' => $optionId,
  165.                     'name' => $option,
  166.                     'group' => [
  167.                         'id' => $groupId,
  168.                         'name' => $group,
  169.                     ],
  170.                 ];
  171.             }
  172.             $variantId Uuid::fromStringToHex(sprintf('%s.%s'$parentId$key));
  173.             $variantProductNumber sprintf('%s.%s'$productNumber$key);
  174.             $payload[] = [
  175.                 'id' => $variantId,
  176.                 'parentId' => $parentId,
  177.                 'productNumber' => $variantProductNumber,
  178.                 'stock' => 0,
  179.                 'options' => $options,
  180.             ];
  181.         }
  182.         return $payload;
  183.     }
  184.     /**
  185.      * convert [["size|m", "size|l"], ["color|blue", "color|red"]]
  186.      * to [["size|m", "color|blue"], ["size|l", "color|blue"], ["size|m", "color|red"], ["size|l", "color|red"]]
  187.      */
  188.     private function getCombinations(array $variantsint $currentIndex 0): array
  189.     {
  190.         if (!isset($variants[$currentIndex])) {
  191.             return [];
  192.         }
  193.         if ($currentIndex === \count($variants) - 1) {
  194.             return $variants[$currentIndex];
  195.         }
  196.         // get combinations from subsequent arrays
  197.         $combinations $this->getCombinations($variants$currentIndex 1);
  198.         $result = [];
  199.         // concat each array from tmp with each element from $variants[$i]
  200.         foreach ($variants[$currentIndex] as $variant) {
  201.             foreach ($combinations as $combination) {
  202.                 $result[] = \is_array($combination) ? array_merge([$variant], $combination) : [$variant$combination];
  203.             }
  204.         }
  205.         return $result;
  206.     }
  207.     private function getProductConfiguratorSettingPayload(array $variantsPayloadstring $parentId): array
  208.     {
  209.         $options array_merge(...array_column($variantsPayload'options'));
  210.         $optionIds array_unique(array_column($options'id'));
  211.         $payload = [];
  212.         foreach ($optionIds as $optionId) {
  213.             $payload[] = [
  214.                 'id' => Uuid::fromStringToHex(sprintf('%s_configurator'$optionId)),
  215.                 'optionId' => $optionId,
  216.                 'productId' => $parentId,
  217.             ];
  218.         }
  219.         return $payload;
  220.     }
  221.     private function getGroupId(string $groupName): string
  222.     {
  223.         $groupId Uuid::fromStringToHex($groupName);
  224.         if (isset($this->groupIdCache[$groupId])) {
  225.             return $this->groupIdCache[$groupId];
  226.         }
  227.         $criteria = new Criteria();
  228.         $criteria->addFilter(new EqualsFilter('name'$groupName));
  229.         $group $this->groupRepository->search($criteriaContext::createDefaultContext())->first();
  230.         if ($group !== null) {
  231.             $this->groupIdCache[$groupId] = $group->getId();
  232.             return $group->getId();
  233.         }
  234.         $this->groupIdCache[$groupId] = $groupId;
  235.         return $groupId;
  236.     }
  237.     private function getOptionId(string $groupNamestring $optionName): string
  238.     {
  239.         $optionId Uuid::fromStringToHex(sprintf('%s.%s'$groupName$optionName));
  240.         if (isset($this->optionIdCache[$optionId])) {
  241.             return $this->optionIdCache[$optionId];
  242.         }
  243.         $criteria = new Criteria();
  244.         $criteria->addFilter(new EqualsFilter('name'$optionName));
  245.         $criteria->addFilter(new EqualsFilter('group.name'$groupName));
  246.         $option $this->optionRepository->search($criteriaContext::createDefaultContext())->first();
  247.         if ($option !== null) {
  248.             $this->optionIdCache[$optionId] = $option->getId();
  249.             return $option->getId();
  250.         }
  251.         $this->optionIdCache[$optionId] = $optionId;
  252.         return $optionId;
  253.     }
  254. }