src/Controller/ProductController.php line 97

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\Controller;
  15. use App\Model\Product\AbstractProduct;
  16. use App\Model\Product\AccessoryPart;
  17. use App\Model\Product\Car;
  18. use App\Model\Product\Category;
  19. use App\Services\SegmentTrackingHelperService;
  20. use App\Website\LinkGenerator\ProductLinkGenerator;
  21. use App\Website\Navigation\BreadcrumbHelperService;
  22. use Knp\Component\Pager\Pagination\SlidingPagination;
  23. use Knp\Component\Pager\PaginatorInterface;
  24. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  25. use Pimcore\Bundle\EcommerceFrameworkBundle\FilterService\ListHelper;
  26. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\DefaultMysql;
  27. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ElasticSearch\AbstractElasticSearch;
  28. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
  29. use Pimcore\Config;
  30. use Pimcore\Model\DataObject\AbstractObject;
  31. use Pimcore\Model\DataObject\Concrete;
  32. use Pimcore\Model\DataObject\Data\UrlSlug;
  33. use Pimcore\Model\DataObject\FilterDefinition;
  34. use Pimcore\Translation\Translator;
  35. use Pimcore\Twig\Extension\Templating\HeadTitle;
  36. use Pimcore\Twig\Extension\Templating\Placeholder;
  37. use Symfony\Component\HttpFoundation\JsonResponse;
  38. use Symfony\Component\HttpFoundation\Request;
  39. use Symfony\Component\HttpFoundation\Response;
  40. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  41. use Symfony\Component\Routing\Annotation\Route;
  42. class ProductController extends BaseController
  43. {
  44.     /**
  45.      * @param Request $request
  46.      * @param AbstractObject $object
  47.      * @param UrlSlug $urlSlug
  48.      * @return Response
  49.      */
  50.     public function productDetailSlugAction(Request $requestAbstractObject $objectUrlSlug $urlSlug) {
  51.         return $this->forward('App\Controller\ProductController::detailAction', ['product' => $object]);
  52.     }
  53.     /**
  54.      * @Route("/shop/{path}{productname}~p{product}", name="shop-detail", defaults={"path"=""}, requirements={"path"=".*?", "productname"="[\w-]+", "product"="\d+"})
  55.      *
  56.      * @param Request $request
  57.      * @param HeadTitle $headTitleHelper
  58.      * @param BreadcrumbHelperService $breadcrumbHelperService
  59.      * @param Factory $ecommerceFactory
  60.      * @param SegmentTrackingHelperService $segmentTrackingHelperService
  61.      * @param Concrete $product built-in parameter conversion, please see https://github.com/pimcore/pimcore/pull/5554
  62.      * @param ProductLinkGenerator $productLinkGenerator
  63.      *
  64.      * @return Response
  65.      *
  66.      * @throws \Exception
  67.      */
  68.     public function detailAction(
  69.         Request $request,
  70.         HeadTitle $headTitleHelper,
  71.         BreadcrumbHelperService $breadcrumbHelperService,
  72.         Factory $ecommerceFactory,
  73.         SegmentTrackingHelperService $segmentTrackingHelperService,
  74.         Concrete $product,
  75.         ProductLinkGenerator $productLinkGenerator
  76.     )
  77.     {
  78.         if (!(
  79.                 $product && ($product->isPublished() && (($product instanceof Car && $product->getObjectType() == Car::OBJECT_TYPE_ACTUAL_CAR) || $product instanceof AccessoryPart) || $this->verifyPreviewRequest($request$product))
  80.             )
  81.         ) {
  82.             throw new NotFoundHttpException('Product not found.');
  83.         }
  84.         //redirect to main url
  85.         $generatorUrl $productLinkGenerator->generate($product);
  86.         if($generatorUrl != $request->getPathInfo()) {
  87.             $queryString $request->getQueryString();
  88.             return $this->redirect($generatorUrl . ($queryString '?' $queryString ''));
  89.         }
  90.         $breadcrumbHelperService->enrichProductDetailPage($product);
  91.         $headTitleHelper($product->getOSName());
  92.         $paramBag $this->getAllParameters($request);
  93.         $paramBag['product'] = $product;
  94.         //track segments for personalization
  95.         $segmentTrackingHelperService->trackSegmentsForProduct($product);
  96.         $trackingManager $ecommerceFactory->getTrackingManager();
  97.         $trackingManager->trackProductView($product);
  98.         if ($product instanceof Car) {
  99.             foreach ($product->getAccessories() as $accessory) {
  100.                 $trackingManager->trackProductImpression($accessory'crosssells');
  101.             }
  102.             return $this->render('product/detail.html.twig'$paramBag);
  103.         } elseif ($product instanceof AccessoryPart) {
  104.             // get all compatible products
  105.             $productList $ecommerceFactory->getIndexService()->getProductListForCurrentTenant();
  106.             $productList->setVariantMode(ProductListInterface::VARIANT_MODE_VARIANTS_ONLY);
  107.             if($productList instanceof DefaultMysql) {
  108.                 $productList->addCondition('o_id IN (' implode(','$product->getCompatibleToProductIds()) . ')''o_id');
  109.             } else if($productList instanceof AbstractElasticSearch) {
  110.                 $productList->addCondition(['terms' => ['system.o_id' => $product->getCompatibleToProductIds()]], 'o_id');
  111.             }
  112.             foreach($productList as $compatibleProduct) {
  113.                 $trackingManager->trackProductImpression($compatibleProduct'crosssells');
  114.             }
  115.             $paramBag['compatibleTo'] = $productList;
  116.             return $this->render('product/detail_accessory.html.twig'$paramBag);
  117.         }
  118.     }
  119.     /**
  120.      * @Route("/shop/{path}{categoryname}~c{category}", name="shop-category", defaults={"path"=""}, requirements={"path"=".*?", "categoryname"="[\w-]+", "category"="\d+"})
  121.      *
  122.      * @param Request $request
  123.      * @param HeadTitle $headTitleHelper
  124.      * @param BreadcrumbHelperService $breadcrumbHelperService
  125.      * @param Factory $ecommerceFactory
  126.      * @param SegmentTrackingHelperService $segmentTrackingHelperService
  127.      * @param ListHelper $listHelper
  128.      *
  129.      * @return Response
  130.      */
  131.     public function listingAction(Request $requestHeadTitle $headTitleHelperBreadcrumbHelperService $breadcrumbHelperServiceFactory $ecommerceFactorySegmentTrackingHelperService $segmentTrackingHelperServiceListHelper $listHelperPaginatorInterface $paginator)
  132.     {
  133.         $params array_merge($request->query->all(), $request->attributes->all());
  134.         //needed to make sure category filter filters for active category
  135.         $params['parentCategoryIds'] = $params['category'] ?? null;
  136.         $category Category::getById($params['category'] ?? null);
  137.         $params['category'] = $category;
  138.         if ($category) {
  139.             $headTitleHelper($category->getName());
  140.             $breadcrumbHelperService->enrichCategoryPage($category);
  141.         }
  142.         $indexService $ecommerceFactory->getIndexService();
  143.         $productListing $indexService->getProductListForCurrentTenant();
  144.         $productListing->setVariantMode(ProductListInterface::VARIANT_MODE_VARIANTS_ONLY);
  145.         $params['productListing'] = $productListing;
  146.         // load current filter
  147.         if ($category) {
  148.             $filterDefinition $category->getFilterdefinition();
  149.             //track segments for personalization
  150.             $segmentTrackingHelperService->trackSegmentsForCategory($category);
  151.             $trackingManager $ecommerceFactory->getTrackingManager();
  152.             $trackingManager->trackCategoryPageView($category->getName(), null);
  153.         }
  154.         if ($request->get('filterdefinition') instanceof FilterDefinition) {
  155.             $filterDefinition $request->get('filterdefinition');
  156.         }
  157.         if (empty($filterDefinition)) {
  158.             $filterDefinition Config::getWebsiteConfig()->get('fallbackFilterdefinition');
  159.         }
  160.         $filterService $ecommerceFactory->getFilterService();
  161.         $listHelper->setupProductList($filterDefinition$productListing$params$filterServicetrue);
  162.         $params['filterService'] = $filterService;
  163.         $params['filterDefinition'] = $filterDefinition;
  164.         /** @var SlidingPagination $paginator */
  165.         // init pagination
  166.         $paginator $paginator->paginate(
  167.             $productListing,
  168.             $request->get('page'1),
  169.             $filterDefinition->getPageLimit()
  170.         );
  171.         $params['results'] = $paginator;
  172.         $params['paginationVariables'] = $paginator->getPaginationData();
  173.         if ($request->attributes->get('noLayout')) {
  174.             return $this->render('product/listing_content.html.twig'$params);
  175.         }
  176.         // track product impressions
  177.         $trackingManager $ecommerceFactory->getTrackingManager();
  178.         foreach ($paginator as $product) {
  179.             $trackingManager->trackProductImpression($product'grid');
  180.         }
  181.         return $this->render('product/listing.html.twig'$params);
  182.     }
  183.     /**
  184.      * @param Request $request
  185.      * @param Factory $ecommerceFactory
  186.      *
  187.      * @return Response
  188.      */
  189.     public function productTeaserAction(Request $requestFactory $ecommerceFactory)
  190.     {
  191.         $paramsBag = [];
  192.         if ($request->get('type') == 'object') {
  193.             AbstractObject::setGetInheritedValues(true);
  194.             $product AbstractProduct::getById($request->get('id'));
  195.             $paramsBag['product'] = $product;
  196.             //track product impression
  197.             $trackingManager $ecommerceFactory->getTrackingManager();
  198.             $trackingManager->trackProductImpression($product'teaser');
  199.             return $this->render('product/product_teaser.html.twig'$paramsBag);
  200.         }
  201.         throw new NotFoundHttpException('Product not found.');
  202.     }
  203.     /**
  204.      * @Route("/search", name="search")
  205.      *
  206.      * @param Request $request
  207.      * @param ListHelper $listHelper
  208.      * @param Factory $ecommerceFactory
  209.      * @param ProductLinkGenerator $productLinkGenerator
  210.      * @param Translator $translator
  211.      * @param BreadcrumbHelperService $breadcrumbHelperService
  212.      * @param HeadTitle $headTitleHelper
  213.      * @param Placeholder $placeholder
  214.      *
  215.      * @return Response|JsonResponse
  216.      */
  217.     public function searchAction(Request $requestListHelper $listHelperFactory $ecommerceFactoryProductLinkGenerator $productLinkGeneratorTranslator $translatorBreadcrumbHelperService $breadcrumbHelperServiceHeadTitle $headTitleHelperPlaceholder $placeholderPaginatorInterface $paginator)
  218.     {
  219.         $params $request->query->all();
  220.         $params['category'] = Category::getById($params['category'] ?? null);
  221.         $indexService $ecommerceFactory->getIndexService();
  222.         $productListing $indexService->getProductListForCurrentTenant();
  223.         $productListing->setVariantMode(ProductListInterface::VARIANT_MODE_VARIANTS_ONLY);
  224.         $term strip_tags($request->get('term'));
  225.         if($productListing instanceof AbstractElasticSearch) {
  226.             // simple elastic search query - uses multi-match query on all defined search_attributes
  227. //            $productListing->addQueryCondition($term);
  228.             //sample for a more specific elastic search query - not considers search_attributes but provides full flexibility
  229.             // this query weights cars more that accessories
  230.             $query = [
  231.                 'function_score' => [
  232.                     'query' => [
  233.                         'multi_match' => [
  234.                             "query" => $term,
  235.                             "type" => "cross_fields",
  236.                             "operator" => "and",
  237.                             "fields" => [
  238.                                 "attributes.name^4",
  239.                                 "attributes.name.analyzed",
  240.                                 "attributes.name.analyzed_ngram",
  241.                                 "attributes.manufacturer_name^3",
  242.                                 "attributes.manufacturer_name.analyzed",
  243.                                 "attributes.manufacturer_name.analyzed_ngram",
  244.                                 "attributes.color",
  245.                                 "attributes.color.analyzed",
  246.                                 "attributes.color.analyzed_ngram",
  247.                                 "attributes.carClass",
  248.                                 "attributes.carClass.analyzed",
  249.                                 "attributes.carClass.analyzed_ngram"
  250.                             ]
  251.                         ]
  252.                     ],
  253.                     'functions' => [
  254.                         [
  255.                             'filter' => ['match' => ['system.o_classId' => 'AP']],
  256.                             'weight' => 1
  257.                         ],
  258.                         [
  259.                             'filter' => ['match' => ['system.o_classId' => 'CAR']],
  260.                             'weight' => 2
  261.                         ]
  262.                     ],
  263.                     'boost_mode' => 'multiply'
  264.                 ]
  265.             ];
  266.             $productListing->addQueryCondition($query'searchTerm');
  267.         } else {
  268.             //default mysql search query condition - would also work for elastic search in that way
  269.             $term trim(preg_replace('/\s+/'' '$term));
  270.             if (!empty($term)) {
  271.                 foreach (explode(' '$term) as $t) {
  272.                     $productListing->addQueryCondition($t);
  273.                 }
  274.             }
  275.         }
  276.         if (isset($params['autocomplete'])) {
  277.             $resultset = [];
  278.             $productListing->setLimit(10);
  279.             foreach ($productListing as $product) {
  280.                 $result['href'] = $productLinkGenerator->generateWithMockup($product, []);
  281.                 if ($product instanceof Car) {
  282.                     $result['product'] = $product->getOSName() . ' ' $product->getColor()[0] . ', ' $product->getCarClass();
  283.                 } else {
  284.                     $result['product'] = $product->getOSName();
  285.                 }
  286.                 $resultset[] = $result;
  287.             }
  288.             return $this->json($resultset);
  289.         }
  290.         $filterDefinition $params['filterDefinition'] = Config::getWebsiteConfig()->get('fallbackFilterdefinition');
  291.         // create and init filter service
  292.         $filterService Factory::getInstance()->getFilterService();
  293.         $listHelper->setupProductList($filterDefinition$productListing$params$filterServicetrue);
  294.         $params['filterService'] = $filterService;
  295.         $params['products'] = $productListing;
  296.         // init pagination
  297.         $paginator $paginator->paginate(
  298.             $productListing,
  299.             $request->get('page'1),
  300.             $filterDefinition->getPageLimit()
  301.         );
  302.         $params['results'] = $paginator;
  303.         $params['paginationVariables'] = $paginator->getPaginationData();
  304.         $trackingManager $ecommerceFactory->getTrackingManager();
  305.         foreach ($paginator as $product) {
  306.             $trackingManager->trackProductImpression($product'search-results');
  307.         }
  308.         //breadcrumbs
  309.         $placeholder('addBreadcrumb')->append([
  310.             'parentId' => $this->document->getId(),
  311.             'id' => 'search-result',
  312.             'label' => $translator->trans('shop.search-result', [$term])
  313.         ]);
  314.         $params['language'] = $request->getLocale();
  315.         $params['term'] = $term;
  316.         $breadcrumbHelperService->enrichGenericDynamicPage($translator->trans('shop.search-result', [$term]));
  317.         $headTitleHelper($translator->trans('shop.search-result', [$term]));
  318.         return $this->render('product/search.html.twig'$params);
  319.     }
  320. }