<?php
namespace CioRentalProductsCheckoutValidation\Subscriber;
use CioRentalProductsCheckoutValidation\Error\DifferentRentalDurationsError;
use Rhiem\RhiemRentalProducts\Components\RentalTime\RentalTime;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Cart\Cart;
class CheckoutValidationSubscriber implements EventSubscriberInterface
{
private ContainerInterface $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public static function getSubscribedEvents() : array
{
return [
StorefrontRenderEvent::class => 'onStorefrontRender'
];
}
public function onStorefrontRender(StorefrontRenderEvent $event)
{
$route = $event->getRequest()->get('_route');
if ($route == 'frontend.checkout.confirm.page') {
/** @var Cart $cart */
$cart = $event->getParameters()['page']->getCart();
/** @var LineItemCollection $lineItemCollection */
$lineItemCollection = $cart->getLineItems();
$rentDurationStartFirstLineItem = $this->getRentalDurationStart($lineItemCollection->first());
$rentDurationEndFirstLineItem = $this->getRentalDurationEnd($lineItemCollection->first());
$rentDurationStartFirstLineItem = $rentDurationStartFirstLineItem instanceof \DateTime ? $rentDurationStartFirstLineItem->format('Y-m-d H:i') : null;
$rentDurationEndFirstLineItem = $rentDurationEndFirstLineItem instanceof \DateTime ? $rentDurationEndFirstLineItem->format('Y-m-d H:i') : null;
/** @var LineItem $lineItem */
foreach ($lineItemCollection as $lineItem) {
$rentDurationStartCurrentLineItem = $this->getRentalDurationStart($lineItem);
$rentDurationEndCurrentLineItem = $this->getRentalDurationEnd($lineItem);
$rentDurationStartCurrentLineItem = $rentDurationStartCurrentLineItem instanceof \DateTime ? $rentDurationStartCurrentLineItem->format('Y-m-d H:i') : null;
$rentDurationEndCurrentLineItem = $rentDurationEndCurrentLineItem instanceof \DateTime ? $rentDurationEndCurrentLineItem->format('Y-m-d H:i') : null;
if ($rentDurationStartCurrentLineItem !== $rentDurationStartFirstLineItem || $rentDurationEndCurrentLineItem !== $rentDurationEndFirstLineItem) {
$cart->addErrors(new DifferentRentalDurationsError());
$event->getRequest()->getSession()->getFlashBag()->add('danger', 'Um die Bestellung bestätigen zu können muss für alle Mietartikel der identische Mietzeitraum ausgewählt sein.');
return;
}
}
}
}
protected function getRentalDurationStart(LineItem $lineItem)
{
if (key_exists('rentalProduct', $lineItem->getPayload()) && is_array($lineItem->getPayload()['rentalProduct']) && key_exists('rentalTime', $lineItem->getPayload()['rentalProduct']) && $lineItem->getPayload()['rentalProduct']['rentalTime'] instanceof RentalTime) {
/** @var RentalTime $rentalTime */
$rentalTime = $lineItem->getPayload()['rentalProduct']['rentalTime'];
return $rentalTime->getStartDate();
}
return null;
}
protected function getRentalDurationEnd(LineItem $lineItem)
{
if (key_exists('rentalProduct', $lineItem->getPayload()) && is_array($lineItem->getPayload()['rentalProduct']) && key_exists('rentalTime', $lineItem->getPayload()['rentalProduct']) && $lineItem->getPayload()['rentalProduct']['rentalTime'] instanceof RentalTime) {
/** @var RentalTime $rentalTime */
$rentalTime = $lineItem->getPayload()['rentalProduct']['rentalTime'];
return $rentalTime->getEndDate();
}
return null;
}
}