custom/plugins/CioSubaccounts/src/Subscriber/SubaccountsStoresInheritance.php line 30

Open in your IDE?
  1. <?php
  2. namespace CioSubaccounts\Subscriber;
  3. use CioBudget\Event\CustomerStoresLoadedEvent;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class SubaccountsStoresInheritance implements EventSubscriberInterface
  12. {
  13.     private ContainerInterface $container;
  14.     public function __construct(ContainerInterface $container)
  15.     {
  16.         $this->container $container;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             CustomerStoresLoadedEvent::class => 'onCustomerStoreIdsLoadedEvent'
  22.         ];
  23.     }
  24.     public function onCustomerStoreIdsLoadedEvent(CustomerStoresLoadedEvent $event)
  25.     {
  26.         $customer $event->getCustomer();
  27.         $parentCustomer $customer->getExtension('customerParent');
  28.         // ignore customers without parent
  29.         if (is_null($parentCustomer)) {
  30.             return;
  31.         }
  32.         /** @var EntityRepositoryInterface $customerRepository */
  33.         $customerRepository $this->container->get('customer.repository');
  34.         $criteria = new Criteria();
  35.         $criteria->addFilter(new EqualsFilter('id'$parentCustomer->getParent()));
  36.         $parentCustomer $customerRepository->search($criteriaContext::createDefaultContext())->first();
  37.         // customer has a parent for inheritance
  38.         if ($parentCustomer instanceof CustomerEntity) {
  39.             $storeLoaderService $this->container->get('customer.store_loader_service');
  40.             $event->setStores(
  41.                 array_merge(
  42.                     $event->getStores(), // stores from user
  43.                     $storeLoaderService->getCustomerStores($parentCustomer// stores from parent
  44.                 )
  45.             );
  46.         }
  47.     }
  48. }