<?php declare(strict_types=1);
namespace ThemeOkeOnline\Storefront\Page\Checkout;
use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
use Shopware\Core\Checkout\Shipping\SalesChannel\AbstractShippingMethodRoute;
use Shopware\Core\Content\Product\ProductCollection;
use Shopware\Core\Content\Product\SalesChannel\CrossSelling\AbstractProductCrossSellingRoute;
use Shopware\Core\Content\Product\SalesChannel\CrossSelling\ProductCrossSellingRouteResponse;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
class CheckoutRegisterPageLoadedEventSubscriber implements EventSubscriberInterface
{
private SalesChannelContext $salesChannelContext;
private Context $context;
private AbstractShippingMethodRoute $shippingMethodRoute;
private EntityRepositoryInterface $shippingMethodRepository;
public function __construct(
AbstractShippingMethodRoute $shippingMethodRoute,
EntityRepositoryInterface $shippingMethodRepository,
SystemConfigService $systemConfigService
)
{
$this->shippingMethodRoute = $shippingMethodRoute;
$this->shippingMethodRepository = $shippingMethodRepository;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutRegisterPageLoadedEvent::class => ['addShippingMethodsToPage']
];
}
public function addShippingMethodsToPage(CheckoutRegisterPageLoadedEvent $event)
{
$request = new Request();
$request->query->set('onlyAvailable', '1');
$salesChannelContext = $event->getSalesChannelContext();
$context = $salesChannelContext->getContext();
$page = $event->getPage();
$salesChannelId = $salesChannelContext->getSalesChannel()->id;
$criteria = new Criteria();
$criteria->addAssociation('salesChannels');
$criteria->addAssociation('prices');
$criteria->addFilter(
new EqualsFilter('active', true),
new EqualsFilter('salesChannels.id', $salesChannelId)
);
$shippingMethods = $this->shippingMethodRoute->load($request, $salesChannelContext, new Criteria())->getShippingMethods();
$page->addExtension(
'shippingMethods',
$shippingMethods
);
}
}