vendor/shopware/core/Checkout/Customer/Subscriber/CustomerMetaFieldSubscriber.php line 50

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Customer\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Checkout\Order\OrderDefinition;
  5. use Shopware\Core\Checkout\Order\OrderStates;
  6. use Shopware\Core\Defaults;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  10. use Shopware\Core\Framework\Uuid\Uuid;
  11. use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class CustomerMetaFieldSubscriber implements EventSubscriberInterface
  14. {
  15.     private Connection $connection;
  16.     public function __construct(Connection $connection)
  17.     {
  18.         $this->connection $connection;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             StateMachineTransitionEvent::class => 'fillCustomerMetaDataFields',
  24.             PreWriteValidationEvent::class => 'deleteOrder',
  25.         ];
  26.     }
  27.     public function fillCustomerMetaDataFields(StateMachineTransitionEvent $event): void
  28.     {
  29.         if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  30.             return;
  31.         }
  32.         if ($event->getEntityName() !== 'order') {
  33.             return;
  34.         }
  35.         if ($event->getToPlace()->getTechnicalName() !== OrderStates::STATE_COMPLETED && $event->getFromPlace()->getTechnicalName() !== OrderStates::STATE_COMPLETED) {
  36.             return;
  37.         }
  38.         $this->updateCustomer([$event->getEntityId()]);
  39.     }
  40.     public function deleteOrder(PreWriteValidationEvent $event): void
  41.     {
  42.         if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  43.             return;
  44.         }
  45.         $orderIds = [];
  46.         foreach ($event->getCommands() as $command) {
  47.             if ($command->getDefinition()->getClass() === OrderDefinition::class
  48.                 && $command instanceof DeleteCommand
  49.             ) {
  50.                 $orderIds[] = Uuid::fromBytesToHex($command->getPrimaryKey()['id']);
  51.             }
  52.         }
  53.         $this->updateCustomer($orderIdstrue);
  54.     }
  55.     private function updateCustomer(array $orderIdsbool $isDelete false): void
  56.     {
  57.         if (empty($orderIds)) {
  58.             return;
  59.         }
  60.         $customerIds $this->connection->fetchFirstColumn(
  61.             'SELECT DISTINCT LOWER(HEX(customer_id)) FROM `order_customer` WHERE order_id IN (:ids) AND order_version_id = :version',
  62.             ['ids' => Uuid::fromHexToBytesList($orderIds), 'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION)],
  63.             ['ids' => Connection::PARAM_STR_ARRAY]
  64.         );
  65.         if (empty($customerIds)) {
  66.             return;
  67.         }
  68.         $whereOrder $isDelete 'AND `order`.id NOT IN (:exceptOrderIds)' '';
  69.         $select '
  70.             SELECT `order_customer`.customer_id as id,
  71.                    COUNT(`order`.id) as order_count,
  72.                    SUM(`order`.amount_total) as order_total_amount,
  73.                    MAX(`order`.order_date_time) as last_order_date
  74.             FROM `order_customer`
  75.             INNER JOIN `order`
  76.                 ON `order`.id = `order_customer`.order_id
  77.                 AND `order`.version_id = `order_customer`.order_version_id
  78.                 AND `order`.version_id = :version
  79.                 ' $whereOrder '
  80.             INNER JOIN `state_machine_state`
  81.                 ON `state_machine_state`.id = `order`.state_id
  82.                 AND `state_machine_state`.technical_name = :state
  83.             WHERE `order_customer`.customer_id IN (:customerIds)
  84.             GROUP BY `order_customer`.customer_id
  85.         ';
  86.         $data $this->connection->fetchAllAssociative($select, [
  87.             'customerIds' => Uuid::fromHexToBytesList($customerIds),
  88.             'version' => Uuid::fromHexToBytes(Defaults::LIVE_VERSION),
  89.             'state' => OrderStates::STATE_COMPLETED,
  90.             'exceptOrderIds' => Uuid::fromHexToBytesList($orderIds),
  91.         ], [
  92.             'customerIds' => Connection::PARAM_STR_ARRAY,
  93.             'exceptOrderIds' => Connection::PARAM_STR_ARRAY,
  94.         ]);
  95.         if (empty($data)) {
  96.             foreach ($customerIds as $customerId) {
  97.                 $data[] = [
  98.                     'id' => Uuid::fromHexToBytes($customerId),
  99.                     'order_count' => 0,
  100.                     'order_total_amount' => 0,
  101.                     'last_order_date' => null,
  102.                 ];
  103.             }
  104.         }
  105.         $update = new RetryableQuery(
  106.             $this->connection,
  107.             $this->connection->prepare('UPDATE `customer` SET order_count = :order_count, order_total_amount = :order_total_amount, last_order_date = :last_order_date WHERE id = :id')
  108.         );
  109.         foreach ($data as $record) {
  110.             $update->execute($record);
  111.         }
  112.     }
  113. }