<?php
namespace B2bSellersCore\Components\Customer\Subscriber;
use B2bSellersCore\Components\Customer\CustomerDisplayName;
use FontLib\Table\Type\glyf;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CustomerEntitySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
CustomerEvents::CUSTOMER_LOADED_EVENT => 'onCustomersLoadedAddDisplayName',
OrderEvents::ORDER_LOADED_EVENT => 'onOrdersLoadedAddDisplayName',
];
}
public function onCustomersLoadedAddDisplayName(EntityLoadedEvent $event): void
{
/** @var CustomerEntity $customerEntity */
foreach ($event->getEntities() as $customerEntity) {
$isB2bCustomer = (bool)($customerEntity->getCustomFields()['b2b_platform_access'] ?? false);
$this->addDisplayNameExtension($customerEntity, $isB2bCustomer);
}
}
public function onOrdersLoadedAddDisplayName(EntityLoadedEvent $event): void
{
/** @var OrderEntity $orderEntity */
foreach ($event->getEntities() as $orderEntity) {
if(!$orderEntity->getOrderCustomer()) {
continue;
}
/** @var OrderCustomerEntity $orderCustomer */
$orderCustomer = $orderEntity->getOrderCustomer();
$isB2bCustomer = false;
$employeeId = $orderEntity->getCustomFields()['b2b_order_customer_employee_id'] ?? null;
if($orderCustomer->getCustomer()) {
/** @var CustomerEntity $customer */
$customer = $orderCustomer->getCustomer();
$isB2bCustomer = (bool)($customer->getCustomFields()['b2b_platform_access'] ?? false);
$this->addDisplayNameExtension($customer, $isB2bCustomer);
}
if(!$isB2bCustomer && !empty($employeeId)) {
$isB2bCustomer = true;
}
$this->addDisplayNameExtension($orderCustomer, $isB2bCustomer);
}
}
/**
* @param OrderCustomerEntity|CustomerEntity $entity
* @param bool $isB2bCustomer
* @return void
*/
private function addDisplayNameExtension($entity, bool $isB2bCustomer): void
{
$shortName = $entity->getFirstName() . ' ' . $entity->getLastName();
$name = $shortName;
$longName = $shortName;
if(!empty($entity->getTitle())) {
$longName = $entity->getTitle() . ' ' . $shortName;
}
if($entity->getSalutation() && $entity->getSalutation()->getSalutationKey() !== 'not_specified') {
$name = $entity->getSalutation()->getTranslation('displayName') . ' ' . $name;
$longName = $entity->getSalutation()->getTranslation('displayName') . ' ' . $longName;
}
if($isB2bCustomer && !empty($entity->getCompany())) {
$longName = $entity->getCompany() . ' - ' . $shortName;
$shortName = $entity->getCompany();
$name = $entity->getCompany();
if ((empty($entity->getFirstName()) || empty($entity->getLastName()))) {
$longName = $entity->getCompany();
}
}
$entity->addExtension('displayName', new CustomerDisplayName($shortName, $name, $longName));
}
}