custom/plugins/b2bsellerscore/src/Components/Customer/Subscriber/CustomerEntitySubscriber.php line 25

Open in your IDE?
  1. <?php
  2. namespace B2bSellersCore\Components\Customer\Subscriber;
  3. use B2bSellersCore\Components\Customer\CustomerDisplayName;
  4. use FontLib\Table\Type\glyf;
  5. use Shopware\Core\Checkout\Customer\CustomerEntity;
  6. use Shopware\Core\Checkout\Customer\CustomerEvents;
  7. use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
  8. use Shopware\Core\Checkout\Order\OrderEntity;
  9. use Shopware\Core\Checkout\Order\OrderEvents;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class CustomerEntitySubscriber implements EventSubscriberInterface
  13. {
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             CustomerEvents::CUSTOMER_LOADED_EVENT => 'onCustomersLoadedAddDisplayName',
  18.             OrderEvents::ORDER_LOADED_EVENT => 'onOrdersLoadedAddDisplayName',
  19.         ];
  20.     }
  21.     public function onCustomersLoadedAddDisplayName(EntityLoadedEvent $event): void
  22.     {
  23.         /** @var CustomerEntity $customerEntity */
  24.         foreach ($event->getEntities() as $customerEntity) {
  25.             $isB2bCustomer = (bool)($customerEntity->getCustomFields()['b2b_platform_access'] ?? false);
  26.             $this->addDisplayNameExtension($customerEntity$isB2bCustomer);
  27.         }
  28.     }
  29.     public function onOrdersLoadedAddDisplayName(EntityLoadedEvent $event): void
  30.     {
  31.         /** @var OrderEntity $orderEntity */
  32.         foreach ($event->getEntities() as $orderEntity) {
  33.             if(!$orderEntity->getOrderCustomer()) {
  34.                 continue;
  35.             }
  36.             /** @var OrderCustomerEntity $orderCustomer */
  37.             $orderCustomer $orderEntity->getOrderCustomer();
  38.             $isB2bCustomer false;
  39.             $employeeId $orderEntity->getCustomFields()['b2b_order_customer_employee_id'] ?? null;
  40.             if($orderCustomer->getCustomer()) {
  41.                 /** @var CustomerEntity $customer */
  42.                 $customer $orderCustomer->getCustomer();
  43.                 $isB2bCustomer = (bool)($customer->getCustomFields()['b2b_platform_access'] ?? false);
  44.                 $this->addDisplayNameExtension($customer$isB2bCustomer);
  45.             }
  46.             if(!$isB2bCustomer && !empty($employeeId)) {
  47.                 $isB2bCustomer true;
  48.             }
  49.             $this->addDisplayNameExtension($orderCustomer$isB2bCustomer);
  50.         }
  51.     }
  52.     /**
  53.      * @param OrderCustomerEntity|CustomerEntity $entity
  54.      * @param bool $isB2bCustomer
  55.      * @return void
  56.      */
  57.     private function addDisplayNameExtension($entitybool $isB2bCustomer): void
  58.     {
  59.         $shortName $entity->getFirstName() . ' ' $entity->getLastName();
  60.         $name $shortName;
  61.         $longName $shortName;
  62.         if(!empty($entity->getTitle())) {
  63.             $longName $entity->getTitle() . ' ' $shortName;
  64.         }
  65.         if($entity->getSalutation() && $entity->getSalutation()->getSalutationKey() !== 'not_specified') {
  66.             $name $entity->getSalutation()->getTranslation('displayName') . ' ' $name;
  67.             $longName $entity->getSalutation()->getTranslation('displayName') . ' ' $longName;
  68.         }
  69.         if($isB2bCustomer && !empty($entity->getCompany())) {
  70.             $longName $entity->getCompany() . ' - ' $shortName;
  71.             $shortName $entity->getCompany();
  72.             $name $entity->getCompany();
  73.             if ((empty($entity->getFirstName()) || empty($entity->getLastName()))) {
  74.                 $longName $entity->getCompany();
  75.             }
  76.         }
  77.         $entity->addExtension('displayName', new CustomerDisplayName($shortName$name$longName));
  78.     }
  79. }