custom/plugins/HochwarthCheckoutCheckbox/src/Core/Checkout/CheckoutPageLoadedSubscriber.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace HochwarthCheckoutCheckbox\Core\Checkout;
  3. use HochwarthCheckoutCheckbox\Core\Content\HochwarthCheckbox\SalesChannel\AbstractHochwarthCheckboxRoute;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  10. use Shopware\Core\Framework\Struct\ArrayStruct;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class CheckoutPageLoadedSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var SystemConfigService
  19.      */
  20.     private SystemConfigService $configService;
  21.     private AbstractHochwarthCheckboxRoute $checkboxRoute;
  22.     public function __construct(
  23.         SystemConfigService $configService,
  24.         AbstractHochwarthCheckboxRoute $checkboxRoute
  25.     )
  26.     {
  27.         $this->configService $configService;
  28.         $this->checkboxRoute $checkboxRoute;
  29.     }
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded',
  34.         ];
  35.     }
  36.     public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
  37.     {
  38.         $checkboxes $this->checkboxRoute->load(
  39.             $event->getRequest(),
  40.             $event->getSalesChannelContext(),
  41.             new Criteria()
  42.         )->getCheckboxes();
  43.         if (\count($checkboxes) > 0) {
  44.             $event->getPage()->addExtension('checkboxes'$checkboxes);
  45.             return;
  46.         }
  47.         $lineItemIds $event->getPage()->getCart()->getLineItems()->getReferenceIds();
  48.         $configProducts $this->configService->get('HochwarthCheckoutCheckbox.config.selectedProducts');
  49.         if ($configProducts == null || \count($configProducts) === 0) {
  50.             $event->getPage()->addExtension('showCheckoutCheckbox', new ArrayStruct(['value' => true]));
  51.             return;
  52.         }
  53.         $productInCart = \count(array_intersect($configProducts$lineItemIds)) > true false;
  54.         $event->getPage()->addExtension('showCheckoutCheckbox', new ArrayStruct(['value' => $productInCart]));
  55.     }
  56. }