<?php
namespace EnetSubshopRequired\Subscriber;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPage;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\System\SalesChannel\SalesChannelEntity;
use Symfony\Component\HttpFoundation\Request;
class OrderPlacedSubscriber implements EventSubscriberInterface
{
private $logger;
/** @var \Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $orderRepository */
private $orderRepository;
/** @var \Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $orderRepository */
private $salesChannelRepository;
/** @var \Symfony\Component\HttpFoundation\RequestStack $requestStack */
private $requestStack;
/** @var \Symfony\Component\DependencyInjection\Container $container */
private $container;
public function __construct($logger, $orderRepository, $salesChannelRepository, $requestStack, $container)
{
$this->logger = $logger;
$this->orderRepository = $orderRepository;
$this->salesChannelRepository = $salesChannelRepository;
$this->requestStack = $requestStack;
$this->container = $container;
}
public static function getSubscribedEvents()
{
return [
OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderPlaced',
OrderEvents::ORDER_LOADED_EVENT => 'onOrderLoaded',
// CheckoutOrderPlacedEvent::EVENT_NAME => 'onOrderPlaced',
// CheckoutFinishPageLoadedEvent::class => 'onOrderPlaced',
// 'checkout.order.placed' => 'onOrderPlaced'
// FinishPageEvent::class
// HeaderPageletLoadedEvent::class => 'onOrderPlaced'
];
}
public function onOrderLoaded(EntityLoadedEvent $event)
{
// $this->container->get('router')->generate('hier der route der derzeitigen seite');
// TODO:
// über seperates plugin muss noch die angabe eines zeitraums erlaubt werden
// dieses plugin prüft of ob alle arikel am lager sind, und erlaubt ein angeben von Datum + Uhrzeit
// mindestabholzeit ist 20min, falls der artikel nicht mehr am lager ist (POS) dann wird er als "fehlerhaft" markiert
// und der artikel könnte erst am nächsten tag abgeholt werden bzw. aus der bestellung entfernt werden
//
//
// $request = $this->requestStack->getCurrentRequest();
// $route = $request->attributes->get('_route');
// $context = $event->getContext();
//
// $allowedRoutes = [
// 'frontend.checkout.finish.order'
// ];
//
// if(!in_array($route, $allowedRoutes)) {
// return;
// }
//
// $subshopId = $request->request->get('enet_subshop', false);
// $salesChannelId = $context->getSource()->getSalesChannelId();
// // prüfen ob $salesChannelId nicht main oder master ist, prüfen ob mit der saleschannelId auch gekauft werden darf
// $criteria = new Criteria();
// $criteria->addFilter(new EqualsFilter('sales_channel.id', $salesChannelId));
// $criteria->addFilter(new ContainsFilter('sales_channel.translations.customFields', '"enet_adress_showasselection": "1"'));
// $criteria->addAssociation('type_translation');
// $channelResult = $this->salesChannelRepository->search($criteria, $context);
//
// $channel = $channelResult->getElements();
// // no subshopId should never trigger
// if(!$subshopId || count($channel) === 0) {
// dump('keine subshop id gefunden');
// }
//
// $criteria = new Criteria();
// $criteria->addFilter(new EqualsFilter('sales_channel.active', true));
// $criteria->addFilter(new EqualsFilter('sales_channel.maintenance', false));
// $criteria->addFilter(new ContainsFilter('sales_channel.translations.customFields', '"enet_adress_showasselection": "1"'));
// $channels = $this->salesChannelRepository->search($criteria, $context);
// dump($context);
//// f6a09ace160848f7bcdb871ceb09f214
//
// die();
}
public function onOrderPlaced(EntityWrittenEvent $event)
{
$context = $event->getContext();
foreach ($event->getWriteResults() as $writeResult) {
if ($writeResult->getExistence() !== null && $writeResult->getExistence()->exists()) {
break;
}
$payload = $writeResult->getPayload();
if (empty($payload)) {
continue;
}
$orderResult = $this->orderRepository->search(
new Criteria([$payload['id']]),
$context
);
/** @var OrderEntity|null $order */
$order = $orderResult->first();
// es muss hier zusätzlich noch der sales_channel geholt werden
//
if (!($order instanceof OrderEntity)) {
continue;
}
$request = $this->requestStack->getCurrentRequest();
$subshopId = $request->request->get('enet_subshop', false);
if(!$subshopId) {
// TODO: throw exception, when no store id is inserted
continue;
}
$this->orderRepository->update(
[
[
'id' => $payload['id'],
'salesChannelId' => $subshopId
]
],
$context);
}
}
}