custom/plugins/NrbnMailNotification/src/Subscriber/LoginSubscriber.php line 83

Open in your IDE?
  1. <?php
  2. namespace NrbnMailNotification\Subscriber;
  3. use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  6. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  7. use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\ParameterBag;
  15. use Symfony\Component\HttpFoundation\Session\Session;
  16. use Shopware\Core\Content\Mail\Service\MailService;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. class LoginSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var CartService 
  22.      */
  23.     protected CartService $cartService;
  24.     /**
  25.      * @var MailService
  26.      */
  27.     protected MailService $mailService;
  28.     /**
  29.      * @var EntityRepository
  30.      */
  31.     protected EntityRepository $mailTemplate;
  32.     /**
  33.      * @var TranslatorInterface
  34.      */
  35.     protected TranslatorInterface $translator;
  36.     /**
  37.      * @var Session
  38.      */
  39.     protected Session $session;
  40.     /**
  41.      * @param CartService           $cartService
  42.      * @param MailService           $mailService
  43.      * @param EntityRepository      $mailTemplate
  44.      * @param TranslatorInterface   $translator
  45.      * @param Session               $session
  46.      */
  47.     public function __construct(
  48.         CartService          $cartService,
  49.         MailService          $mailService,
  50.         EntityRepository     $mailTemplate,
  51.         TranslatorInterface  $translator,
  52.         Session              $session
  53.     )
  54.     {
  55.         $this->cartService  $cartService;
  56.         $this->mailService  $mailService;
  57.         $this->mailTemplate $mailTemplate;
  58.         $this->translator   $translator;
  59.         $this->session      $session;
  60.     }
  61.     /**
  62.      * @return string[]
  63.      */
  64.     public static function getSubscribedEvents(): array
  65.     {
  66.         return [
  67.             CustomerLoginEvent::class => "checkCart"
  68.         ];
  69.     }
  70.     /**
  71.      * @param CustomerLoginEvent $event
  72.      */
  73.     public function checkCart(CustomerLoginEvent $event)
  74.     {
  75.         $customer $event->getCustomer();
  76.         $cart $this->cartService->getCart($event->getContextToken(), $event->getSalesChannelContext());
  77.         if($cart->getLineItems()->count()){
  78.             $this->session->getFlashBag()->set(
  79.                 'warning',
  80.                 $this->translator->trans('nrbnMailNotification.warning')
  81.             );
  82.             $this->sendMail($cart->getLineItems(), $customer$event->getSalesChannelContext());
  83.         }
  84.     }
  85.     private function sendMail(
  86.         LineItemCollection $lineItems,
  87.         CustomerEntity $customer,
  88.         SalesChannelContext $context
  89.     )
  90.     {
  91.         $mailTemplate $this->getNotificationFormMailTemplate(
  92.             $context->getContext()
  93.         );
  94.         $data = new ParameterBag();
  95.         $data->set(
  96.             'recipients',
  97.             [
  98.                 $customer->getEmail() => $customer->getFirstName() . ' ' $customer->getLastName()
  99.             ]
  100.         );
  101.         $data->set('senderName'$mailTemplate->getSenderName());
  102.         $data->set('salesChannelId'$context->getSalesChannel()->getId());
  103.         $data->set('contentHtml'$mailTemplate->getContentHtml());
  104.         $data->set('contentPlain'$mailTemplate->getContentPlain());
  105.         $data->set('subject'$mailTemplate->getSubject());
  106.         $this->mailService->send(
  107.             $data->all(),
  108.             $context->getContext(),
  109.             [
  110.                 "customer"  => $customer,
  111.                 "lineItems" => $lineItems
  112.             ]
  113.         );
  114.     }
  115.     private function getNotificationFormMailTemplate(Context $context): MailTemplateEntity
  116.     {
  117.         return $this->mailTemplate->search(
  118.             (new Criteria())->addFilter(new EqualsFilter('translations.subject''Hinweis auf Produkte im Warenkorb')),
  119.             $context
  120.         )->first();
  121.     }
  122. }