<?php declare(strict_types=1);
namespace HochwarthCheckoutCheckbox\Core\Checkout;
use HochwarthCheckoutCheckbox\Core\Content\HochwarthCheckbox\SalesChannel\AbstractHochwarthCheckboxRoute;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CheckoutPageLoadedSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private SystemConfigService $configService;
private AbstractHochwarthCheckboxRoute $checkboxRoute;
public function __construct(
SystemConfigService $configService,
AbstractHochwarthCheckboxRoute $checkboxRoute
)
{
$this->configService = $configService;
$this->checkboxRoute = $checkboxRoute;
}
public static function getSubscribedEvents()
{
return [
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded',
];
}
public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
{
$checkboxes = $this->checkboxRoute->load(
$event->getRequest(),
$event->getSalesChannelContext(),
new Criteria()
)->getCheckboxes();
if (\count($checkboxes) > 0) {
$event->getPage()->addExtension('checkboxes', $checkboxes);
return;
}
$lineItemIds = $event->getPage()->getCart()->getLineItems()->getReferenceIds();
$configProducts = $this->configService->get('HochwarthCheckoutCheckbox.config.selectedProducts');
if ($configProducts == null || \count($configProducts) === 0) {
$event->getPage()->addExtension('showCheckoutCheckbox', new ArrayStruct(['value' => true]));
return;
}
$productInCart = \count(array_intersect($configProducts, $lineItemIds)) > 0 ? true : false;
$event->getPage()->addExtension('showCheckoutCheckbox', new ArrayStruct(['value' => $productInCart]));
}
}