<?php
namespace NrbnMailNotification\Subscriber;
use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Session\Session;
use Shopware\Core\Content\Mail\Service\MailService;
use Symfony\Contracts\Translation\TranslatorInterface;
class LoginSubscriber implements EventSubscriberInterface
{
/**
* @var CartService
*/
protected CartService $cartService;
/**
* @var MailService
*/
protected MailService $mailService;
/**
* @var EntityRepository
*/
protected EntityRepository $mailTemplate;
/**
* @var TranslatorInterface
*/
protected TranslatorInterface $translator;
/**
* @var Session
*/
protected Session $session;
/**
* @param CartService $cartService
* @param MailService $mailService
* @param EntityRepository $mailTemplate
* @param TranslatorInterface $translator
* @param Session $session
*/
public function __construct(
CartService $cartService,
MailService $mailService,
EntityRepository $mailTemplate,
TranslatorInterface $translator,
Session $session
)
{
$this->cartService = $cartService;
$this->mailService = $mailService;
$this->mailTemplate = $mailTemplate;
$this->translator = $translator;
$this->session = $session;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
CustomerLoginEvent::class => "checkCart"
];
}
/**
* @param CustomerLoginEvent $event
*/
public function checkCart(CustomerLoginEvent $event)
{
$customer = $event->getCustomer();
$cart = $this->cartService->getCart($event->getContextToken(), $event->getSalesChannelContext());
if($cart->getLineItems()->count()){
$this->session->getFlashBag()->set(
'warning',
$this->translator->trans('nrbnMailNotification.warning')
);
$this->sendMail($cart->getLineItems(), $customer, $event->getSalesChannelContext());
}
}
private function sendMail(
LineItemCollection $lineItems,
CustomerEntity $customer,
SalesChannelContext $context
)
{
$mailTemplate = $this->getNotificationFormMailTemplate(
$context->getContext()
);
$data = new ParameterBag();
$data->set(
'recipients',
[
$customer->getEmail() => $customer->getFirstName() . ' ' . $customer->getLastName()
]
);
$data->set('senderName', $mailTemplate->getSenderName());
$data->set('salesChannelId', $context->getSalesChannel()->getId());
$data->set('contentHtml', $mailTemplate->getContentHtml());
$data->set('contentPlain', $mailTemplate->getContentPlain());
$data->set('subject', $mailTemplate->getSubject());
$this->mailService->send(
$data->all(),
$context->getContext(),
[
"customer" => $customer,
"lineItems" => $lineItems
]
);
}
private function getNotificationFormMailTemplate(Context $context): MailTemplateEntity
{
return $this->mailTemplate->search(
(new Criteria())->addFilter(new EqualsFilter('translations.subject', 'Hinweis auf Produkte im Warenkorb')),
$context
)->first();
}
}