<?php declare(strict_types=1);
namespace NetzpShopmanager6\Subscriber;
use NetzpShopmanager6\Components\PushMessageHelper;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FrontendSubscriber implements EventSubscriberInterface
{
private $container;
private $config;
private $pushHelper;
public function __construct(ContainerInterface $container,
SystemConfigService $config,
PushMessageHelper $pushHelper)
{
$this->container = $container;
$this->config = $config;
$this->pushHelper = $pushHelper;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutOrderPlacedEvent::class => 'onOrder'
];
}
public function onOrder(CheckoutOrderPlacedEvent $event): void
{
$config = $this->config->get('NetzpShopmanager6.config');
if(array_key_exists('usepush', $config) && $config['usepush']) {
$salesChannelId = $event->getSalesChannelId();
$repo = $this->container->get('language.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('id', $event->getContext()->getLanguageId()));
$criteria->addAssociation('locale');
$language = $repo->search($criteria, $event->getContext())->getEntities()->first();
$localeCode = 'de';
if($language != null) {
$localeCode = $language->getLocale()->getCode() ?? 'de';
}
$localeCode = strtolower(substr($localeCode, 0, 2));
$template = $this->config->get('NetzpShopmanager6.config.pushtemplate', $salesChannelId);
if ($template == '') {
$template = PushMessageHelper::DEFAULT_PUSH_TEMPLATE;
}
$this->pushHelper->sendPushMessage(
$event->getOrder(),
$event->getContext(),
$template,
$salesChannelId,
null,
$localeCode);
}
}
}