<?php
namespace VioRepresentativeLogin\Storefront\Subscriber;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use VioRepresentativeLogin\Core\Services\AgentCustomerCartReplaceService;
use VioRepresentativeLogin\Core\Services\AgentCustomerCartService;
class NoCustomerCartSubscriber implements EventSubscriberInterface
{
private AgentCustomerCartService $agentCustomerCartService;
private AgentCustomerCartReplaceService $agentCustomerCartReplaceService;
private SystemConfigService $systemConfigService;
public function __construct(
AgentCustomerCartService $agentCustomerCartService,
AgentCustomerCartReplaceService $agentCustomerCartReplaceService,
SystemConfigService $systemConfigService
) {
$this->agentCustomerCartService = $agentCustomerCartService;
$this->agentCustomerCartReplaceService = $agentCustomerCartReplaceService;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
GenericPageLoadedEvent::class => 'onPageLoaded'
];
}
public function onPageLoaded(GenericPageLoadedEvent $event): void
{
$page = $event->getPage();
// check context payload
// when no customer cart, then add page extension
$context = $event->getSalesChannelContext();
$customerId = $context->getCustomer() ? $context->getCustomer()->getId() : null;
if (
$this->agentCustomerCartService->loggedInAsAgentAndCustomer($context) &&
!$this->agentCustomerCartService->customerCartExistence($customerId)
) {
$handlingMode = $this->systemConfigService->getString('VioRepresentativeLogin.config.noCustomerCartHandling');
switch ($handlingMode){
case "useAgentCart":
$this->agentCustomerCartReplaceService->replaceCustomerCart($context, AgentCustomerCartReplaceService::REPLACE_CART_MODE_COPY);
break;
case "createNewCart":
$this->agentCustomerCartReplaceService->replaceCustomerCart($context, AgentCustomerCartReplaceService::REPLACE_CART_MODE_NEW);
break;
case "popup":
default:
$page->addExtension('agentNoCustomerCart', new ArrayStruct(['agentNoCustomerCart' => true]));
break;
}
}
}
}