<?php
/**
* Product Controller
*
* @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
*/
namespace VisualMedia\ProductBundle\Controller;
use Exception;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use VisualMedia\LisaBundle\Component\DataLayer;
use VisualMedia\LisaBundle\Component\ManagerData;
use VisualMedia\LisaBundle\Controller\BaseController;
use VisualMedia\LisaBundle\Service\SettingProvider;
use VisualMedia\ProductBundle\Manager\BaseProductManager;
use VisualMedia\ProductBundle\Routing\ProductRouting;
use VisualMedia\TranslationBundle\Service\ChannelProvider;
/**
* Product Controller
*/
class ProductController extends BaseController
{
public const SESSION_RECENTLY_VIEWED = 'recently_viewed_products';
/**
* Product
* @var BaseProduct
*/
protected $product;
/**
* View Action
*
* @param Request $request
* @param string $slug
* @param string|null $hash unique number from cart to reconfigure product when needed
*
* @return Response
*/
public function viewAction(Request $request, string $slug, string $hash = null): Response
{
$translator = $this->get('translator');
$manager = $this->get(BaseProductManager::class);
$settingProvider = $this->get(SettingProvider::class);
$productRouting = $this->get(ProductRouting::class);
$channelProvider = $this->get(ChannelProvider::class);
$channel = $channelProvider->getCurrentChannel();
$data = [ $manager::OPTION_SLUG => $slug, 'channel' => $channel, "locale" => $channelProvider->getCurrentLocale() ];
// Get product.
$managerData = new ManagerData($data);
if (null === $this->product = $manager->getFirst($managerData)) {
throw new Exception($translator->trans('product.not_found', [], 'exception'), 404);
}
elseif (!$this->product->getPublished()) {
throw new Exception($translator->trans('product.gone', [], 'exception'), 410);
}
// Fire GA4 "view_item".
DataLayer::pushEcommerce('view_item', $this->product);
// Redirect to the first product variant, if the requested product is a main product.
if ($this->product->isMainProduct()) {
foreach ($this->product->getChildren() as $child) {
if ($child->getPublished()) {
break;
}
}
if($this->product->slug === $child->slug) {
throw new Exception('Main product has same slug as variant');
}
return $this->redirect($productRouting->getRoute($child));
}
// Define product variants, if the requested product is a produt variant itself.
if ($this->product->isProductVariant()) {
$variants = $manager->getIndex(new ManagerData(array(
$manager::OPTION_PARENT => $this->product->getParent()->getId(),
$manager::OPTION_PUBLISHED => true,
), array('ordering' => $manager::ORDER_DIR_ASC)));
}
// Store product in the session.
$max = $settingProvider->getValue($reference = 'recently_viewed_products_max', $domain = 'system');
$stored = $request->getSession()->get(static::SESSION_RECENTLY_VIEWED, array());
$id = $this->product->isProductVariant() ? $this->product->getParent()->getId() : $this->product->getId();
if (empty($max)) {
$stored = array();
}
else {
array_unshift($stored, $id);
$stored = array_unique($stored);
}
$stored = array_slice($stored, 0, $max);
$request->getSession()->set(static::SESSION_RECENTLY_VIEWED, $stored);
//dd($this->product);
// Render view.
return $this->render('@VisualMediaProduct/Product/view.html.twig', array(
'browser_title' => $this->product->browserTitle,
'meta_description' => $this->product->metaDescription,
'meta_keywords' => $this->product->metaKeywords,
'indexable' => $this->product->indexable,
'product' => $this->product,
'variants' => $variants ?? array(),
'hash' => $hash
));
}
/**
* Ssi Recently Viewed
*
* @param Request $request
*
* @return Response
*/
public function ssiRecentlyViewedAction(Request $request): Response
{
$manager = $this->get(BaseProductManager::class);
$products = $mapped = array();
// Get, map and sort the products.
if (!empty($stored = $request->getSession()->get(static::SESSION_RECENTLY_VIEWED, array()))) {
$result = $manager->getIndex(new ManagerData(array(
$manager::OPTION_JOIN_PHOTOS => true,
$manager::OPTION_IDS => $stored,
), array(), 4));
foreach ($result as $product) {
$mapped[$product->getId()] = $product;
}
foreach ($stored as $id) {
$products[] = $mapped[$id] ?? null;
}
}
// Render view.
return $this->render('@VisualMediaProduct/Product/recently_viewed.html.twig', array(
'products' => $products,
));
}
}