custom/plugins/WabsPIM/src/Subscriber/EntityEventSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace WabsPIM\Subscriber;
  3. use Shopware\Core\Content\Product\ProductEntity;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Event\EventData\MailRecipientStruct;
  9. use Shopware\Core\System\CustomField\CustomFieldEntity;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  12. use WabsPIM\Core\Content\PIM\Event\ProductDraftStateChangedEvent;
  13. class EntityEventSubscriber implements EventSubscriberInterface
  14. {
  15.     protected EntityRepositoryInterface $productRepository;
  16.     protected EntityRepositoryInterface $customFieldRepository;
  17.     protected EventDispatcherInterface $eventDispatcher;
  18.     public function __construct(
  19.         EntityRepositoryInterface $productRepository,
  20.         EntityRepositoryInterface $customFieldRepository,
  21.         EventDispatcherInterface $eventDispatcher
  22.     ) {
  23.         $this->productRepository $productRepository;
  24.         $this->customFieldRepository $customFieldRepository;
  25.         $this->eventDispatcher $eventDispatcher;
  26.     }
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             'wabs_product_draft.written' => ['onProductDraftWritten'1000],
  31.         ];
  32.     }
  33.     public function onProductDraftWrittenEntityWrittenEvent $event) {
  34.         $results $event->getWriteResults();
  35.         foreach( $results as $result ) {
  36.             if( $result->hasPayload('state') ) {
  37.                 $productId $result->getProperty('productId');
  38.                 $productVersionId $result->getProperty('productVersionId');
  39.                 $criteria = new Criteria([$productId]);
  40.                 $criteria->addAssociation('productDraft');
  41.                 $criteria->addAssociation('productDraft.customer');
  42.                 $criteria->addAssociation('visibilities');
  43.                 $versionContext $event->getContext()->createWithVersionId($productVersionId);
  44.                 /** @var ProductEntity $product */
  45.                 $product $this->productRepository->search($criteria$versionContext)->getEntities()->first();
  46.                 $visibility $product->getVisibilities()->first();
  47.                 $productCustomFields $product->getCustomFields();
  48.                 $criteria = new Criteria();
  49.                 $criteria->addFilter(new EqualsFilter('name''igm_mbs_verantwortlicher_gruppe'));
  50.                 /** @var CustomFieldEntity $customField */
  51.                 $customField $this->customFieldRepository->search($criteria$event->getContext())->getEntities()->first();
  52.                 $customFieldConfig $customField->getConfig();
  53.                 $verantwortlicherMailAddress null;
  54.                 foreach( $customFieldConfig['options'] as $customFieldOption ) {
  55.                     if( $customFieldOption['value'] == $productCustomFields['igm_mbs_verantwortlicher_gruppe']) {
  56.                         $verantwortlicherMailAddress $customFieldOption['label']['de-DE'];
  57.                     }
  58.                 }
  59.                 if( $verantwortlicherMailAddress ) {
  60.                     $event = new ProductDraftStateChangedEvent(
  61.                         $versionContext,
  62.                         $product,
  63.                         $product->getExtension('productDraft')->getCustomer(),
  64.                         $visibility->getSalesChannelId(),
  65.                         new MailRecipientStruct([
  66.                             $verantwortlicherMailAddress => $verantwortlicherMailAddress
  67.                         ])
  68.                     );
  69.                     $this->eventDispatcher->dispatch($event);
  70.                 }
  71.             }
  72.         }
  73.     }
  74. }