vendor/shopware/platform/src/Core/Checkout/Customer/Subscriber/CustomerMetaFieldSubscriber.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Customer\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEntity;
  4. use Shopware\Core\Checkout\Order\OrderDefinition;
  5. use Shopware\Core\Checkout\Order\OrderEntity;
  6. use Shopware\Core\Checkout\Order\OrderEvents;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class CustomerMetaFieldSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var EntityRepositoryInterface
  16.      */
  17.     private $orderRepository;
  18.     /**
  19.      * @var EntityRepositoryInterface
  20.      */
  21.     private $customerRepository;
  22.     public function __construct(EntityRepositoryInterface $orderRepositoryEntityRepositoryInterface $customerRepository)
  23.     {
  24.         $this->orderRepository $orderRepository;
  25.         $this->customerRepository $customerRepository;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             OrderEvents::ORDER_WRITTEN_EVENT => 'fillCustomerMetaDataFields',
  31.         ];
  32.     }
  33.     public function fillCustomerMetaDataFields(EntityWrittenEvent $event): void
  34.     {
  35.         if ($event->getEntityName() !== OrderDefinition::ENTITY_NAME) {
  36.             return;
  37.         }
  38.         $context $event->getContext();
  39.         foreach ($event->getWriteResults() as $writeResult) {
  40.             if ($writeResult->getExistence() !== null && $writeResult->getExistence()->exists()) {
  41.                 break;
  42.             }
  43.             $payload $writeResult->getPayload();
  44.             if (empty($payload)) {
  45.                 continue;
  46.             }
  47.             /** @var \DateTimeInterface $orderDate */
  48.             $orderDate $payload['orderDateTime'];
  49.             $orderResult $this->orderRepository->search(
  50.                 (new Criteria([$payload['id']]))->addAssociation('orderCustomer'),
  51.                 $context
  52.             );
  53.             /** @var OrderEntity|null $order */
  54.             $order $orderResult->first();
  55.             if (!($order instanceof OrderEntity)) {
  56.                 continue;
  57.             }
  58.             $orderCustomer $order->getOrderCustomer();
  59.             if ($orderCustomer === null) {
  60.                 continue;
  61.             }
  62.             $customerId $orderCustomer->getCustomerId();
  63.             // happens if the customer was deleted
  64.             if (!$customerId) {
  65.                 continue;
  66.             }
  67.             $orderCount 0;
  68.             $customerResult $this->customerRepository->search(
  69.                 (new Criteria([$customerId]))->addAssociation('orderCustomers'),
  70.                 $context
  71.             );
  72.             /** @var CustomerEntity $customer */
  73.             $customer $customerResult->first();
  74.             if ($customer !== null && $customer->getOrderCustomers()) {
  75.                 $orderCount $customer->getOrderCustomers()->count();
  76.             }
  77.             $data = [
  78.                 [
  79.                     'id' => $customerId,
  80.                     'orderCount' => $orderCount,
  81.                     'lastOrderDate' => $orderDate->format('Y-m-d H:i:s.v'),
  82.                 ],
  83.             ];
  84.             $context->scope(Context::SYSTEM_SCOPE, function () use ($data$context): void {
  85.                 $this->customerRepository->update($data$context);
  86.             });
  87.         }
  88.     }
  89. }