<?php declare(strict_types=1);
namespace ImnxxNotifyWhenAvailable\Subscriber;
use ImnxxNwbProductApi\Core\Content\ProductsUpdated\Event\ProductsUpdatedEvent;
use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\AndFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use ImnxxNotifyWhenAvailable\Service\HelperService;
use ImnxxNotifyWhenAvailable\Core\Content\Notify\NotifyEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class NotifySubscriber implements EventSubscriberInterface {
private EntityRepository $notifyRepository;
private EntityRepository $customerRepository;
private EntityRepository $productRepository;
private SeoUrlPlaceholderHandlerInterface $seoUrlHandler;
private HelperService $helperService;
private SystemConfigService $systemConfigService;
public function __construct(
EntityRepository $notifyRepository,
EntityRepository $customerRepository,
EntityRepository $productRepository,
HelperService $helperService,
SeoUrlPlaceholderHandlerInterface $seoUrlHandler,
SystemConfigService $systemConfigService,
) {
$this->notifyRepository = $notifyRepository;
$this->customerRepository = $customerRepository;
$this->productRepository = $productRepository;
$this->helperService = $helperService;
$this->seoUrlHandler = $seoUrlHandler;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array {
return [
ProductsUpdatedEvent::class => 'onProductsUpdated',
ProductPageLoadedEvent::class => 'onProductPageLoaded',
AccountLoginPageLoadedEvent::class => 'onAccountLoginPageLoaded',
];
}
public function onProductPageLoaded(ProductPageLoadedEvent $event) {
if ($event->getSalesChannelContext()->getCustomer() != null) {
$criteria = new Criteria();
$criteria->addFilter(
new AndFilter([
new EqualsFilter(
'customerId',
$event
->getSalesChannelContext()
->getCustomer()
->getId(),
),
new EqualsFilter(
'productId',
$event
->getPage()
->getProduct()
->getId(),
),
]),
);
$savedNotifies = $this->notifyRepository->search($criteria, $event->getContext())->count();
if ($savedNotifies > 0) {
$event->getPage()->assign(['notifyWhenAvailable' => true]);
}
}
}
public function onAccountLoginPageLoaded(AccountLoginPageLoadedEvent $event) {
if (isset($_GET['notifyRedirect']) && $_GET['notifyRedirect'] != '') {
$event->getPage()->assign(['notifyRedirect' => $_GET['notifyRedirect']]);
}
}
/**
* @throws \Exception
*/
public function onProductsUpdated(ProductsUpdatedEvent $event) {
$shopName = $event
->getSalesChannelContext()
->getSalesChannel()
->getCustomFields()['shop_url'];
$mailTemplateId = $this->systemConfigService->get('ImnxxNotifyWhenAvailable.config.mailTemplateId');
if ($mailTemplateId == null) {
throw new \Exception(
'mailTemplateId is null | Please see if the mailTemplateId is set in Plugin-config of ImnxxNotifyWhenAvailable',
);
}
$products = [];
$notifyIds = [];
$savedNotifies = $this->notifyRepository->search(new Criteria(), $event->getContext())->getElements();
foreach ($savedNotifies as $notify) {
$products[$notify->getProductId()][] = $notify->getCustomerId();
$notifyIds[$notify->getProductId()][] = $notify->getId();
}
foreach ($products as $productId => $product) {
$swProduct = $this->productRepository
->search(
(new Criteria())->addFilter(new EqualsFilter('id', $productId)),
$event->getContext(),
)
->first();
if ($swProduct != null) {
if ($swProduct->getCustomFields()['custom_product_attributs_lieferstatus'] == 'Lieferbar') {
foreach ($product as $customerId) {
$mailTemplate = $this->helperService->getMailTemplate(
$mailTemplateId,
$event->getContext(),
);
$mailSubject = $this->helperService->mailTemplateVariablePlacer(
$mailTemplate->getSubject(),
'{{product.name}}',
$swProduct->getName(),
);
$mailContent = $mailTemplate->getContentHtml();
$customer = $this->helperService->getCustomer($customerId, $event->getContext());
$customerName = $customer->getFirstName() . ' ' . $customer->getLastName();
$customerMail = $customer->getCustomFields()['nwb_email'];
$mailContent = $this->helperService->mailTemplateVariablePlacer(
$mailContent,
'{{customer.name}}',
$customerName,
);
$mailContent = $this->helperService->mailTemplateVariablePlacer(
$mailContent,
'{{product.name}}',
$swProduct->getName(),
);
$parameter = ['productId' => $swProduct->getId()];
$raw = $this->seoUrlHandler->generate('frontend.detail.page', $parameter);
$url = $this->seoUrlHandler->replace(
$raw,
'https://' . $shopName,
$event->getSalesChannelContext(),
);
$mailContent = $this->helperService->mailTemplateVariablePlacer(
$mailContent,
'{{product.link}}',
$url,
);
$this->helperService->sendMail(
[$customerMail => $customerName],
$event
->getSalesChannelContext()
->getSalesChannel()
->getName(),
$swProduct->getName() . ' ist jetzt im NWB Shop verfügbar',
$mailContent,
$event->getSalesChannelContext(),
$product,
);
}
foreach ($notifyIds[$productId] as $notifyId) {
$this->notifyRepository->delete([['id' => $notifyId]], $event->getContext());
}
}
}
}
die();
}
}