vendor/store.shopware.com/zeobvabandonedcart/src/Core/Subscriber/OrderSubscriber.php line 52

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Zeobv\AbandonedCart\Core\Subscriber;
  3. use Shopware\Core\Checkout\Order\OrderDefinition;
  4. use Shopware\Core\Checkout\Order\OrderEntity;
  5. use Shopware\Core\Checkout\Order\OrderEvents;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Zeobv\AbandonedCart\Service\AbandonedCartService;
  14. class OrderSubscriber implements EventSubscriberInterface
  15. {
  16.     protected AbandonedCartService $abandonedCartService;
  17.     protected EntityRepositoryInterface $orderRepository;
  18.     protected EntityRepositoryInterface $abandonedCartRepository;
  19.     /**
  20.      * @param AbandonedCartService      $abandonedCartService
  21.      * @param EntityRepositoryInterface $orderRepository
  22.      * @param EntityRepositoryInterface $abandonedCartRepository
  23.      */
  24.     public function __construct(
  25.         AbandonedCartService $abandonedCartService,
  26.         EntityRepositoryInterface $orderRepository,
  27.         EntityRepositoryInterface $abandonedCartRepository
  28.     )
  29.     {
  30.         $this->abandonedCartService $abandonedCartService;
  31.         $this->orderRepository $orderRepository;
  32.         $this->abandonedCartRepository $abandonedCartRepository;
  33.     }
  34.     /**
  35.      * @return array
  36.      */
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderWritten',
  41.         ];
  42.     }
  43.     /**
  44.      * @param EntityWrittenEvent $event
  45.      */
  46.     public function onOrderWritten(EntityWrittenEvent $event): void
  47.     {
  48.         foreach ($event->getWriteResults() as $writeResult) {
  49.             if ($writeResult->getOperation() !== EntityWriteResult::OPERATION_INSERT
  50.                 || $writeResult->getEntityName() !== OrderDefinition::ENTITY_NAME
  51.             ) {
  52.                 continue;
  53.             }
  54.             if (is_array($writeResult->getPrimaryKey())) {
  55.                 foreach ($writeResult->getPrimaryKey() as $id) {
  56.                     $this->resolveAbandonedCartForOrderId($id$event->getContext());
  57.                 }
  58.             } else {
  59.                 $this->resolveAbandonedCartForOrderId($writeResult->getPrimaryKey(), $event->getContext());
  60.             }
  61.         }
  62.     }
  63.     /**
  64.      * @param string  $orderId
  65.      * @param Context $context
  66.      */
  67.     protected function resolveAbandonedCartForOrderId(string $orderIdContext $context): void
  68.     {
  69.         $criteria = new Criteria([$orderId]);
  70.         $criteria->addAssociation('orderCustomer');
  71.         /** @var OrderEntity|null $order */
  72.         $order $this->orderRepository->search($criteria$context)->first();
  73.         if ($order === null || $order->getOrderCustomer() === null) {
  74.             return;
  75.         }
  76.         $criteria = new Criteria();
  77.         $criteria->addFilter(new EqualsFilter('email'$order->getOrderCustomer()->getEmail()));
  78.         $abandonedCart $this->abandonedCartRepository->search($criteria$context)->first();
  79.         if ($abandonedCart === null) {
  80.             return;
  81.         }
  82.         /* Here we are upsert the data on order table for displaying the recovered orders in dashboard */
  83.         if ($abandonedCart->getIsRecovered() == true) {
  84.             $writeData = [
  85.                 'id' => $orderId,
  86.                 'customFields' => ['ZeobvAbandonedCartMail' => 1],
  87.             ];
  88.             $this->orderRepository->upsert([$writeData], Context::createDefaultContext());
  89.         }
  90.         $this->abandonedCartService->resolveAbandonedCart($abandonedCart$context);
  91.     }
  92. }