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

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.         $this->budgetLoaderService $budgetLoaderService;
  23.         $this->container $container;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             CustomerEvents::CUSTOMER_LOADED_EVENT => 'onLoaded'
  29.         ];
  30.     }
  31.     public function onLoaded(EntityLoadedEvent $event)
  32.     {
  33.         /** @var BudgetLoaderService $budgetLoaderService */
  34.         if ($event->getContext()->getScope() !== 'user' || !($event->getContext()->getSource() instanceof SalesChannelApiSource)) {
  35.             return;
  36.         }
  37.         /** @var CustomerEntity $customer */
  38.         $customers $event->getEntities();
  39.         foreach ($customers as $customer) {
  40.             $customFields $customer->getCustomFields();
  41.             $customerBudgets $this->budgetLoaderService->getActiveBudgetsByCustomer($customer$event->getContext());
  42.             if ($customerBudgets) {
  43.                 $customFields['cio_active_budgets'] = array_map(function ($customerBudget) {
  44.                     return $customerBudget->getId();
  45.                 }, $customerBudgets->getElements());
  46.             }
  47.             $customer->setCustomFields($customFields);
  48.         }
  49.     }
  50. }