src/Ecommerce/Controller/ProductController.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\Ecommerce\Controller;
  3. use App\Service\WishListService;
  4. use App\Traits\LoggerAware;
  5. use CoreShop\Component\Core\Model\ProductInterface;
  6. use CoreShop\Component\Tracking\Tracker\TrackerInterface;
  7. use Pimcore\Model\Asset\Image;
  8. use Pimcore\Model\Site;
  9. use Pimcore\Twig\Extension\Templating\HeadMeta;
  10. use Pimcore\Twig\Extension\Templating\HeadTitle;
  11. use Pimcore\Twig\Extension\Templating\Placeholder;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Pimcore\Model\DataObject;
  15. use Pimcore\Model\DataObject\CoreShopProduct;
  16. use Symfony\Component\HttpFoundation\StreamedResponse;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  19. use CoreShop\Bundle\FrontendBundle\Controller\ProductController as BaseProductController;
  20. use App\Traits\VerifyPreviewRequest;
  21. use App\Traits\RecentWatches;
  22. class ProductController extends BaseProductController
  23. {
  24.     private $lastViewedLimit;
  25.     use VerifyPreviewRequest;
  26.     use RecentWatches;
  27.     use LoggerAware;
  28.     public function __construct($lastViewedLimit)
  29.     {
  30.         $this->lastViewedLimit $lastViewedLimit;
  31.         if (Site::isSiteRequest()) {
  32.             throw $this->createNotFoundException('Page not found!');
  33.         }
  34.     }
  35.     /**
  36.      * @Route("/ora/{sku}", name="coreshop_product_watch")
  37.      */
  38.     public function detailAction(Request $requestWishListService $wishListService nullPlaceholder $placeholder nullHeadTitle $headTitle nullHeadMeta $headMeta null): Response
  39.     {
  40.         $product CoreShopProduct::getBySku($request->get("sku"), ['limit' => 1'unpublished' => true]);
  41.         if (!($product instanceof ProductInterface && ($product->isPublished() || $this->verifyPreviewRequest($request$product)))) {
  42.             return $this->render(
  43.                 $this->templateConfigurator->findTemplate('Product/product_not_found.html'),
  44.                 [],
  45.                 new Response(''Response::HTTP_NOT_FOUND)
  46.             );
  47.         }
  48.         $placeholder('breadcrumbProduct')->set($product);
  49.         $headTitle->set($product->getSku());
  50.         $headMeta->setProperty('og:type''article');
  51.         $headMeta->setProperty('og:title'$product->getSku() . ' - Seiko Boutique');
  52.         $headMeta->getItem('property''og:type');
  53.         $headMeta->setProperty('og:description'$product->getShortDescription());
  54.         $ogImage $product->getImages()[0] ?? null;
  55.         if ($ogImage instanceof Image) {
  56.             $headMeta->setProperty('og:image'$request->getSchemeAndHttpHost() . $ogImage->getFullPath());
  57.             $headMeta->setProperty('og:image:width'$ogImage->getWidth());
  58.             $headMeta->setProperty('og:image:height'$ogImage->getHeight());
  59.         }
  60.         $availability_info $this->container->getParameter('product.availability_info');
  61.         $this->get(TrackerInterface::class)->trackProduct($product);
  62.         $this->get(TrackerInterface::class)->trackProductImpression([
  63.             'itemlist' => $product->getSimilarWatches(),
  64.             'item_list_name' => $product->getName(),
  65.             'item_list_id' => $product->getId()
  66.         ]);
  67.         $ga4SelectItem $request->getSession()->get('ga4_select_item');
  68.         $this->get(TrackerInterface::class)->trackProductImpression([
  69.             'itemlist' => [$product],
  70.             'item_list_name' => $ga4SelectItem['item_list_name'] ?? $product->getName(),
  71.             'item_list_id' => $ga4SelectItem['item_list_id'] ?? $product->getId(),
  72.             'event' => 'select_item'
  73.         ]);
  74.         $similarWatches = [];
  75.         foreach ($product->getSimilarWatches() as $similarWatch) {
  76.             $similarWatches[$similarWatch->getId()] = [
  77.                 'product' => $similarWatch,
  78.                 'isWishListed' => $wishListService->hasItem($similarWatch),
  79.             ];
  80.         }
  81.         $this->saveRecentWatch($product);
  82.         return $this->render($this->templateConfigurator->findTemplate('Product/detail.html'), [
  83.             'product' => $product,
  84.             'specification' => $this->container->getParameter('app.watches.details.specifications'),
  85.             'properties' => $this->container->getParameter('app.watches.details.properties'),
  86.             'availabilityOptions' => DataObject\Service::getOptionsForSelectField($product"availability"),
  87.             'availabilityExtra' => $availability_info[$product->getAvailability()] ?? null,
  88.             'similarWatches' => $similarWatches,
  89.             'isWishListed' => $wishListService->hasItem($product),
  90.         ]);
  91.     }
  92.     /**
  93.      * @Route("/getpic/{sku}", name="coreshop_product_pic")
  94.      */
  95.     public function getPictureAction(Request $request): Response
  96.     {
  97.         CoreShopProduct::setHideUnpublished(false);
  98.         $product CoreShopProduct::getBySku($request->get("sku"), 1);
  99.         if ($product instanceof CoreShopProduct && ($image $product->getImage()) instanceof Image) {
  100.             $stream $image->getStream();
  101.             return new StreamedResponse(function () use ($stream) {
  102.                 fpassthru($stream);
  103.             }, Response::HTTP_OK, ['Content-Type' => $image->getMimeType()]);
  104.         }
  105.         throw $this->createNotFoundException();
  106.     }
  107. }