<?php declare(strict_types=1);
namespace Tonur\GreetingCard\Subscriber;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Content\Product\SalesChannel\Detail\AbstractProductDetailRoute;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Tonur\GreetingCard\Service\GreetingCardServiceInterface;
use Tonur\GreetingCard\Util\PluginConfigUtil;
class GreetingCardSubscriber implements EventSubscriberInterface
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var GreetingCardServiceInterface
*/
private $greetingCardService;
/**
* @var AbstractProductDetailRoute
*/
private $productRoute;
/**
* @var EntityRepositoryInterface
*/
private $orderRepository;
private PluginConfigUtil $pluginConfig;
public function __construct(
RequestStack $requestStack,
GreetingCardServiceInterface $greetingCardService,
AbstractProductDetailRoute $productRoute,
EntityRepositoryInterface $orderRepository,
PluginConfigUtil $pluginConfig
)
{
$this->requestStack = $requestStack;
$this->greetingCardService = $greetingCardService;
$this->productRoute = $productRoute;
$this->orderRepository = $orderRepository;
$this->pluginConfig = $pluginConfig;
}
public static function getSubscribedEvents()
{
return [
StorefrontRenderEvent::class => 'onStorefrontRender',
CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlaced',
];
}
/**
* @param StorefrontRenderEvent $event
* @throws \Shopware\Core\Content\Product\Exception\ProductNotFoundException
* @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
* @codeCoverageIgnore
*/
public function onStorefrontRender(StorefrontRenderEvent $event): void
{
if (!$this->pluginConfig->isPluginActive($event->getSalesChannelContext())) {
return;
}
if (strpos($event->getView(), 'storefront/page/checkout/cart/index.html.twig') !== false ||
strpos($event->getView(), 'storefront/page/checkout/confirm/index.html.twig') !== false) {
$event->setParameter(
'repertusGreetingCardCartHasGreetingCard',
$this->greetingCardService->checkIfCartHasAlreadyCard($event->getSalesChannelContext())
);
$event->setParameter(
'repertusGreetingCardCartHasProductWithCard',
$this->greetingCardService->checkIfCartContainsProductWithCard($event->getSalesChannelContext())
);
$salesChannelContext = $event->getSalesChannelContext();
$greetingCards = $this->greetingCardService->getGreetingCardsForSalesChannel($salesChannelContext);
$products = [];
$criteria = (new Criteria())
->addAssociation('manufacturer.media')
->addAssociation('options.group')
->addAssociation('properties.group')
->addAssociation('mainCategories.category');
$criteria
->getAssociation('media')
->addSorting(new FieldSorting('position'));
foreach ($greetingCards as $greetingCard) {
$result = $this->productRoute->load($greetingCard->getProductId(), new Request(), $salesChannelContext, $criteria);
$products[$greetingCard->getId()] = $result->getProduct();
$products[$greetingCard->getId()]->assign([
'greetingCardId' => $greetingCard->getId(),
'greetingCardTextMaxLength' => $greetingCard->getTextMaxLength()
]);
}
$event->setParameter('repertusGreetingCards', $products);
$event->setParameter('repertusGreetingCardContainerId', \Shopware\Core\Framework\Uuid\Uuid::randomHex());
}
}
public function onCheckoutOrderPlaced(CheckoutOrderPlacedEvent $event): void
{
if (!$this->pluginConfig->isPluginActive(null, $event->getSalesChannelId())) {
return;
}
$request = $this->requestStack->getCurrentRequest();
if ($request !== null) {
$noInvoice = $request->request->get('repertusNoInvoice');
if ($noInvoice !== null) {
$order = $event->getOrder();
$customFields = $order->getCustomFields() ?? [];
$customFields['tonur_no_invoice'] = filter_var($noInvoice, FILTER_VALIDATE_BOOLEAN);
$this->orderRepository->upsert([
[
'id' => $order->getId(),
'customFields' => $customFields
]
], $event->getContext());
}
}
}
}