custom/plugins/ZeobvCartRecovery/src/Core/Subscriber/CartConvertedSubscriber.php line 78

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Zeobv\CartRecovery\Core\Subscriber;
  3. use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  6. use Shopware\Core\Checkout\Cart\Order\OrderConverter;
  7. use Shopware\Core\Checkout\Cart\CartPersisterInterface;
  8. use Shopware\Core\Framework\Uuid\Uuid;
  9. use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryCollection;
  10. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Checkout\Order\OrderEntity;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  15. use Shopware\Core\Framework\Context;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  17. use Shopware\Core\Checkout\Order\Aggregate\OrderDeliveryPosition\OrderDeliveryPositionCollection;
  18. /**
  19.  * Class CartConvertedSubscriber
  20.  *
  21.  * @package Zeobv\CartRecovery\Core\Subscriber
  22.  */
  23. class CartConvertedSubscriber implements EventSubscriberInterface
  24. {
  25.     /**
  26.      * @var OrderConverter
  27.      */
  28.     protected $orderConverter;
  29.     /**
  30.      * @var CartPersisterInterface
  31.      */
  32.     protected $cartPersister;
  33.     /**
  34.      * @var AbstractSalesChannelContextFactory
  35.      */
  36.     protected $salesChannelContextFactory;
  37.     /**
  38.      * @var EntityRepositoryInterface
  39.      */
  40.     protected $orderDeliveryRepository;
  41.     /**
  42.      * @var EntityRepositoryInterface
  43.      */
  44.     protected $orderDeliveryPositionsRepository;
  45.     public function __construct(
  46.         OrderConverter $orderConverter,
  47.         CartPersisterInterface $cartPersister,
  48.         AbstractSalesChannelContextFactory $salesChannelContextFactory,
  49.         EntityRepositoryInterface $orderDeliveryRepository,
  50.         EntityRepositoryInterface $orderDeliveryPositionsRepository
  51.     )
  52.     {
  53.         $this->orderConverter $orderConverter;
  54.         $this->cartPersister $cartPersister;
  55.         $this->salesChannelContextFactory $salesChannelContextFactory;
  56.         $this->orderDeliveryRepository $orderDeliveryRepository;
  57.         $this->orderDeliveryPositionsRepository $orderDeliveryPositionsRepository;
  58.     }
  59.     /**
  60.      * @return array|string[]
  61.      */
  62.     public static function getSubscribedEvents()
  63.     {
  64.         return [
  65.             CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlaced'
  66.         ];
  67.     }
  68.     public function onCheckoutOrderPlaced(CheckoutOrderPlacedEvent $event)
  69.     {
  70.         $order $event->getOrder();
  71.         if ($order->getDeliveries()->count() < 1) {
  72.             $deliveries =  $this->getOrderDeliveries($order$event->getContext());
  73.         } else {
  74.             $deliveries $this->setDeliveryPositionsForOrderDeliveries($order->getDeliveries(), $event->getContext());
  75.         }
  76.         $order->setDeliveries($deliveries);
  77.         $cart $this->orderConverter->convertToCart($order$event->getContext());
  78.         $salesChannelContext $this->salesChannelContextFactory->create(Uuid::randomHex(), $event->getSalesChannelId(), [
  79.             SalesChannelContextService::CUSTOMER_ID => $order->getOrderCustomer()->getCustomerId()
  80.         ]);
  81.         $cart->setToken($order->getId());
  82.         $cart->setName('cart_waiting_for_recovery');
  83.         $this->cartPersister->save($cart$salesChannelContext);
  84.     }
  85.     /**
  86.      * @param OrderEntity $order
  87.      * @param Context     $context
  88.      *
  89.      * @return OrderDeliveryCollection
  90.      */
  91.     private function getOrderDeliveries(OrderEntity $orderContext $context): OrderDeliveryCollection
  92.     {
  93.         $criteria = new Criteria();
  94.         $criteria->addFilter(new EqualsFilter('orderId'$order->getId()));
  95.         $criteria->addAssociations([
  96.             'positions.orderLineItem',
  97.             'shippingOrderAddress.country',
  98.             'shippingMethod',
  99.             'state',
  100.         ]);
  101.         /** @var OrderDeliveryCollection $deliveries */
  102.         $deliveries $this->orderDeliveryRepository->search($criteria$context)->getEntities();
  103.         return $deliveries;
  104.     }
  105.     private function setDeliveryPositionsForOrderDeliveries(OrderDeliveryCollection $deliveriesContext $context)
  106.     {
  107.         if ($deliveries->count() < 1) {
  108.             return $deliveries;
  109.         }
  110.         $criteria = new Criteria();
  111.         $criteria->addFilter(new EqualsAnyFilter('orderDeliveryId'$deliveries->getIds()));
  112.         $criteria->addAssociations(['orderLineItem']);
  113.         $positions $this->orderDeliveryPositionsRepository->search($criteria$context)->getEntities();
  114.         foreach ($deliveries as $delivery) {
  115.             /** @var OrderDeliveryPositionCollection $positionsForDelivery */
  116.             $positionsForDelivery $positions->filterByProperty('orderDeliveryId'$delivery->getId());
  117.             $delivery->setPositions($positionsForDelivery);
  118.         }
  119.         return $deliveries;
  120.     }
  121. }