vendor/crehler/mojebambino-engine/src/Subscriber/ImportProductAddSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. declare (strict_types=1);
  3. namespace Crehler\MojeBambinoEngine\Subscriber;
  4. use Crehler\MojeBambinoEngine\Service\CrehlerContextService;
  5. use Shopware\Core\Content\Category\Event\NavigationLoadedEvent;
  6. use Shopware\Core\Content\Product\Aggregate\ProductConfiguratorSetting\ProductConfiguratorSettingEntity;
  7. use Shopware\Core\Content\Product\ProductEntity;
  8. use Shopware\Core\Content\Product\ProductEvents;
  9. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionEntity;
  10. use Shopware\Core\Framework\Api\Sync\SyncBehavior;
  11. use Shopware\Core\Framework\Api\Sync\SyncOperation;
  12. use Shopware\Core\Framework\Api\Sync\SyncServiceInterface;
  13. use Shopware\Core\Framework\Context;
  14. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  19. use Shopware\Core\Framework\Uuid\Uuid;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. class ImportProductAddSubscriber implements EventSubscriberInterface
  22. {
  23.     private EntityRepository $productRepository;
  24.     private EntityRepository $configuratorSettingsRepository;
  25.     private CrehlerContextService $crehlerContext;
  26.     public function __construct(EntityRepository $productRepositoryEntityRepository $configuratorRepositoryCrehlerContextService $crehlerContext)
  27.     {
  28.         $this->productRepository $productRepository;
  29.         $this->configuratorSettingsRepository $configuratorRepository;
  30.         $this->crehlerContext $crehlerContext;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             ProductEvents::PRODUCT_WRITTEN_EVENT => 'onProductWritten'
  36.         ];
  37.     }
  38.     public function onProductWritten(EntityWrittenEvent $event)
  39.     {
  40.         try {
  41.             if ($this->crehlerContext->get(CrehlerContextService::CREHLER_PRODUCT_IMPORT_COMMANDfalse)) return;
  42.             foreach ($event->getWriteResults() as $result) {
  43.                 if (empty($result->getPayload()) || !isset($result->getPayload()['id'])) {
  44.                     continue;
  45.                 }
  46.                 $criteria = new Criteria([$result->getPayload()['id']]);
  47.                 $criteria->addAssociation('options');
  48.                 /** @var ProductEntity $productEntity */
  49.                 $productEntity $this->productRepository->search($criteriaContext::createDefaultContext())->first();
  50.                 if (!$productEntity || !$productEntity->getParentId() || !isset($result->getPayload()['parentId'])) {
  51.                     continue;
  52.                 }
  53.                 if (!$productEntity->getOptionIds() || empty($productEntity->getOptionIds())) {
  54.                     if (isset($result->getPayload()['options']) && count($result->getPayload()['options']) > 0) {
  55.                         continue;
  56.                     }
  57.                     $this->addOptionsIds($productEntity);
  58.                 }
  59.             }
  60.         } catch (\Throwable $exception) {
  61.             dd($exception);
  62.         }
  63.     }
  64.     private function addOptionsIds(ProductEntity $productEntity): void
  65.     {
  66.         $optionsIds = [];
  67.         foreach ($productEntity->getOptions() as $option) {
  68.             $optionsIds[] = ['id' => $option->getId()];
  69.         }
  70.         if (empty($optionsIds)) {
  71.             return;
  72.         }
  73.         $updateData = [
  74.             [
  75.                 'id' => $productEntity->getId(),
  76.                 'options' => $optionsIds
  77.             ]
  78.         ];
  79.         $this->productRepository->upsert($updateDataContext::createDefaultContext());
  80.         $this->addConfiguratorSettings($productEntity);
  81.     }
  82.     private function addConfiguratorSettings(ProductEntity $productEntity)
  83.     {
  84.         $groupOptions = [];
  85.         $criteria = (new Criteria())->addFilter(
  86.             new EqualsFilter('productId'$productEntity->getParentId())
  87.         );
  88.         $criteria
  89.             ->addAssociation('option.group');
  90.         $settings $this->configuratorSettingsRepository
  91.             ->search($criteriaContext::createDefaultContext())
  92.             ->getEntities();
  93.         $currentConfiguratorOptions = [];
  94.         /** @var ProductConfiguratorSettingEntity $configItem */
  95.         foreach ($settings as $configItem) {
  96.             $currentConfiguratorOptions[] = $configItem->getOptionId();
  97.         }
  98.         /** @var PropertyGroupOptionEntity $option */
  99.         foreach ($productEntity->getOptions() as $key => $option) {
  100.             $optionsIds[] = $key;
  101.             if (in_array($key$currentConfiguratorOptions)) {
  102.                 continue;
  103.             }
  104.             $groupOptions[] =
  105.                 [
  106.                     'optionId' => $key,
  107.                     'productId' => $productEntity->getParentId()
  108.                 ];
  109.         }
  110.         if (!empty($groupOptions)) {
  111.             $this->configuratorSettingsRepository->upsert($groupOptionsContext::createDefaultContext());
  112.         }
  113.     }
  114. }