custom/static-plugins/NdTheme/src/Storefront/Controller/RecentProductsController.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nd\Theme\Storefront\Controller;
  3. use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  7. use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
  8. use Shopware\Core\PlatformRequest;
  9. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Storefront\Controller\StorefrontController;
  12. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Twig\Environment;
  18. /**
  19.  * @RouteScope(scopes={"store-api"})
  20.  */
  21. //echo "test";
  22. class RecentProductsController extends StorefrontController
  23. {
  24.     private SalesChannelRepositoryInterface $productRepository;
  25.     private Environment $twig;
  26.     public function __construct(SalesChannelRepositoryInterface $productRepositoryEnvironment $twig)
  27.     {
  28.         $this->productRepository $productRepository;
  29.         $this->twig $twig;
  30.     }
  31.     /**
  32.      * @Route("/store-api/nd/recent-products", name="store-api.nd.recent.products", methods={"GET"})
  33.      */
  34.     public function getRecentProducts(Request $requestSalesChannelContext $salesChannelContext): Response
  35.     {
  36.         $response = new Response();
  37.         $request->get('productIds');
  38.         if (!$request->get('productIds')) {
  39.             $response $this->getResponseTemplate($request$salesChannelContext'@NdTheme/storefront/page/recent-products/none.html.twig', []);
  40.             $response->setStatusCode(204);
  41.             return $response;
  42.         }
  43.         $productIds explode(','$request->get('productIds'));
  44.         if (!is_array($productIds)) {
  45.             $response $this->getResponseTemplate($request$salesChannelContext'@NdTheme/storefront/page/recent-products/none.html.twig', []);
  46.             $response->setStatusCode(204);
  47.             return $response;
  48.         }
  49.         $criteria = new Criteria($productIds);
  50.         $criteria->addAssociation("media");
  51.         $criteria->getAssociation("cover");
  52.         $criteria->getAssociation("prices");
  53.         try {
  54.             $products $this->productRepository->search($criteria$salesChannelContext);
  55.         } catch (InvalidUuidException $exception) {
  56.             $response $this->getResponseTemplate($request$salesChannelContext'@NdTheme/storefront/page/recent-products/none.html.twig', []);
  57.             $response->setStatusCode(204);
  58.             return $response;
  59.         }
  60.         if ($products->count() == 0) {
  61.             return $this->getResponseTemplate($request$salesChannelContext'@NdTheme/storefront/page/recent-products/none.html.twig', []);
  62.         }
  63.         return $this->getResponseTemplate($request$salesChannelContext'@NdTheme/storefront/page/recent-products/products.html.twig', ['products' => $products->getEntities()]);
  64.     }
  65.     private function getResponseTemplate(Request $request$salesChannelContext$view$payload): Response
  66.     {
  67.         $response = new Response();
  68.         if (isset($this->twig)) {
  69.             $response->setContent($this->twig->render($view$payload));
  70.             $content $response->getContent();
  71.             if ($content !== false) {
  72.                 $seoUrlReplacer $this->get(SeoUrlPlaceholderHandlerInterface::class);
  73.                 $host $request->attributes->get(RequestTransformer::STOREFRONT_URL);
  74.                 $response->setContent(
  75.                     $seoUrlReplacer->replace($content$request->getSchemeAndHttpHost(), $salesChannelContext)
  76.                 );
  77.             }
  78.         }
  79.         return $response;
  80.     }
  81. }