custom/plugins/IabTheme/src/Service/CheckoutSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace IabTheme\Service;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  7. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  8. use Shopware\Storefront\Page\PageLoadedEvent;
  9. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  10. use Shopware\Core\Content\Product\Aggregate\ProductTranslation\ProductTranslationDefinition;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPage;
  13. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPage;
  14. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
  15. class CheckoutSubscriber implements EventSubscriberInterface
  16. {
  17.     private EntityRepository $productRepository;
  18.     public function __construct(
  19.         EntityRepository $productRepository
  20.     ) {
  21.         $this->productRepository $productRepository;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             CheckoutCartPageLoadedEvent::class => 'addQuantityUnits',
  27.             CheckoutConfirmPageLoadedEvent::class => 'addQuantityUnits',
  28.             OffcanvasCartPageLoadedEvent::class => 'addQuantityUnits',
  29.         ];
  30.     }
  31.     public function addQuantityUnits(PageLoadedEvent $pageLoadedEvent): void
  32.     {
  33.         /** @var CheckoutCartPage|CheckoutConfirmPage|OffcanvasCartPage $page */
  34.         $page $pageLoadedEvent->getPage();
  35.         $lineItems $page->getCart()->getLineItems();
  36.         $context $pageLoadedEvent->getSalesChannelContext()->getContext();
  37.         if ($lineItems) {
  38.             $ids $lineItems->getReferenceIds();
  39.             $criteria = new Criteria($ids);
  40.             $criteria->addAssociation(ProductTranslationDefinition::ENTITY_NAME);
  41.             $products $this->productRepository->search($criteria$context);
  42.             $page->addExtension('productsWithTranslations'$products);
  43.         }
  44.     }
  45. }