src/Controller/ProductController.php line 372

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\DataMapper\Product\ProductDetailDataMapper;
  4. use App\DataMapper\Product\ProductListingDataMapper;
  5. use App\Model\Product\Product;
  6. use App\Repository\ProductRepository;
  7. use App\Website\LinkGenerator\ProductLinkGenerator;
  8. use Knp\Component\Pager\Pagination\SlidingPagination;
  9. use Knp\Component\Pager\PaginatorInterface;
  10. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  11. use Pimcore\Bundle\EcommerceFrameworkBundle\FilterService\ListHelper;
  12. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ElasticSearch\AbstractElasticSearch;
  13. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
  14. use Pimcore\Config;
  15. use Pimcore\Controller\FrontendController;
  16. use App\Model\Product\Category;
  17. use Pimcore\Model\DataObject\ClassDefinition;
  18. use Pimcore\Model\DataObject\FilterDefinition;
  19. use Pimcore\Model\DataObject\ProductGroup;
  20. use Pimcore\Model\DataObject\ProductLine;
  21. use Pimcore\Translation\Translator;
  22. use Pimcore\Twig\Extension\Templating\HeadTitle;
  23. use Pimcore\Twig\Extension\Templating\Placeholder;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use Symfony\Component\HttpFoundation\JsonResponse;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Pimcore\Model\DataObject;
  29. class ProductController extends FrontendController
  30. {
  31.     private int $productParentId;
  32.     public function __construct(
  33.         private ProductRepository $productRepository
  34.     ){
  35.         // get parentId of folder /Produkte
  36.         $this->productParentId DataObject\Folder::getByPath('/Produkte')->getId();
  37.     }
  38.     /**
  39.      * @param Request $request
  40.      * @return Response
  41.      */
  42.     public function indexAction(Request $requestHeadTitle $headTitleHelperFactory $ecommerceFactoryListHelper $listHelperPaginatorInterface $paginator): Response
  43.     {
  44.         $params array_merge($request->query->all(), $request->attributes->all());
  45.         $indexService $ecommerceFactory->getIndexService();
  46.         $productListing $indexService->getProductListForCurrentTenant();
  47.         if(!isset($params['sort'])){
  48.             $productListing->setOrderKey([
  49.                 [
  50.                     "sortingWeight""desc",
  51.                 ], [
  52.                     "code""asc"
  53.                 ]]);
  54.         }else{
  55.             $elements explode("-"$params['sort']);
  56.             $productListing->setOrderKey([
  57.                 [
  58.                     $elements[0], $elements[1],
  59.                 ]
  60.             ]);
  61.         }
  62.         $productListing->addCondition('showInStoreFront = 1');
  63.         // show only direct children in path /Produkte
  64.         $productListing->addCondition('o_parentId = ' $this->productParentId);
  65.         $productListing->setVariantMode(ProductListInterface::VARIANT_MODE_HIDE);
  66.         $params['productListing'] = $productListing;
  67.         if ($request->get('filterdefinition') instanceof FilterDefinition) {
  68.             $filterDefinition $request->get('filterdefinition');
  69.         }
  70.         if (empty($filterDefinition)) {
  71.             $filterDefinition Config::getWebsiteConfig()['fallbackFilterdefinition'];
  72.         }
  73.         $filterService $ecommerceFactory->getFilterService();
  74.         $listHelper->setupProductList($filterDefinition$productListing$params$filterServicetrue);
  75.         $params['filterService'] = $filterService;
  76.         $params['filterDefinition'] = $filterDefinition;
  77.         /** @var SlidingPagination $paginator */
  78.         // init pagination
  79.         $paginator $paginator->paginate(
  80.             $productListing,
  81.             $request->get('page'1),
  82.             $filterDefinition->getPageLimit()
  83.         );
  84.         $params['results'] = $paginator;
  85.         $params['paginationVariables'] = $paginator->getPaginationData();
  86.         return $this->render('product/index.html.twig'$params);
  87.     }
  88.     /**
  89.      *
  90.      * @Route("/{_locale}/product-category/{categoryname}-{category}", name="shop-category", requirements={"categoryname"="[\w-]+", "category"="\d+"})
  91.      *
  92.      * @param Request $request
  93.      * @param HeadTitle $headTitleHelper
  94.      * @param Factory $ecommerceFactory
  95.      * @param ListHelper $listHelper
  96.      *
  97.      * @return Response
  98.      */
  99.     public function listingAction(Request $requestFactory $ecommerceFactoryListHelper $listHelperPaginatorInterface $paginator)
  100.     {
  101.         $params array_merge($request->query->all(), $request->attributes->all());
  102.         //needed to make sure category filter filters for active category
  103.         $params['parentCategoryIds'] = $params['category'] ?? null;
  104.         $category Category::getById($params['category'] ?? -1);
  105.         $params['category'] = $category;
  106.         $indexService $ecommerceFactory->getIndexService();
  107.         $productListing $indexService->getProductListForCurrentTenant();
  108.         if(!isset($params['sort'])){
  109.             $productListing->setOrderKey([
  110.                 [
  111.                     "sortingWeight""desc",
  112.                 ], [
  113.                     "code""asc"
  114.                 ]]);
  115.         }else{
  116.             $elements explode("-"$params['sort']);
  117.             $productListing->setOrderKey([
  118.                 [
  119.                     $elements[0], $elements[1],
  120.                 ]
  121.             ]);
  122.         }
  123.         $productListing->addCondition('showInStoreFront = 1');
  124.         $productListing->addCondition('o_parentId = ' $this->productParentId);
  125.         $productListing->setVariantMode(ProductListInterface::VARIANT_MODE_HIDE);
  126.         $params['productListing'] = $productListing;
  127.         // load current filter
  128.         if ($category) {
  129.             $filterDefinition $category->getFilterDefinition();
  130.             $trackingManager $ecommerceFactory->getTrackingManager();
  131.             $trackingManager->trackCategoryPageView($category->getName(), null);
  132.         }
  133.         if ($request->get('filterdefinition') instanceof FilterDefinition) {
  134.             $filterDefinition $request->get('filterdefinition');
  135.         }
  136.         if (empty($filterDefinition)) {
  137.             $filterDefinition Config::getWebsiteConfig()['fallbackFilterdefinition'];
  138.         }
  139.         $filterService $ecommerceFactory->getFilterService();
  140.         $listHelper->setupProductList($filterDefinition$productListing$params$filterServicetrue);
  141.         $params['filterService'] = $filterService;
  142.         $params['filterDefinition'] = $filterDefinition;
  143.         /** @var SlidingPagination $paginator */
  144.         // init pagination
  145.         $paginator $paginator->paginate(
  146.             $productListing,
  147.             $request->get('page'1),
  148.             $filterDefinition->getPageLimit()
  149.         );
  150.         $params['results'] = $paginator;
  151.         $params['paginationVariables'] = $paginator->getPaginationData();
  152.         if ($request->attributes->get('noLayout')) {
  153.             return $this->render('product/listing_content.html.twig'$params);
  154.         }
  155.         // track product impressions
  156.         $trackingManager $ecommerceFactory->getTrackingManager();
  157.         foreach ($paginator as $product) {
  158.             $trackingManager->trackProductImpression($product'grid');
  159.         }
  160.         return $this->render('product/index.html.twig'$params);
  161.     }
  162.     /**
  163.      *
  164.      * @Route("/{_locale}/product-line/{productlinename}-{productline}", name="shop-productline", requirements={"productlinename"="[\w-]+", "productline"="\d+"})
  165.      *
  166.      * @param Request $request
  167.      * @param HeadTitle $headTitleHelper
  168.      * @param Factory $ecommerceFactory
  169.      * @param ListHelper $listHelper
  170.      *
  171.      * @return Response
  172.      */
  173.     public function listingProductLines(Request $requestFactory $ecommerceFactoryListHelper $listHelperPaginatorInterface $paginator): Response
  174.     {
  175.         $params array_merge($request->query->all(), $request->attributes->all());
  176.         $productLine ProductLine::getById($params['productline'] ?? -1);
  177.         $indexService $ecommerceFactory->getIndexService();
  178.         $productListing $indexService->getProductListForCurrentTenant();
  179.         if(!isset($params['sort'])){
  180.             $productListing->setOrderKey([
  181.                 [
  182.                     "sortingWeight""desc",
  183.                 ], [
  184.                     "code""asc"
  185.                 ]]);
  186.         }else{
  187.             $elements explode("-"$params['sort']);
  188.             $productListing->setOrderKey([
  189.                 [
  190.                     $elements[0], $elements[1],
  191.                 ]
  192.             ]);
  193.         }
  194.         $productListing->addCondition('showInStoreFront = 1');
  195.         $productListing->addCondition('o_parentId = ' $this->productParentId);
  196.         $productListing->addRelationCondition("productLine""dest IN (".$productLine->getId().")");
  197.         $productListing->setVariantMode(ProductListInterface::VARIANT_MODE_HIDE);
  198.         $params['productListing'] = $productListing;
  199.         if ($request->get('filterdefinition') instanceof FilterDefinition) {
  200.             $filterDefinition $request->get('filterdefinition');
  201.         }
  202.         if (empty($filterDefinition)) {
  203.             $filterDefinition Config::getWebsiteConfig()['fallbackFilterdefinition'];
  204.         }
  205.         $filterService $ecommerceFactory->getFilterService();
  206.         $listHelper->setupProductList($filterDefinition$productListing$params$filterServicetrue);
  207.         $params['filterService'] = $filterService;
  208.         $params['filterDefinition'] = $filterDefinition;
  209.         /** @var SlidingPagination $paginator */
  210.         // init pagination
  211.         $paginator $paginator->paginate(
  212.             $productListing,
  213.             $request->get('page'1),
  214.             $filterDefinition->getPageLimit()
  215.         );
  216.         $params['results'] = $paginator;
  217.         $params['paginationVariables'] = $paginator->getPaginationData();
  218.         if ($request->attributes->get('noLayout')) {
  219.             return $this->render('product/listing_content.html.twig'$params);
  220.         }
  221.         // track product impressions
  222.         $trackingManager $ecommerceFactory->getTrackingManager();
  223.         foreach ($paginator as $product) {
  224.             $trackingManager->trackProductImpression($product'grid');
  225.         }
  226.         return $this->render('product/index.html.twig'$params);
  227.     }
  228.     /**
  229.      *
  230.      * @Route("/{_locale}/product-group/{productgroupname}-{productgroup}", name="shop-productgroup", requirements={"productgroupname"="[\w-]+", "productgroup"="\d+"})
  231.      *
  232.      * @param Request $request
  233.      * @param HeadTitle $headTitleHelper
  234.      * @param Factory $ecommerceFactory
  235.      * @param ListHelper $listHelper
  236.      *
  237.      * @return Response
  238.      */
  239.     public function listingProductGroups(Request $requestFactory $ecommerceFactoryListHelper $listHelperPaginatorInterface $paginator): Response
  240.     {
  241.         $params array_merge($request->query->all(), $request->attributes->all());
  242.         $productGroup ProductGroup::getById($params['productgroup'] ?? -1);
  243.         $indexService $ecommerceFactory->getIndexService();
  244.         $productListing $indexService->getProductListForCurrentTenant();
  245.         if(!isset($params['sort'])){
  246.             $productListing->setOrderKey([
  247.                 [
  248.                     "sortingWeight""desc",
  249.                 ], [
  250.                     "code""asc"
  251.                 ]]);
  252.         }else{
  253.             $elements explode("-"$params['sort']);
  254.             $productListing->setOrderKey([
  255.                 [
  256.                     $elements[0], $elements[1],
  257.                 ]
  258.             ]);
  259.         }
  260.         $productListing->addCondition('showInStoreFront = 1');
  261.         $productListing->addCondition('o_parentId = ' $this->productParentId);
  262.         $productListing->addRelationCondition("productGroup""dest IN (".$productGroup->getId().")");
  263.         $productListing->setVariantMode(ProductListInterface::VARIANT_MODE_HIDE);
  264.         $params['productListing'] = $productListing;
  265.         if ($request->get('filterdefinition') instanceof FilterDefinition) {
  266.             $filterDefinition $request->get('filterdefinition');
  267.         }
  268.         if (empty($filterDefinition)) {
  269.             $filterDefinition Config::getWebsiteConfig()['fallbackFilterdefinition'];
  270.         }
  271.         $filterService $ecommerceFactory->getFilterService();
  272.         $listHelper->setupProductList($filterDefinition$productListing$params$filterServicetrue);
  273.         $params['filterService'] = $filterService;
  274.         $params['filterDefinition'] = $filterDefinition;
  275.         /** @var SlidingPagination $paginator */
  276.         // init pagination
  277.         $paginator $paginator->paginate(
  278.             $productListing,
  279.             $request->get('page'1),
  280.             $filterDefinition->getPageLimit()
  281.         );
  282.         $params['results'] = $paginator;
  283.         $params['paginationVariables'] = $paginator->getPaginationData();
  284.         if ($request->attributes->get('noLayout')) {
  285.             return $this->render('product/listing_content.html.twig'$params);
  286.         }
  287.         // track product impressions
  288.         $trackingManager $ecommerceFactory->getTrackingManager();
  289.         foreach ($paginator as $product) {
  290.             $trackingManager->trackProductImpression($product'grid');
  291.         }
  292.         return $this->render('product/index.html.twig'$params);
  293.     }
  294.     /**
  295.      * @Route ("/{_locale}/products/{slug}-{productId}", name="product_show", requirements={"slug"="[\w-]+", "productId"="\d+"})
  296.      * @param Request $request
  297.      * @param int $productId
  298.      * @return Response
  299.      * @throws \Exception
  300.      */
  301.     public function showAction(Request $requestint $productIdProductLinkGenerator $productLinkGenerator): Response
  302.     {
  303.         $product $this->productRepository->find($productId);
  304.         if(empty($product)){
  305.             throw $this->createNotFoundException('Product not found');
  306.         }
  307.         $otherLanguageUrl str_replace("/" $request->getLocale() . "/""/" . ($request->getLocale() == "de" "en" "de") . "/"$request->getRequestUri());
  308.         return $this->render('product/show.html.twig', [
  309.             'otherLanguageUrl' => $otherLanguageUrl,
  310.             'product' => (new ProductDetailDataMapper($product))->toArray($request),
  311.         ]);
  312.     }
  313.     /**
  314.      * @Route ("/{_locale}/products-raw/{slug}-{productId}", name="products_raw_show", requirements={"slug"="[\w-]+", "productId"="\d+"})
  315.      * @param Request $request
  316.      * @param int $productId
  317.      * @return JsonResponse
  318.      * @throws \Exception
  319.      */
  320.     public function showRawAction(Request $requestint $productId): JsonResponse
  321.     {
  322.         $product $this->productRepository->find($productId);
  323.         if(empty($product)){
  324.             throw $this->createNotFoundException('Product not found');
  325.         }
  326.         return new JsonResponse((new ProductDetailDataMapper($product))->toArray($request));
  327.     }
  328.     /**
  329.      * @Route("/{_locale}/search", name="search", methods={"GET"})
  330.      */
  331.     public function searchAction(Request $requestListHelper $listHelperFactory $ecommerceFactoryProductLinkGenerator $productLinkGeneratorTranslator $translatorHeadTitle $headTitleHelperPlaceholder $placeholderPaginatorInterface $paginator): Response
  332.     {
  333.         $params $request->query->all();
  334.         $indexService $ecommerceFactory->getIndexService();
  335.         $productListing $indexService->getProductListForCurrentTenant();
  336.         $productListing->setVariantMode(ProductListInterface::VARIANT_MODE_HIDE);
  337.         $productListing->addCondition('showInStoreFront = 1');
  338.         $productListing->addCondition('o_parentId = ' $this->productParentId);
  339.         $term strip_tags($request->query->get('term'));
  340.         $term trim(preg_replace('/\s+/'' '$term));
  341.         if (!empty($term)) {
  342.             $cleanedTerm str_replace(' '''$term);
  343.             $productListing->addCondition('REPLACE(code, " ", "") LIKE "%' $cleanedTerm '%" OR name LIKE "%' $term '%"');
  344.         }
  345.         //$productListing->addCondition('code LIKE "%12/5510%"');
  346.         if ($request->get('filterdefinition') instanceof FilterDefinition) {
  347.             $filterDefinition $params['filterDefinition'] = $request->get('filterdefinition');
  348.         }
  349.         if (empty($filterDefinition)) {
  350.             $filterDefinition $params['filterDefinition'] = Config::getWebsiteConfig()['fallbackFilterdefinition'];
  351.         }
  352.         // create and init filter service
  353.         $filterService Factory::getInstance()->getFilterService();
  354.         $listHelper->setupProductList($filterDefinition$productListing$params$filterServicetrue);
  355.         $params['filterService'] = $filterService;
  356.         $params['productListing'] = $productListing;
  357.         // init pagination
  358.         $paginator $paginator->paginate(
  359.             $productListing,
  360.             $request->query->getInt('page'1),
  361.             $filterDefinition->getPageLimit()
  362.         );
  363.         $params['results'] = $paginator;
  364.         $params['paginationVariables'] = $paginator->getPaginationData();
  365.         $trackingManager $ecommerceFactory->getTrackingManager();
  366.         foreach ($paginator as $product) {
  367.             $trackingManager->trackProductImpression($product'search-results');
  368.         }
  369.         $params['language'] = $request->getLocale();
  370.         $params['term'] = $term;
  371.         //$breadcrumbHelperService->enrichGenericDynamicPage($translator->trans('shop.search-result', [$term]));
  372.         $headTitleHelper($translator->trans('shop.search-result', [$term]));
  373.         return $this->render('product/index.html.twig'$params);
  374.     }
  375.     /**
  376.      * @Route ("/{_locale}/p/{productCode}", name="product_shortener")
  377.      */
  378.     public function productShortener(Request $requeststring $productCodeProductLinkGenerator $productLinkGenerator): Response{
  379.         $products Product::getByCode($productCode);
  380.         foreach($products as $product){
  381.             // redirect to product page
  382.             return $this->redirect($productLinkGenerator->generate($product, []));
  383.         }
  384.         throw $this->createNotFoundException('Product not found');
  385.     }
  386.     /**
  387.      * @Route ("/{_locale}/raw/{productCode}", name="single_product_raw")
  388.      * @param Request $request
  389.      * @param string $productCode
  390.      * @return JsonResponse
  391.      */
  392.     public function singleProductRaw(Request $requeststring $productCode): JsonResponse{
  393.         $products Product::getByCode($productCode);
  394.         foreach($products as $product){
  395.             // redirect to product page
  396.             return new JsonResponse((new ProductDetailDataMapper($product))->toArray($request));
  397.         }
  398.         throw $this->createNotFoundException('Product not found');
  399.     }
  400. }