vendor/shopware/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.             $customerId $order->getOrderCustomer()->getCustomerId();
  59.             // happens if the customer was deleted
  60.             if (!$customerId) {
  61.                 continue;
  62.             }
  63.             $orderCount 0;
  64.             $customerResult $this->customerRepository->search(
  65.                 (new Criteria([$customerId]))->addAssociation('orderCustomers'),
  66.                 $context
  67.             );
  68.             /** @var CustomerEntity $customer */
  69.             $customer $customerResult->first();
  70.             if ($customer !== null && $customer->getOrderCustomers()) {
  71.                 $orderCount $customer->getOrderCustomers()->count();
  72.             }
  73.             $data = [
  74.                 [
  75.                     'id' => $customerId,
  76.                     'orderCount' => $orderCount,
  77.                     'lastOrderDate' => $orderDate->format('Y-m-d H:i:s.v'),
  78.                 ],
  79.             ];
  80.             $context->scope(Context::SYSTEM_SCOPE, function () use ($data$context): void {
  81.                 $this->customerRepository->update($data$context);
  82.             });
  83.         }
  84.     }
  85. }