vendor/shopware/storefront/Controller/ProductController.php line 77

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Content\Product\SalesChannel\ProductReviewService;
  4. use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
  5. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  6. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  7. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
  10. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  11. use Shopware\Storefront\Page\Product\Configurator\ProductCombinationFinder;
  12. use Shopware\Storefront\Page\Product\ProductPageLoader;
  13. use Shopware\Storefront\Page\Product\QuickView\MinimalQuickViewPageLoader;
  14. use Shopware\Storefront\Page\Product\Review\ProductReviewLoader;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * @RouteScope(scopes={"storefront"})
  21.  */
  22. class ProductController extends StorefrontController
  23. {
  24.     /**
  25.      * @var ProductPageLoader
  26.      */
  27.     private $productPageLoader;
  28.     /**
  29.      * @var ProductCombinationFinder
  30.      */
  31.     private $combinationFinder;
  32.     /**
  33.      * @var MinimalQuickViewPageLoader
  34.      */
  35.     private $minimalQuickViewPageLoader;
  36.     /**
  37.      * @var ProductReviewService
  38.      */
  39.     private $productReviewService;
  40.     /**
  41.      * @var SeoUrlPlaceholderHandlerInterface
  42.      */
  43.     private $seoUrlPlaceholderHandler;
  44.     /**
  45.      * @var ProductReviewLoader
  46.      */
  47.     private $productReviewLoader;
  48.     public function __construct(
  49.         ProductPageLoader $productPageLoader,
  50.         ProductCombinationFinder $combinationFinder,
  51.         MinimalQuickViewPageLoader $minimalQuickViewPageLoader,
  52.         ProductReviewService $productReviewService,
  53.         SeoUrlPlaceholderHandlerInterface $seoUrlPlaceholderHandler,
  54.         ProductReviewLoader $productReviewLoader
  55.     ) {
  56.         $this->productPageLoader $productPageLoader;
  57.         $this->combinationFinder $combinationFinder;
  58.         $this->minimalQuickViewPageLoader $minimalQuickViewPageLoader;
  59.         $this->productReviewService $productReviewService;
  60.         $this->seoUrlPlaceholderHandler $seoUrlPlaceholderHandler;
  61.         $this->productReviewLoader $productReviewLoader;
  62.     }
  63.     /**
  64.      * @HttpCache()
  65.      * @Route("/detail/{productId}", name="frontend.detail.page", methods={"GET"})
  66.      */
  67.     public function index(SalesChannelContext $contextRequest $request): Response
  68.     {
  69.         $page $this->productPageLoader->load($request$context);
  70.         $ratingSuccess $request->get('success');
  71.         return $this->renderStorefront('@Storefront/storefront/page/product-detail/index.html.twig', ['page' => $page'ratingSuccess' => $ratingSuccess]);
  72.     }
  73.     /**
  74.      * @HttpCache()
  75.      * @Route("/detail/{productId}/switch", name="frontend.detail.switch", methods={"GET"}, defaults={"XmlHttpRequest": true})
  76.      */
  77.     public function switch(string $productIdRequest $requestSalesChannelContext $salesChannelContext): JsonResponse
  78.     {
  79.         $switchedOption $request->query->get('switched');
  80.         $newOptions json_decode($request->query->get('options'), true);
  81.         $redirect $this->combinationFinder->find($productId$switchedOption$newOptions$salesChannelContext);
  82.         $host $request->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL)
  83.             . $request->attributes->get(RequestTransformer::SALES_CHANNEL_BASE_URL);
  84.         $url $this->seoUrlPlaceholderHandler->replace(
  85.             $this->seoUrlPlaceholderHandler->generate(
  86.                 'frontend.detail.page',
  87.                 ['productId' => $redirect->getVariantId()]
  88.             ),
  89.             $host,
  90.             $salesChannelContext
  91.         );
  92.         return new JsonResponse(['url' => $url]);
  93.     }
  94.     /**
  95.      * @Route("/quickview/{productId}", name="widgets.quickview.minimal", methods={"GET"}, defaults={"XmlHttpRequest": true})
  96.      */
  97.     public function quickviewMinimal(Request $requestSalesChannelContext $context): Response
  98.     {
  99.         $page $this->minimalQuickViewPageLoader->load($request$context);
  100.         return $this->renderStorefront('@Storefront/storefront/component/product/quickview/minimal.html.twig', ['page' => $page]);
  101.     }
  102.     /**
  103.      * @Route("/product/{productId}/rating", name="frontend.detail.review.save", methods={"POST"}, defaults={"XmlHttpRequest"=true})
  104.      */
  105.     public function saveReview(string $productIdRequestDataBag $dataSalesChannelContext $context): Response
  106.     {
  107.         $this->denyAccessUnlessLoggedIn();
  108.         try {
  109.             $this->productReviewService->save($productId$data$context);
  110.         } catch (ConstraintViolationException $formViolations) {
  111.             return $this->forwardToRoute('frontend.product.reviews', [
  112.                 'productId' => $productId,
  113.                 'success' => -1,
  114.                 'formViolations' => $formViolations,
  115.                 'data' => $data,
  116.             ], ['productId' => $productId]);
  117.         }
  118.         $forwardParams = [
  119.             'productId' => $productId,
  120.             'success' => 1,
  121.             'data' => $data,
  122.             'parentId' => $data->get('parentId'),
  123.         ];
  124.         if ($data->has('id')) {
  125.             $forwardParams['success'] = 2;
  126.         }
  127.         return $this->forwardToRoute('frontend.product.reviews'$forwardParams, ['productId' => $productId]);
  128.     }
  129.     /**
  130.      * @Route("/product/{productId}/reviews", name="frontend.product.reviews", methods={"GET","POST"}, defaults={"XmlHttpRequest"=true})
  131.      */
  132.     public function loadReviews(Request $requestRequestDataBag $dataSalesChannelContext $context): Response
  133.     {
  134.         $reviews $this->productReviewLoader->load($request$context);
  135.         return $this->renderStorefront('storefront/page/product-detail/review/review.html.twig', [
  136.             'reviews' => $reviews,
  137.             'ratingSuccess' => $request->get('success'),
  138.         ]);
  139.     }
  140. }