custom/plugins/CioSalesRepresentative/src/Subscriber/OrderCreateSubscriber.php line 62

Open in your IDE?
  1. <?php
  2. namespace CioSalesRepresentative\Subscriber;
  3. use CioMasterdata\Definition\Masterdata\MasterdataEntity;
  4. use CioMasterdata\Service\MasterdataService;
  5. use CioOrderConfirmation\Event\AuthorizedReleaserBeforeSendMailEvent;
  6. use CioSalesRepresentative\Definition\SalesRepresentative\SalesRepresentativeEntity;
  7. use Shopware\Core\Checkout\Customer\CustomerEntity;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Storefront\Event\StorefrontRenderEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Session\Session;
  16. class OrderCreateSubscriber implements EventSubscriberInterface
  17. {
  18.     private EntityRepositoryInterface $salesRepRepository;
  19.     private Session $session;
  20.     private SystemConfigService $systemConfigService;
  21.     private MasterdataService $masterdataService;
  22.     public function __construct(EntityRepositoryInterface $salesRepRepositorySession $sessionSystemConfigService $systemConfigServiceMasterdataService $masterdataService)
  23.     {
  24.         $this->salesRepRepository $salesRepRepository;
  25.         $this->session $session;
  26.         $this->systemConfigService $systemConfigService;
  27.         $this->masterdataService $masterdataService;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             AuthorizedReleaserBeforeSendMailEvent::class => 'onAuthorizedReleaserBeforeSendMailEvent',
  33.             StorefrontRenderEvent::class => 'onStorefrontRenderEvent'
  34.         ];
  35.     }
  36.     public function onAuthorizedReleaserBeforeSendMailEvent(AuthorizedReleaserBeforeSendMailEvent $event)
  37.     {
  38.         $order $event->getOrder();
  39.         $customer $event->getCustomer();
  40.         $salesRepId null;
  41.         $masterdatas $this->masterdataService->getMasterdatasForCustomer($customer);
  42.         /** @var MasterdataEntity|null $masterdata */
  43.         if ($masterdatas instanceof EntitySearchResult && ($masterdata $masterdatas->first()) instanceof MasterdataEntity) {
  44.             $salesRepId $masterdata->getVPartnerId();
  45.         }
  46.         /** @var SalesRepresentativeEntity $salesRepResult */
  47.         $salesRepResult $this->salesRepRepository->search(new Criteria([$salesRepId]), Context::createDefaultContext())->first();
  48.         $event->setAuthorizedReleaserEmail($salesRepResult->getEmail());
  49.     }
  50.     public function onStorefrontRenderEvent(StorefrontRenderEvent $event)
  51.     {
  52.         if ($event->getSalesChannelContext()->getCustomer() instanceof CustomerEntity && $this->session->has('cio_master_login') && $this->session->get('cio_master_login') != $event->getSalesChannelContext()->getCustomer()->getId()) {
  53.             $event->setParameter('cio_master_login_show_bar'true);
  54.         } else {
  55.             $event->setParameter('cio_master_login_show_bar'false);
  56.         }
  57.     }
  58. }