custom/plugins/VioRepresentativeLogin/src/Storefront/Subscriber/NoCustomerCartSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace VioRepresentativeLogin\Storefront\Subscriber;
  3. use Shopware\Core\Framework\Struct\ArrayStruct;
  4. use Shopware\Core\System\SystemConfig\SystemConfigService;
  5. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use VioRepresentativeLogin\Core\Services\AgentCustomerCartReplaceService;
  8. use VioRepresentativeLogin\Core\Services\AgentCustomerCartService;
  9. class NoCustomerCartSubscriber implements EventSubscriberInterface
  10. {
  11.     private AgentCustomerCartService $agentCustomerCartService;
  12.     private AgentCustomerCartReplaceService $agentCustomerCartReplaceService;
  13.     private SystemConfigService $systemConfigService;
  14.     public function __construct(
  15.         AgentCustomerCartService $agentCustomerCartService,
  16.         AgentCustomerCartReplaceService $agentCustomerCartReplaceService,
  17.         SystemConfigService  $systemConfigService
  18.     ) {
  19.         $this->agentCustomerCartService $agentCustomerCartService;
  20.         $this->agentCustomerCartReplaceService $agentCustomerCartReplaceService;
  21.         $this->systemConfigService $systemConfigService;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             GenericPageLoadedEvent::class => 'onPageLoaded'
  27.         ];
  28.     }
  29.     public function onPageLoaded(GenericPageLoadedEvent $event): void
  30.     {
  31.         $page $event->getPage();
  32.         // check context payload
  33.         // when no customer cart, then add page extension
  34.         $context $event->getSalesChannelContext();
  35.         $customerId $context->getCustomer() ? $context->getCustomer()->getId() : null;
  36.         if (
  37.             $this->agentCustomerCartService->loggedInAsAgentAndCustomer($context) &&
  38.             !$this->agentCustomerCartService->customerCartExistence($customerId)
  39.         ) {
  40.             $handlingMode $this->systemConfigService->getString('VioRepresentativeLogin.config.noCustomerCartHandling');
  41.             switch ($handlingMode){
  42.                 case "useAgentCart":
  43.                     $this->agentCustomerCartReplaceService->replaceCustomerCart($contextAgentCustomerCartReplaceService::REPLACE_CART_MODE_COPY);
  44.                     break;
  45.                 case "createNewCart":
  46.                     $this->agentCustomerCartReplaceService->replaceCustomerCart($contextAgentCustomerCartReplaceService::REPLACE_CART_MODE_NEW);
  47.                     break;
  48.                 case "popup":
  49.                 default:
  50.                     $page->addExtension('agentNoCustomerCart', new ArrayStruct(['agentNoCustomerCart' => true]));
  51.                     break;
  52.             }
  53.         }
  54.     }
  55. }