<?php
namespace GlobusSW6\Subscriber;
use GlobusSW6\Service\ShippingTimeService;
use GlobusSW6\Service\StoreLocator\PickupTimeCalculator;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use GlobusSW6\Core\IaneoDefaults;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use GlobusSW6\Service\StoreLocator\Struct\PickupTimeStruct;
class CalculatePickupTimeSubscriber implements EventSubscriberInterface
{
/** @var PickupTimeCalculator */
private $pickupTimeCalculator;
/** @var SessionInterface */
private $session;
/**
* ShippingTimesDetailPage constructor.
* @param ShippingTimeService $shippingTimeService
*/
public function __construct(PickupTimeCalculator $pickupTimeCalculator, SessionInterface $session)
{
$this->pickupTimeCalculator = $pickupTimeCalculator;
$this->session = $session;
}
public static function getSubscribedEvents()
{
return [
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded',
CartConvertedEvent::class => 'onCartConverted'
];
}
public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event)
{
//TODO SMN Ist das sicher?
$storeId = $this->session->get('ianeoCurrentStore');
if (empty($storeId))
{
return;
}
$holzzuschnitt = false;
if(!empty($event->getPage()->getCart()->getExtension("acid_holzzuschnitt")))
{
$holzzuschnitt = true;
}
$pickuptime = $this->pickupTimeCalculator->calculate(
$storeId,
IaneoDefaults::PICKUPTIME,
NULL,
false,
$holzzuschnitt,
$event->getSalesChannelContext()->getContext()
);
$event->getPage()->getCart()->removeExtension("ianeo_pickup_time");
$cart = $event->getPage()->getCart();
if($pickuptime) {
$cart->addExtension(
"ianeo_pickup_time",new PickupTimeStruct($pickuptime->format('d.m.Y H:i'))
);
} else {
// Es konnte keine Abholzeit für den Store ermittelt werden
$cart->addExtension("ianeo_pickup_time", null);
}
$event->getPage()->setCart($cart);
}
public function onCartConverted(CartConvertedEvent $event)
{
$convertedCart = $event->getConvertedCart();
//TODO SMN Ist das sicher?
$storeId = $this->session->get('ianeoCurrentStore');
if (empty($storeId))
{
return;
}
$holzzuschnitt = false;
if(!empty($event->getCart()->getExtension("acid_holzzuschnitt")))
{
$holzzuschnitt = true;
}
$pickuptime = $this->pickupTimeCalculator->calculate(
$storeId,
IaneoDefaults::PICKUPTIME,
NULL,
false,
$holzzuschnitt,
$event->getSalesChannelContext()->getContext()
);
if(!empty($convertedCart))
{
if($pickuptime) {
$convertedCart['ianeoAttributes'] = [
'pickupDatetime' => $pickuptime->format('d.m.Y H:i')
];
}
}
$event->setConvertedCart($convertedCart);
}
}