<?php declare(strict_types=1);
namespace Acris\MerchantRegister\Storefront\Subscriber;
use Acris\MerchantRegister\Components\CustomerGroupService;
use Acris\MerchantRegister\Components\Struct\CustomerGroupActivatedStruct;
use Acris\Search\Storefront\Page\Manufacturer\ManufacturerPage;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Account\Login\AccountLoginPage;
use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Shopware\Storefront\Page\Page;
use Shopware\Storefront\Page\PageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\Struct\Struct;
class ReplaceRegisterFormSubscriber implements EventSubscriberInterface
{
private CustomerGroupService $customerGroupService;
private SystemConfigService $systemConfigService;
public function __construct(CustomerGroupService $customerGroupService, SystemConfigService $systemConfigService)
{
$this->customerGroupService = $customerGroupService;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
AccountLoginPageLoadedEvent::class => 'onAccountLoginPageLoaded',
CheckoutRegisterPageLoadedEvent::class => 'onCheckoutRegisterPageLoaded'
];
}
public function onAccountLoginPageLoaded(AccountLoginPageLoadedEvent $event):void
{
$this->addPageExtensions($event);
}
public function onCheckoutRegisterPageLoaded(CheckoutRegisterPageLoadedEvent $event):void
{
$this->addPageExtensions($event);
}
private function addPageExtensions(PageLoadedEvent $event): void {
$page = $event->getPage();
$context = $event->getContext();
$configCustomerGroupFormId = $this->systemConfigService->get('AcrisMerchantRegisterCS.config.replaceRegisterFormWith', $event->getSalesChannelContext()->getSalesChannelId());
if (!empty($configCustomerGroupFormId)) {
$customerGroup= $this->customerGroupService->getCustomerGroupById($context, $configCustomerGroupFormId);
if (!empty($customerGroup) && $customerGroup->registrationActive) {
$page->addExtension(CustomerGroupActivatedStruct::CUSTOMER_GROUP_ACTIVATED_EXTENSION, new CustomerGroupActivatedStruct());
$this->maintainCustomerGroupProperty($page, $customerGroup);
}
}
}
private function maintainCustomerGroupProperty(Page $page, Struct $customerGroupEntity) {
$page->addExtension('configCustomerGroup', $customerGroupEntity);
$page->group = $customerGroupEntity;
}
}