<?php
declare(strict_types=1);
namespace Bodymed\Webshop\StoreFront\Controller\Fragment;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\PlatformRequest;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @RouteScope(scopes={"storefront"})
*/
class MykeyController extends StorefrontController
{
public function __construct(
private RequestStack $requestStack,
private SalesChannelRepository $salesChannelProductRepository
) {
}
public function productButton(string $productId): Response
{
$mainRequest = $this->requestStack->getMainRequest();
$currentRequest = $this->requestStack->getCurrentRequest();
$salesChannelContext = $this->getSalesChannelContext($mainRequest);
$productEntity = $this->salesChannelProductRepository->search(new Criteria([$productId]), $salesChannelContext)->first();
$minimal = false;
if ($salesChannelContext && $productEntity instanceof SalesChannelProductEntity) {
return $this->render('@MadBodymedTheme/storefront/fragment/mykey/product-button.html.twig', [
'product' => $productEntity,
]);
}
throw new \InvalidArgumentException();
}
public function bundleProductButton(string $productId, bool $bundleSelections): Response
{
$mainRequest = $this->requestStack->getMainRequest();
$currentRequest = $this->requestStack->getCurrentRequest();
$salesChannelContext = $this->getSalesChannelContext($mainRequest);
$productEntity = $this->salesChannelProductRepository->search(new Criteria([$productId]), $salesChannelContext)->first();
$minimal = false;
if ($salesChannelContext && $productEntity instanceof SalesChannelProductEntity) {
return $this->render('@MadBodymedTheme/storefront/fragment/mykey/bundle-product-button.html.twig', [
'product' => $productEntity,
'bundleSelections' => $bundleSelections,
]);
}
throw new \InvalidArgumentException();
}
private function getSalesChannelContext(Request $request): ?SalesChannelContext
{
return $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
}
}