custom/static-plugins/extended-customer-list/src/Subscriber/Customer/Checkout/CustomerOrderAmountSubscriber.php line 44

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wexo\ExtendedCustomerList\Subscriber\Customer\Checkout;
  3. use Wexo\ExtendedCustomerList\Helpers\Helper;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Checkout\Order\OrderDefinition;
  6. use Shopware\Core\Checkout\Order\OrderEntity;
  7. use Shopware\Core\Checkout\Order\OrderEvents;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class CustomerOrderAmountSubscriber implements EventSubscriberInterface
  14. {
  15.     private Helper $helper;
  16.     /**
  17.      * @param EntityRepositoryInterface $orderRepository
  18.      * @param EntityRepositoryInterface $customerRepository
  19.      */
  20.     public function __construct(
  21.         protected EntityRepositoryInterface $orderRepository,
  22.         protected EntityRepositoryInterface $customerRepository
  23.     ) {
  24.         $this->helper = new Helper($customerRepositoryContext::createDefaultContext());
  25.     }
  26.     /**
  27.      * @return array|string[]
  28.      */
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             OrderEvents::ORDER_WRITTEN_EVENT => 'fillCustomerMetaDataFields',
  33.         ];
  34.     }
  35.     /**
  36.      * @param EntityWrittenEvent $event
  37.      */
  38.     public function fillCustomerMetaDataFields(EntityWrittenEvent $event): void
  39.     {
  40.         if ($event->getEntityName() !== OrderDefinition::ENTITY_NAME) {
  41.             return;
  42.         }
  43.         $context $event->getContext();
  44.         foreach ($event->getWriteResults() as $writeResult) {
  45.             if ($writeResult->getExistence() !== null && $writeResult->getExistence()->exists()) {
  46.                 break;
  47.             }
  48.             $payload $writeResult->getPayload();
  49.             if (empty($payload)) {
  50.                 continue;
  51.             }
  52.             $orderResult $this->orderRepository->search(
  53.                 (new Criteria([$payload['id']]))->addAssociation('orderCustomer'),
  54.                 $context
  55.             );
  56.             /** @var OrderEntity|null $order */
  57.             $order $orderResult->first();
  58.             if (!($order instanceof OrderEntity)) {
  59.                 continue;
  60.             }
  61.             $customerId $order->getOrderCustomer()->getCustomerId();
  62.             // happens if the customer was deleted
  63.             if (!$customerId) {
  64.                 continue;
  65.             }
  66.             $this->helper->calculate($customerId);
  67.         }
  68.     }
  69. }