<?php
namespace App\Ecommerce\Controller;
use App\Service\WishListService;
use App\Traits\LoggerAware;
use CoreShop\Component\Core\Model\ProductInterface;
use CoreShop\Component\Tracking\Tracker\TrackerInterface;
use Pimcore\Model\Asset\Image;
use Pimcore\Model\Site;
use Pimcore\Twig\Extension\Templating\HeadMeta;
use Pimcore\Twig\Extension\Templating\HeadTitle;
use Pimcore\Twig\Extension\Templating\Placeholder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\CoreShopProduct;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use CoreShop\Bundle\FrontendBundle\Controller\ProductController as BaseProductController;
use App\Traits\VerifyPreviewRequest;
use App\Traits\RecentWatches;
class ProductController extends BaseProductController
{
private $lastViewedLimit;
use VerifyPreviewRequest;
use RecentWatches;
use LoggerAware;
public function __construct($lastViewedLimit)
{
$this->lastViewedLimit = $lastViewedLimit;
if (Site::isSiteRequest()) {
throw $this->createNotFoundException('Page not found!');
}
}
/**
* @Route("/ora/{sku}", name="coreshop_product_watch")
*/
public function detailAction(Request $request, WishListService $wishListService = null, Placeholder $placeholder = null, HeadTitle $headTitle = null, HeadMeta $headMeta = null): Response
{
$product = CoreShopProduct::getBySku($request->get("sku"), ['limit' => 1, 'unpublished' => true]);
if (!($product instanceof ProductInterface && ($product->isPublished() || $this->verifyPreviewRequest($request, $product)))) {
return $this->render(
$this->templateConfigurator->findTemplate('Product/product_not_found.html'),
[],
new Response('', Response::HTTP_NOT_FOUND)
);
}
$placeholder('breadcrumbProduct')->set($product);
$headTitle->set($product->getSku());
$headMeta->setProperty('og:type', 'article');
$headMeta->setProperty('og:title', $product->getSku() . ' - Seiko Boutique');
$headMeta->getItem('property', 'og:type');
$headMeta->setProperty('og:description', $product->getShortDescription());
$ogImage = $product->getImages()[0] ?? null;
if ($ogImage instanceof Image) {
$headMeta->setProperty('og:image', $request->getSchemeAndHttpHost() . $ogImage->getFullPath());
$headMeta->setProperty('og:image:width', $ogImage->getWidth());
$headMeta->setProperty('og:image:height', $ogImage->getHeight());
}
$availability_info = $this->container->getParameter('product.availability_info');
$this->get(TrackerInterface::class)->trackProduct($product);
$this->get(TrackerInterface::class)->trackProductImpression([
'itemlist' => $product->getSimilarWatches(),
'item_list_name' => $product->getName(),
'item_list_id' => $product->getId()
]);
$ga4SelectItem = $request->getSession()->get('ga4_select_item');
$this->get(TrackerInterface::class)->trackProductImpression([
'itemlist' => [$product],
'item_list_name' => $ga4SelectItem['item_list_name'] ?? $product->getName(),
'item_list_id' => $ga4SelectItem['item_list_id'] ?? $product->getId(),
'event' => 'select_item'
]);
$similarWatches = [];
foreach ($product->getSimilarWatches() as $similarWatch) {
$similarWatches[$similarWatch->getId()] = [
'product' => $similarWatch,
'isWishListed' => $wishListService->hasItem($similarWatch),
];
}
$this->saveRecentWatch($product);
return $this->render($this->templateConfigurator->findTemplate('Product/detail.html'), [
'product' => $product,
'specification' => $this->container->getParameter('app.watches.details.specifications'),
'properties' => $this->container->getParameter('app.watches.details.properties'),
'availabilityOptions' => DataObject\Service::getOptionsForSelectField($product, "availability"),
'availabilityExtra' => $availability_info[$product->getAvailability()] ?? null,
'similarWatches' => $similarWatches,
'isWishListed' => $wishListService->hasItem($product),
]);
}
/**
* @Route("/getpic/{sku}", name="coreshop_product_pic")
*/
public function getPictureAction(Request $request): Response
{
CoreShopProduct::setHideUnpublished(false);
$product = CoreShopProduct::getBySku($request->get("sku"), 1);
if ($product instanceof CoreShopProduct && ($image = $product->getImage()) instanceof Image) {
$stream = $image->getStream();
return new StreamedResponse(function () use ($stream) {
fpassthru($stream);
}, Response::HTTP_OK, ['Content-Type' => $image->getMimeType()]);
}
throw $this->createNotFoundException();
}
}