custom/plugins/CioBudget/src/Subscriber/FilterCustomerEntityBudgetsSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. namespace CioBudget\Subscriber;
  3. use CioBudget\Definition\Budget\BudgetEntity;
  4. use CioBudget\Service\BudgetLoaderService;
  5. use Shopware\Core\Checkout\Customer\CustomerEntity;
  6. use Shopware\Core\Checkout\Customer\CustomerEvents;
  7. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  12. use Symfony\Component\DependencyInjection\Container;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class FilterCustomerEntityBudgetsSubscriber implements EventSubscriberInterface
  15. {
  16.     private BudgetLoaderService $budgetLoaderService;
  17.     private Container $container;
  18.     public function __construct(
  19.         Container $container,
  20.         BudgetLoaderService $budgetLoaderService
  21.     )
  22.     {
  23.         $this->budgetLoaderService $budgetLoaderService;
  24.         $this->container $container;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             CustomerEvents::CUSTOMER_LOADED_EVENT => 'onLoaded'
  30.         ];
  31.     }
  32.     public function onLoaded(EntityLoadedEvent $event)
  33.     {
  34.         /** @var BudgetLoaderService $budgetLoaderService */
  35.         if ($event->getContext()->getScope() !== 'user' || !($event->getContext()->getSource() instanceof SalesChannelApiSource)) {
  36.             return;
  37.         }
  38.         /** @var CustomerEntity $customer */
  39.         $customers $event->getEntities();
  40.         foreach ($customers as $customer) {
  41.             $customFields $customer->getCustomFields();
  42.             $customerBudgets $this->budgetLoaderService->getActiveBudgetsByCustomer($customer$event->getContext());
  43.             if ($customerBudgets) {
  44.                 $customFields['cio_active_budgets'] = array_map(function ($customerBudget) {
  45.                     return $customerBudget->getId();
  46.                 }, $customerBudgets->getElements());
  47.             }
  48.             $customer->setCustomFields($customFields);
  49.         }
  50.     }
  51. }