custom/plugins/CioSubaccounts/src/Subscriber/SubaccountsMasterdataInheritance.php line 32

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