src/StoreFront/Controller/Fragment/MykeyController.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Bodymed\Webshop\StoreFront\Controller\Fragment;
  4. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  7. use Shopware\Core\PlatformRequest;
  8. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Storefront\Controller\StorefrontController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16.  * @RouteScope(scopes={"storefront"})
  17.  */
  18. class MykeyController extends StorefrontController
  19. {
  20.     public function __construct(
  21.         private RequestStack $requestStack,
  22.         private SalesChannelRepository $salesChannelProductRepository
  23.     ) {
  24.     }
  25.     public function productButton(string $productId): Response
  26.     {
  27.         $mainRequest $this->requestStack->getMainRequest();
  28.         $currentRequest $this->requestStack->getCurrentRequest();
  29.         $salesChannelContext $this->getSalesChannelContext($mainRequest);
  30.         $productEntity $this->salesChannelProductRepository->search(new Criteria([$productId]), $salesChannelContext)->first();
  31.         $minimal false;
  32.         if ($salesChannelContext && $productEntity instanceof SalesChannelProductEntity) {
  33.             return $this->render('@MadBodymedTheme/storefront/fragment/mykey/product-button.html.twig', [
  34.                 'product' => $productEntity,
  35.             ]);
  36.         }
  37.         throw new \InvalidArgumentException();
  38.     }
  39.     public function bundleProductButton(string $productIdbool $bundleSelections): Response
  40.     {
  41.         $mainRequest $this->requestStack->getMainRequest();
  42.         $currentRequest $this->requestStack->getCurrentRequest();
  43.         $salesChannelContext $this->getSalesChannelContext($mainRequest);
  44.         $productEntity $this->salesChannelProductRepository->search(new Criteria([$productId]), $salesChannelContext)->first();
  45.         $minimal false;
  46.         if ($salesChannelContext && $productEntity instanceof SalesChannelProductEntity) {
  47.             return $this->render('@MadBodymedTheme/storefront/fragment/mykey/bundle-product-button.html.twig', [
  48.                 'product' => $productEntity,
  49.                 'bundleSelections' => $bundleSelections,
  50.             ]);
  51.         }
  52.         throw new \InvalidArgumentException();
  53.     }
  54.     private function getSalesChannelContext(Request $request): ?SalesChannelContext
  55.     {
  56.         return $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  57.     }
  58. }