<?php declare(strict_types=1);
/**
* PremSoft
* Copyright © 2020 Premsoft - Sven Mittreiter
*
* @copyright Copyright (c) 2020, premsoft - Sven Mittreiter (http://www.premsoft.de)
* @author Sven Mittreiter <info@premsoft.de>
*/
namespace Prems\Plugin\PremsIndividualOffer6\Storefront\Page\Checkout\Subscriber;
use Prems\Plugin\PremsIndividualOffer6\Core\Offer\Storefront\ConfigService;
use Prems\Plugin\PremsIndividualOffer6\Core\Offer\Storefront\OfferService;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Prems\Plugin\PremsIndividualOffer6\Storefront\Page\Listing\ListingPageLoader;
class PageSubscriber implements EventSubscriberInterface
{
/**
* @var ConfigService
*/
private $configService;
/**
* @var OfferService $offerService
*/
private $offerService;
public function __construct(
ConfigService $configService,
OfferService $offerService
) {
$this->configService = $configService;
$this->offerService = $offerService;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutCartPageLoadedEvent::class => 'onCartPageLoadedEvent',
OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCartPageLoadedEvent',
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoadedEvent',
//CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoadedEvent',
];
}
/**
* @param CheckoutFinishPageLoadedEvent $event
*/
/**public function onCheckoutFinishPageLoadedEvent($event): void
{
$request = $event->getRequest();
$context = $event->getSalesChannelContext();
$offerSettings = null;
if ($event instanceof CheckoutFinishPageLoadedEvent) {
$offerSettings = $this->configService->getConfig($context);
} else {
return;
}
if (!$offerSettings) {
return;
}
if ($this->offerService->getOfferMode($context)) {
$offer = $this->offerService->getOfferById($this->offerService->getOfferMode($context), $event->getContext());
$this->offerService->disableOfferMode($context);
$this->offerService->acceptOffer($offer, $event->getContext());
}
}*/
/**
* @param CheckoutConfirmPageLoadedEvent $event
*
* @throws InconsistentCriteriaIdsException
*/
public function onCheckoutConfirmPageLoadedEvent(CheckoutConfirmPageLoadedEvent $event): void
{
$request = $event->getRequest();
$context = $event->getSalesChannelContext();
$offerSettings = $this->configService->getConfig($context);
if (!$offerSettings) {
return;
}
$offerMode = $this->offerService->getOfferMode($context, $request->getSession());
if ($offerMode) {
$customer = $context->getCustomer();
if (!$customer) {
return;
}
$offer = $this->offerService->getOfferById($offerMode, $context->getContext(), $customer);
if (!$offer) {
return;
}
$event->getPage()->assign(['offer' => $offer]);
$event->getPage()->assign(['offerMode' => $offerMode]);
}
$event->getPage()->addExtension('premsOffer', $offerSettings);
}
/**
* @param CheckoutCartPageLoadedEvent $event
*
* @throws InconsistentCriteriaIdsException
*/
public function onCartPageLoadedEvent(CheckoutCartPageLoadedEvent $event): void
{
$request = $event->getRequest();
$context = $event->getSalesChannelContext();
$offerSettings = $this->configService->getConfig($context);
if (!$offerSettings) {
return;
}
$offerMode = $this->offerService->getOfferMode($context, $request->getSession());
if ($offerMode) {
$customer = $context->getCustomer();
if (!$customer) {
return;
}
$offer = $this->offerService->getOfferById($offerMode, $context->getContext(), $customer);
if (!$offer) {
return;
}
$event->getPage()->assign(['offer' => $offer]);
$event->getPage()->assign(['offerMode' => $offerMode]);
}
$settingVars = $offerSettings->getVars();
if (!$settingVars['showOfferButtonInCart'] || $offerMode) {
return;
}
$event->getPage()->addExtension('premsOffer', $offerSettings);
}
/**
* @param OffcanvasCartPageLoadedEvent $event
*
* @throws InconsistentCriteriaIdsException
*/
public function onOffcanvasCartPageLoadedEvent(OffcanvasCartPageLoadedEvent $event): void
{
$request = $event->getRequest();
$context = $event->getSalesChannelContext();
$offerSettings = $this->configService->getConfig($context);
$offerMode = $this->offerService->getOfferMode($context, $request->getSession());
if ($offerMode) {
$event->getPage()->assign(['offerMode' => $offerMode]);
}
if (!$offerSettings) {
return;
}
$settingVars = $offerSettings->getVars();
if (!$settingVars['showOfferButtonInOffcanvasCart'] || $offerMode) {
return;
}
$event->getPage()->addExtension('premsOffer', $offerSettings);
}
}