<?php
/**
* PremSoft
* Copyright © 2021 Premsoft - Sven Mittreiter
*
* @copyright Copyright (c) 2021, premsoft - Sven Mittreiter (http://www.premsoft.de)
* @author Sven Mittreiter <info@premsoft.de>
*/
namespace Prems\Plugin\PremsIndividualOffer6\Subscriber;
use Prems\Plugin\PremsIndividualOffer6\Core\Offer\Storefront\ConfigService;
use Prems\Plugin\PremsIndividualOffer6\Core\Offer\Storefront\OfferService;
use Prems\Plugin\PremsIndividualOffer6\PremsIndividualOffer6;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Shopware\Core\Checkout\Cart\Order\OrderConvertedEvent;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextRestorer;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class OrderConvertedSubscriber implements EventSubscriberInterface
{
private OfferService $offerService;
private RequestStack $requestStack;
private SalesChannelContextRestorer $salesChannelContextRestorer;
public function __construct(
OfferService $offerService,
SalesChannelContextRestorer $salesChannelContextRestorer,
RequestStack $requestStack
)
{
$this->offerService = $offerService;
$this->requestStack = $requestStack;
$this->salesChannelContextRestorer = $salesChannelContextRestorer;
}
public static function getSubscribedEvents(): array
{
return [
CartConvertedEvent::class => 'onCartConvertedEvent',
CheckoutOrderPlacedEvent::class => ['resetToOriginalCart', -1000],
OrderConvertedEvent::class => 'onOrderConvertedEvent',
];
}
/**
* Flag offer as ordered. Close offer mode
* @param CartConvertedEvent $event
*/
public function onCartConvertedEvent(CartConvertedEvent $event): void
{
$salesChannelContext = $event->getSalesChannelContext();
$request = $this->requestStack->getMainRequest();
if (!$request instanceof RequestStack) {
return;
}
$session = $request->getSession();
if ($offerMode = $this->offerService->getOfferMode($salesChannelContext, $session)) {
$offer = $this->offerService->getOfferById($offerMode, $event->getContext());
$this->offerService->disableOfferMode($salesChannelContext);
$this->offerService->acceptOffer($offer, $event->getContext());
$customFields = $event->getConvertedCart()['customFields'] ?? [];
$customFields[PremsIndividualOffer6::ORDER_FROM_OFFER] = true;
$customFields[PremsIndividualOffer6::ORDER_FROM_OFFER_ID] = $offer->getId();
$cartData = $event->getOriginalConvertedCart();
$cartData['customFields'] = $customFields;
$event->setConvertedCart($cartData);
}
}
public function resetToOriginalCart(CheckoutOrderPlacedEvent $event): void
{
if (method_exists($this->salesChannelContextRestorer,'restoreByOrder')) {
$salesChannelContext = $this->salesChannelContextRestorer->restoreByOrder($event->getOrderId(), $event->getContext());
$request = $this->requestStack->getMainRequest();
if (!$request instanceof RequestStack) {
return;
}
$session = $request->getSession();
$this->offerService->restoreBackupCart($salesChannelContext, $session);
}
}
public function onOrderConvertedEvent(OrderConvertedEvent $event): void
{
$customFields = $event->getOrder()->getCustomFields() ?? [];
if (isset($customFields[PremsIndividualOffer6::ORDER_FROM_OFFER])) {
$event->getConvertedCart()->addExtension(PremsIndividualOffer6::OFFER_MODE_EXTENSION_NAME, new ArrayEntity(['id' => $customFields[PremsIndividualOffer6::ORDER_FROM_OFFER_ID]]));
}
}
}