<?php
declare(strict_types=1);
namespace IabTheme\Service;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Shopware\Core\Content\Product\Aggregate\ProductTranslation\ProductTranslationDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPage;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPage;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
class CheckoutSubscriber implements EventSubscriberInterface
{
private EntityRepository $productRepository;
public function __construct(
EntityRepository $productRepository
) {
$this->productRepository = $productRepository;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutCartPageLoadedEvent::class => 'addQuantityUnits',
CheckoutConfirmPageLoadedEvent::class => 'addQuantityUnits',
OffcanvasCartPageLoadedEvent::class => 'addQuantityUnits',
];
}
public function addQuantityUnits(PageLoadedEvent $pageLoadedEvent): void
{
/** @var CheckoutCartPage|CheckoutConfirmPage|OffcanvasCartPage $page */
$page = $pageLoadedEvent->getPage();
$lineItems = $page->getCart()->getLineItems();
$context = $pageLoadedEvent->getSalesChannelContext()->getContext();
if ($lineItems) {
$ids = $lineItems->getReferenceIds();
$criteria = new Criteria($ids);
$criteria->addAssociation(ProductTranslationDefinition::ENTITY_NAME);
$products = $this->productRepository->search($criteria, $context);
$page->addExtension('productsWithTranslations', $products);
}
}
}