custom/plugins/EnetSubshopRequired/src/Subscriber/OrderPlacedSubscriber.php line 112

Open in your IDE?
  1. <?php
  2. namespace EnetSubshopRequired\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  4. use Shopware\Core\Checkout\Order\OrderEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  11. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPage;
  12. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Shopware\Core\Checkout\Order\OrderEvents;
  15. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  16. use Symfony\Component\HttpFoundation\Request;
  17. class OrderPlacedSubscriber implements EventSubscriberInterface
  18. {
  19.     private $logger;
  20.     /** @var \Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $orderRepository */
  21.     private $orderRepository;
  22.     /** @var \Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $orderRepository */
  23.     private $salesChannelRepository;
  24.     /** @var \Symfony\Component\HttpFoundation\RequestStack $requestStack */
  25.     private $requestStack;
  26.     /** @var \Symfony\Component\DependencyInjection\Container $container */
  27.     private $container;
  28.     public function __construct($logger$orderRepository$salesChannelRepository$requestStack$container)
  29.     {
  30.         $this->logger $logger;
  31.         $this->orderRepository $orderRepository;
  32.         $this->salesChannelRepository $salesChannelRepository;
  33.         $this->requestStack $requestStack;
  34.         $this->container $container;
  35.     }
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.             OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderPlaced',
  40.             OrderEvents::ORDER_LOADED_EVENT => 'onOrderLoaded',
  41. //            CheckoutOrderPlacedEvent::EVENT_NAME => 'onOrderPlaced',
  42. //            CheckoutFinishPageLoadedEvent::class => 'onOrderPlaced',
  43. //            'checkout.order.placed' => 'onOrderPlaced'
  44. //            FinishPageEvent::class
  45. //            HeaderPageletLoadedEvent::class => 'onOrderPlaced'
  46.         ];
  47.     }
  48.     public function onOrderLoaded(EntityLoadedEvent $event)
  49.     {
  50. //        $this->container->get('router')->generate('hier der route der derzeitigen seite');
  51. //         TODO:
  52. //            über seperates plugin muss noch die angabe eines zeitraums erlaubt werden
  53. //            dieses plugin prüft of ob alle arikel am lager sind, und erlaubt ein angeben von Datum + Uhrzeit
  54. //            mindestabholzeit ist 20min, falls der artikel nicht mehr am lager ist (POS) dann wird er als "fehlerhaft" markiert
  55. //            und der artikel könnte erst am nächsten tag abgeholt werden bzw. aus der bestellung entfernt werden
  56. //
  57. //
  58. //        $request = $this->requestStack->getCurrentRequest();
  59. //        $route = $request->attributes->get('_route');
  60. //        $context = $event->getContext();
  61. //
  62. //        $allowedRoutes = [
  63. //            'frontend.checkout.finish.order'
  64. //        ];
  65. //
  66. //        if(!in_array($route, $allowedRoutes)) {
  67. //            return;
  68. //        }
  69. //
  70. //        $subshopId = $request->request->get('enet_subshop', false);
  71. //        $salesChannelId = $context->getSource()->getSalesChannelId();
  72. //        // prüfen ob $salesChannelId nicht main oder master ist, prüfen ob mit der saleschannelId auch gekauft werden darf
  73. //        $criteria = new Criteria();
  74. //        $criteria->addFilter(new EqualsFilter('sales_channel.id', $salesChannelId));
  75. //        $criteria->addFilter(new ContainsFilter('sales_channel.translations.customFields', '"enet_adress_showasselection": "1"'));
  76. //        $criteria->addAssociation('type_translation');
  77. //        $channelResult = $this->salesChannelRepository->search($criteria, $context);
  78. //
  79. //        $channel = $channelResult->getElements();
  80. //        // no subshopId should never trigger
  81. //        if(!$subshopId || count($channel) === 0) {
  82. //            dump('keine subshop id gefunden');
  83. //        }
  84. //
  85. //        $criteria = new Criteria();
  86. //        $criteria->addFilter(new EqualsFilter('sales_channel.active', true));
  87. //        $criteria->addFilter(new EqualsFilter('sales_channel.maintenance', false));
  88. //        $criteria->addFilter(new ContainsFilter('sales_channel.translations.customFields', '"enet_adress_showasselection": "1"'));
  89. //        $channels = $this->salesChannelRepository->search($criteria, $context);
  90. //        dump($context);
  91. ////        f6a09ace160848f7bcdb871ceb09f214
  92. //
  93. //        die();
  94.     }
  95.     public function onOrderPlaced(EntityWrittenEvent $event)
  96.     {
  97.         $context $event->getContext();
  98.         foreach ($event->getWriteResults() as $writeResult) {
  99.             if ($writeResult->getExistence() !== null && $writeResult->getExistence()->exists()) {
  100.                 break;
  101.             }
  102.             $payload $writeResult->getPayload();
  103.             if (empty($payload)) {
  104.                 continue;
  105.             }
  106.             $orderResult $this->orderRepository->search(
  107.                 new Criteria([$payload['id']]),
  108.                 $context
  109.             );
  110.             /** @var OrderEntity|null $order */
  111.             $order $orderResult->first();
  112.             // es muss hier zusätzlich noch der sales_channel geholt werden
  113.             //
  114.             if (!($order instanceof OrderEntity)) {
  115.                 continue;
  116.             }
  117.             $request $this->requestStack->getCurrentRequest();
  118.             $subshopId $request->request->get('enet_subshop'false);
  119.             if(!$subshopId) {
  120.                 // TODO: throw exception, when no store id is inserted
  121.                 continue;
  122.             }
  123.             $this->orderRepository->update(
  124.                 [
  125.                     [
  126.                         'id' => $payload['id'],
  127.                         'salesChannelId' => $subshopId
  128.                     ]
  129.                 ],
  130.                 $context);
  131.         }
  132.     }
  133. }