custom/plugins/WebkulAbandonedCart/src/Subscriber/AbandonedCartEventSubscriber.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Webkul\Abandoned\Subscriber;
  3. use Shopware\Core\Checkout\Order\OrderEvents;
  4. use Shopware\Core\Framework\Context;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. class AbandonedCartEventSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var EntityRepositoryInterface
  14.      */
  15.     protected $cartNotificationRepository;
  16.     /**
  17.      * @var EntityRepositoryInterface
  18.      */
  19.     protected $orderCustomerRepository;
  20.     public function __construct(EntityRepositoryInterface $cartNotificationRepositoryEntityRepositoryInterface $orderCustomerRepository)
  21.     {
  22.         $this->cartNotificationRepository $cartNotificationRepository;
  23.         $this->orderCustomerRepository $orderCustomerRepository;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             OrderEvents::ORDER_WRITTEN_EVENT => 'updateAbandonedCart'
  29.         ];
  30.     }
  31.     public function updateAbandonedCart(EntityWrittenEvent $event)
  32.     {
  33.         $results $event->getWriteResults();
  34.         if ($results) {
  35.             foreach ($results as $result) {
  36.                 $payload $result->getPayload();
  37.                 $this->removeLastNotified($payload['id']);
  38.             }
  39.         }
  40.     }
  41.     private function removeLastNotified($orderId): void
  42.     {
  43.         $orderCustomerEntitites $this->orderCustomerRepository->search(
  44.             (new Criteria())->addFilter(new EqualsFilter('orderId'$orderId)),
  45.             Context::createDefaultContext()
  46.         );
  47.         if ($orderCustomerEntitites->getTotal()) {
  48.             $customerId $orderCustomerEntitites->first()->getCustomerId();
  49.             $notificationEntities $this->cartNotificationRepository->search(
  50.                 (new Criteria())->addFilter(new EqualsFilter('customerId'$customerId)),
  51.                 Context::createDefaultContext()
  52.             );
  53.             if ($notificationEntities->getTotal() > 0) {
  54.                 $id $notificationEntities->first()->getId();
  55.                 $this->cartNotificationRepository->delete([['id' => $id]], Context::createDefaultContext());
  56.             }
  57.         }
  58.     }
  59. }