custom/plugins/ImnxxNotifyWhenAvailable/src/Subscriber/NotifySubscriber.php line 49

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ImnxxNotifyWhenAvailable\Subscriber;
  3. use ImnxxNwbProductApi\Core\Content\ProductsUpdated\Event\ProductsUpdatedEvent;
  4. use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\AndFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use ImnxxNotifyWhenAvailable\Service\HelperService;
  13. use ImnxxNotifyWhenAvailable\Core\Content\Notify\NotifyEntity;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. class NotifySubscriber implements EventSubscriberInterface {
  16.     private EntityRepository $notifyRepository;
  17.     private EntityRepository $customerRepository;
  18.     private EntityRepository $productRepository;
  19.     private SeoUrlPlaceholderHandlerInterface $seoUrlHandler;
  20.     private HelperService $helperService;
  21.     private SystemConfigService $systemConfigService;
  22.     public function __construct(
  23.         EntityRepository $notifyRepository,
  24.         EntityRepository $customerRepository,
  25.         EntityRepository $productRepository,
  26.         HelperService $helperService,
  27.         SeoUrlPlaceholderHandlerInterface $seoUrlHandler,
  28.         SystemConfigService $systemConfigService,
  29.     ) {
  30.         $this->notifyRepository $notifyRepository;
  31.         $this->customerRepository $customerRepository;
  32.         $this->productRepository $productRepository;
  33.         $this->helperService $helperService;
  34.         $this->seoUrlHandler $seoUrlHandler;
  35.         $this->systemConfigService $systemConfigService;
  36.     }
  37.     public static function getSubscribedEvents(): array {
  38.         return [
  39.             ProductsUpdatedEvent::class => 'onProductsUpdated',
  40.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  41.             AccountLoginPageLoadedEvent::class => 'onAccountLoginPageLoaded',
  42.         ];
  43.     }
  44.     public function onProductPageLoaded(ProductPageLoadedEvent $event) {
  45.         if ($event->getSalesChannelContext()->getCustomer() != null) {
  46.             $criteria = new Criteria();
  47.             $criteria->addFilter(
  48.                 new AndFilter([
  49.                     new EqualsFilter(
  50.                         'customerId',
  51.                         $event
  52.                             ->getSalesChannelContext()
  53.                             ->getCustomer()
  54.                             ->getId(),
  55.                     ),
  56.                     new EqualsFilter(
  57.                         'productId',
  58.                         $event
  59.                             ->getPage()
  60.                             ->getProduct()
  61.                             ->getId(),
  62.                     ),
  63.                 ]),
  64.             );
  65.             $savedNotifies $this->notifyRepository->search($criteria$event->getContext())->count();
  66.             if ($savedNotifies 0) {
  67.                 $event->getPage()->assign(['notifyWhenAvailable' => true]);
  68.             }
  69.         }
  70.     }
  71.     public function onAccountLoginPageLoaded(AccountLoginPageLoadedEvent $event) {
  72.         if (isset($_GET['notifyRedirect']) && $_GET['notifyRedirect'] != '') {
  73.             $event->getPage()->assign(['notifyRedirect' => $_GET['notifyRedirect']]);
  74.         }
  75.     }
  76.     /**
  77.      * @throws \Exception
  78.      */
  79.     public function onProductsUpdated(ProductsUpdatedEvent $event) {
  80.         $shopName $event
  81.             ->getSalesChannelContext()
  82.             ->getSalesChannel()
  83.             ->getCustomFields()['shop_url'];
  84.         $mailTemplateId $this->systemConfigService->get('ImnxxNotifyWhenAvailable.config.mailTemplateId');
  85.         if ($mailTemplateId == null) {
  86.             throw new \Exception(
  87.                 'mailTemplateId is null | Please see if the mailTemplateId is set in Plugin-config of ImnxxNotifyWhenAvailable',
  88.             );
  89.         }
  90.         $products = [];
  91.         $notifyIds = [];
  92.         $savedNotifies $this->notifyRepository->search(new Criteria(), $event->getContext())->getElements();
  93.         foreach ($savedNotifies as $notify) {
  94.             $products[$notify->getProductId()][] = $notify->getCustomerId();
  95.             $notifyIds[$notify->getProductId()][] = $notify->getId();
  96.         }
  97.         foreach ($products as $productId => $product) {
  98.             $swProduct $this->productRepository
  99.                 ->search(
  100.                     (new Criteria())->addFilter(new EqualsFilter('id'$productId)),
  101.                     $event->getContext(),
  102.                 )
  103.                 ->first();
  104.             if ($swProduct != null) {
  105.                 if ($swProduct->getCustomFields()['custom_product_attributs_lieferstatus'] == 'Lieferbar') {
  106.                     foreach ($product as $customerId) {
  107.                         $mailTemplate $this->helperService->getMailTemplate(
  108.                             $mailTemplateId,
  109.                             $event->getContext(),
  110.                         );
  111.                         $mailSubject $this->helperService->mailTemplateVariablePlacer(
  112.                             $mailTemplate->getSubject(),
  113.                             '{{product.name}}',
  114.                             $swProduct->getName(),
  115.                         );
  116.                         $mailContent $mailTemplate->getContentHtml();
  117.                         $customer $this->helperService->getCustomer($customerId$event->getContext());
  118.                         $customerName $customer->getFirstName() . ' ' $customer->getLastName();
  119.                         $customerMail $customer->getCustomFields()['nwb_email'];
  120.                         $mailContent $this->helperService->mailTemplateVariablePlacer(
  121.                             $mailContent,
  122.                             '{{customer.name}}',
  123.                             $customerName,
  124.                         );
  125.                         $mailContent $this->helperService->mailTemplateVariablePlacer(
  126.                             $mailContent,
  127.                             '{{product.name}}',
  128.                             $swProduct->getName(),
  129.                         );
  130.                         $parameter = ['productId' => $swProduct->getId()];
  131.                         $raw $this->seoUrlHandler->generate('frontend.detail.page'$parameter);
  132.                         $url $this->seoUrlHandler->replace(
  133.                             $raw,
  134.                             'https://' $shopName,
  135.                             $event->getSalesChannelContext(),
  136.                         );
  137.                         $mailContent $this->helperService->mailTemplateVariablePlacer(
  138.                             $mailContent,
  139.                             '{{product.link}}',
  140.                             $url,
  141.                         );
  142.                         $this->helperService->sendMail(
  143.                             [$customerMail => $customerName],
  144.                             $event
  145.                                 ->getSalesChannelContext()
  146.                                 ->getSalesChannel()
  147.                                 ->getName(),
  148.                             $swProduct->getName() . ' ist jetzt im NWB Shop verfügbar',
  149.                             $mailContent,
  150.                             $event->getSalesChannelContext(),
  151.                             $product,
  152.                         );
  153.                     }
  154.                     foreach ($notifyIds[$productId] as $notifyId) {
  155.                         $this->notifyRepository->delete([['id' => $notifyId]], $event->getContext());
  156.                     }
  157.                 }
  158.             }
  159.         }
  160.         die();
  161.     }
  162. }