custom/plugins/theme/src/Subscriber/CustomerGroupSubscriber.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DonCarneTheme\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEntity;
  4. use Shopware\Core\Framework\Struct\ArrayStruct;
  5. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  6. use Shopware\Storefront\Page\Page;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class CustomerGroupSubscriber implements EventSubscriberInterface
  9. {
  10.     public static function getSubscribedEvents(): array
  11.     {
  12.         return [
  13.             GenericPageLoadedEvent::class => 'onGenericPageLoaded',
  14.         ];
  15.     }
  16.     public function onGenericPageLoaded(GenericPageLoadedEvent $event): void
  17.     {
  18.         $page $event->getPage();
  19.         $context $event->getSalesChannelContext();
  20.         $customer $context->getCustomer();
  21.         if (!$customer instanceof CustomerEntity) {
  22.             $this->addCustomerGroupInfo($pagenullnull);
  23.             return;
  24.         }
  25.         // Hole die Kundengruppe aus dem SalesChannelContext, da dort die Übersetzungen geladen sind
  26.         $customerGroup $context->getCurrentCustomerGroup();
  27.         
  28.         if ($customerGroup) {
  29.             $this->addCustomerGroupInfo(
  30.                 $page,
  31.                 $customerGroup->getId(),
  32.                 $customerGroup->getTranslation('name') ?? $customerGroup->getName()
  33.             );
  34.         } else {
  35.             $this->addCustomerGroupInfo($pagenullnull);
  36.         }
  37.     }
  38.     private function addCustomerGroupInfo(Page $page, ?string $groupId, ?string $groupName): void
  39.     {
  40.         if($groupName){
  41.             $groupName str_replace(' ''_'$groupName);
  42.         }
  43.         $page->addExtension('customerGroupInfo', new ArrayStruct([
  44.             'id' => $groupId,
  45.             'name' => $groupName,
  46.             'isLoggedIn' => $groupId !== null,
  47.         ]));
  48.     }
  49. }