custom/plugins/GrimmTheme/src/Storefront/Subscriber/CheckoutPagesLoadedSubscriber.php line 93

Open in your IDE?
  1. <?php declare(strict_types 1);
  2. namespace GrimmTheme\Storefront\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  5. use Shopware\Core\System\SystemConfig\SystemConfigService;
  6. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  7. use Shopware\Core\Framework\Struct\ArrayStruct;
  8. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  9. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  10. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  11. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
  12. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  13. // SD-4986
  14. class CheckoutPagesLoadedSubscriber implements EventSubscriberInterface
  15. {
  16.     private SystemConfigService $systemConfigService;
  17.     private SalesChannelContextPersister $contextPersister;
  18.     public function __construct(
  19.         SystemConfigService $systemConfigService,
  20.         SalesChannelContextPersister $contextPersister
  21.     ){
  22.         $this->systemConfigService $systemConfigService;
  23.         $this->contextPersister $contextPersister;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             CheckoutRegisterPageLoadedEvent::class => 'onCheckoutRegisterPageLoaded',
  29.             CheckoutConfirmPageLoadedEvent::class => 'onConfirmPageLoaded',
  30.             CheckoutCartPageLoadedEvent::class => 'onCartPageLoaded',
  31.             OffcanvasCartPageLoadedEvent::class => 'onOffcanvasLoaded',
  32.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoaded'
  33.         ];
  34.     }
  35.     public function onCheckoutRegisterPageLoaded(CheckoutRegisterPageLoadedEvent $event)
  36.     {
  37.         $page $event->getPage();
  38.         $this->addRequestOnlyPageExtension($page);
  39.     }
  40.     public function onConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event)
  41.     {
  42.         $page $event->getPage();
  43.         $containsRequestOnlyProducts $this->addRequestOnlyPageExtension($page);
  44.         
  45.         $requestOnlyPaymentMethodId $this->systemConfigService->get('GrimmTheme.config.requestPaymentMethodId');
  46.         if(!$requestOnlyPaymentMethodId){
  47.             return;
  48.         }
  49.         // we have at least one product with request only in cart -> remove all payment methods besides "Angebotsanfrage"
  50.         if($containsRequestOnlyProducts){
  51.             $paymentMethods $page->getPaymentMethods();
  52.             $paymentMethodsElements $paymentMethods->getElements();
  53.             
  54.             // remove all payment methods besides requestOnlyPaymentMethod
  55.             if(!isset($paymentMethodsElements) || !isset($paymentMethodsElements[$requestOnlyPaymentMethodId])){
  56.                 return;
  57.             }
  58.             $requestOnlyPaymentMethod $paymentMethodsElements[$requestOnlyPaymentMethodId];
  59.             $filteredPaymentMethods = new PaymentMethodCollection([$requestOnlyPaymentMethod]);
  60.             $page->setPaymentMethods($filteredPaymentMethods);
  61.             // switch context paymentMethodId to Angebotsanfrage, mimics logic of /checkout/configure Route that gets called when payment manually switched
  62.             // necessary because selecting preselecting payment method in twig not enough
  63.             $context $event->getSalesChannelContext();
  64.             $parameters = ["paymentMethodId" => $requestOnlyPaymentMethodId];
  65.             $customer $context->getCustomer();
  66.             $this->contextPersister->save(
  67.                 $context->getToken(),
  68.                 $parameters,
  69.                 $context->getSalesChannel()->getId(),
  70.                 $customer && empty($context->getPermissions()) ? $customer->getId() : null
  71.             );
  72.         }
  73.     }
  74.     public function onCartPageLoaded(CheckoutCartPageLoadedEvent $event)
  75.     {
  76.         $page $event->getPage();
  77.         $this->addRequestOnlyPageExtension($page);
  78.     }
  79.     public function onOffcanvasLoaded(OffcanvasCartPageLoadedEvent $event)
  80.     {
  81.         $page $event->getPage();
  82.         $this->addRequestOnlyPageExtension($page);
  83.     }
  84.     // needed in order to hide trusted shops on finish page
  85.     public function onCheckoutFinishPageLoaded(CheckoutFinishPageLoadedEvent $event)
  86.     {
  87.         $page $event->getPage();
  88.         $order $page->getOrder();
  89.         $transactions $order->getTransactions();
  90.         // account for unsuccessful order
  91.         if(!$transactions){
  92.             return;
  93.         }
  94.         $requestOnlyPaymentMethodId $this->systemConfigService->get('GrimmTheme.config.requestPaymentMethodId');
  95.         if(!$requestOnlyPaymentMethodId){
  96.             return;
  97.         }
  98.         if($transactions->first()){
  99.             $paymentId $transactions->first()->getPaymentMethodId();
  100.             if($paymentId == $requestOnlyPaymentMethodId){
  101.                 $page->addExtension("productsRequestOnly", new ArrayStruct([]));
  102.             }
  103.         }
  104.     }
  105.     private function addRequestOnlyPageExtension($page): bool
  106.     {
  107.         $cart $page->getCart();
  108.         $lineItems $cart->getLineItems()->getPayload();
  109.         if(!$lineItems){
  110.             return false;
  111.         }
  112.         $productsRequestOnly = [];
  113.         foreach($lineItems as $lineItem){
  114.             if(isset($lineItem["customFields"]["grimm_customfields_productRequest_only"]) && $lineItem["customFields"]["grimm_customfields_productRequest_only"] == "true"){
  115.                 array_push($productsRequestOnly$lineItem["productNumber"]);
  116.             }
  117.         }
  118.         if(!empty($productsRequestOnly)){
  119.             // add productNumber as page extension
  120.             $pageExtension = new ArrayStruct($productsRequestOnly);
  121.             $page->addExtension("productsRequestOnly"$pageExtension);
  122.             return true;
  123.         }
  124.         return false;
  125.     }
  126. }