<?php
declare(strict_types=1);
namespace FourtwosixThemeCustomization\Subscriber;
use FourtwosixThemeCustomization\FourtwosixThemeCustomization as Plugin;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Storefront\Framework\Routing\Router;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class CheckoutConfirmSubscriber implements EventSubscriberInterface
{
/** @var Router $router */
private Router $router;
public function __construct(Router $router)
{
$this->router = $router;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirm'
];
}
public function onCheckoutConfirm(PageLoadedEvent $event)
{
$customerGroupId = $event->getSalesChannelContext()->getCurrentCustomerGroup()->getId();
if (
$customerGroupId !== Plugin::CUSTOMER_GROUP_TRADER_ID &&
$customerGroupId !== Plugin::CUSTOMER_GROUP_FOREIGN_TRADER_ID
) {
return;
}
$cart = $event->getPage()->getCart();
$manufacturerIds = [
Plugin::MANUFACTURER_PLATINLUX_ID,
Plugin::MANUFACTURER_LUCIDE_ID,
];
if ($this->containsManufacturers($cart, $manufacturerIds)) {
return;
}
$path = $this->router->generate('frontend.checkout.cart.page');
$this->redirectTo($path);
}
private function containsManufacturers(Cart &$cart, array &$manufacturerIds): bool
{
foreach ($cart->getLineItems()->getElements() as $product) {
foreach ($manufacturerIds as $manufacturerId) {
if ($product->getPayload()['manufacturerId'] === $manufacturerId) {
return true;
}
}
}
return false;
}
private function redirectTo(string &$path)
{
$response = new RedirectResponse($path . '?missing_products=1', 307);
$response->send();
}
}