<?php
namespace CioProductCustomerInputs\Subscriber;
use CioBudget\PaymentHandler\BudgetPayment;
use CioBudget\Service\StoreLoaderService;
use CioSponsoredOrderConfirmation\Core\Error\MixedSponsoredProductsError;
use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\DefaultPayment;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
use Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
class AutoChangeShippingMethodSubscriber implements EventSubscriberInterface
{
private ContainerInterface $container;
private RouterInterface $router;
public function __construct(ContainerInterface $container, RouterInterface $router)
{
$this->container = $container;
$this->router = $router;
}
public static function getSubscribedEvents()
{
return [
StorefrontRenderEvent::class => 'onStorefrontRender'
];
}
public function onStorefrontRender(StorefrontRenderEvent $event)
{
$route = $event->getRequest()->get('_route');
if (in_array($route, ['frontend.cart.offcanvas', 'frontend.checkout.confirm.page', 'frontend.checkout.cart.page'])) {
/** @var CartService $cartService */
$cartService = $this->container->get('Shopware\Core\Checkout\Cart\SalesChannel\CartService');
$cart = $cartService->getCart($event->getSalesChannelContext()->getToken(), $event->getSalesChannelContext());
if ($cart->getErrors()->count() > 1) {
return;
}
if ($cart->getLineItems()->count() == 1 && count($cart->getErrors()->getElements())) {
if (!array_key_exists(MixedSponsoredProductsError::MESSAGE_KEY, $cart->getErrors()->getElements())) {
$data = null;
if (array_key_exists('shipping-method-blocked-Standard', $cart->getErrors()->getElements())) {
$cart->setErrors(new ErrorCollection(array_filter($cart->getErrors()->getElements(), function ($key) {
return $key !== 'shipping-method-blocked-Standard';
}, ARRAY_FILTER_USE_KEY)));
$data = new RequestDataBag([
SalesChannelContextService::SHIPPING_METHOD_ID => $this->getShippingMethodId('Sponsoring Versand')
]);
}
if (array_key_exists('shipping-method-blocked-Sponsoring Versand', $cart->getErrors()->getElements())) {
$cart->setErrors(new ErrorCollection(array_filter($cart->getErrors()->getElements(), function ($key) {
return $key !== 'shipping-method-blocked-Sponsoring Versand';
}, ARRAY_FILTER_USE_KEY)));
$data = new RequestDataBag([
SalesChannelContextService::SHIPPING_METHOD_ID => $this->getShippingMethodId('Standard')
]);
}
if (!is_null($data)) {
/** @var ContextSwitchRoute $contextSwitchRoute */
$contextSwitchRoute = $this->container->get('Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute');
$contextSwitchRoute->switchContext($data, $event->getSalesChannelContext());
$response = new RedirectResponse($this->router->generate($route));
$response->send();
}
}
} elseif (!$cart->getLineItems()->count()) {
if (array_key_exists('shipping-method-blocked-Sponsoring Versand', $cart->getErrors()->getElements())) {
$cart->setErrors(new ErrorCollection(array_filter($cart->getErrors()->getElements(), function ($key) {
return $key !== 'shipping-method-blocked-Sponsoring Versand';
}, ARRAY_FILTER_USE_KEY)));
$data = new RequestDataBag([
SalesChannelContextService::SHIPPING_METHOD_ID => $this->getShippingMethodId('Standard')
]);
/** @var ContextSwitchRoute $contextSwitchRoute */
$contextSwitchRoute = $this->container->get('Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute');
$contextSwitchRoute->switchContext($data, $event->getSalesChannelContext());
$response = new RedirectResponse($this->router->generate($route));
$response->send();
}
}
}
}
protected function getShippingMethodId($name): ?string
{
/** @var EntityRepositoryInterface $repository */
$repository = $this->container->get('shipping_method.repository');
$criteria = (new Criteria())
->addFilter(new EqualsFilter('name', $name))
->addAssociation('shipping_method_translation');
return $repository->searchIds($criteria, Context::createDefaultContext())->firstId();
}
}