lib/composer/ProductBundle/src/VisualMedia/ProductBundle/Controller/ProductController.php line 43

Open in your IDE?
  1. <?php
  2. /**
  3. * Product Controller
  4. *
  5. * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6. */
  7. namespace VisualMedia\ProductBundle\Controller;
  8. use Exception;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use VisualMedia\LisaBundle\Component\DataLayer;
  12. use VisualMedia\LisaBundle\Component\ManagerData;
  13. use VisualMedia\LisaBundle\Controller\BaseController;
  14. use VisualMedia\LisaBundle\Service\SettingProvider;
  15. use VisualMedia\ProductBundle\Manager\BaseProductManager;
  16. use VisualMedia\ProductBundle\Routing\ProductRouting;
  17. use VisualMedia\TranslationBundle\Service\ChannelProvider;
  18. /**
  19. * Product Controller
  20. */
  21. class ProductController extends BaseController
  22. {
  23. public const SESSION_RECENTLY_VIEWED = 'recently_viewed_products';
  24. /**
  25. * Product
  26. * @var BaseProduct
  27. */
  28. protected $product;
  29. /**
  30. * View Action
  31. *
  32. * @param Request $request
  33. * @param string $slug
  34. * @param string|null $hash unique number from cart to reconfigure product when needed
  35. *
  36. * @return Response
  37. */
  38. public function viewAction(Request $request, string $slug, string $hash = null): Response
  39. {
  40. $translator = $this->get('translator');
  41. $manager = $this->get(BaseProductManager::class);
  42. $settingProvider = $this->get(SettingProvider::class);
  43. $productRouting = $this->get(ProductRouting::class);
  44. $channelProvider = $this->get(ChannelProvider::class);
  45. $channel = $channelProvider->getCurrentChannel();
  46. $data = [ $manager::OPTION_SLUG => $slug, 'channel' => $channel, "locale" => $channelProvider->getCurrentLocale() ];
  47. // Get product.
  48. $managerData = new ManagerData($data);
  49. if (null === $this->product = $manager->getFirst($managerData)) {
  50. throw new Exception($translator->trans('product.not_found', [], 'exception'), 404);
  51. }
  52. elseif (!$this->product->getPublished()) {
  53. throw new Exception($translator->trans('product.gone', [], 'exception'), 410);
  54. }
  55. // Fire GA4 "view_item".
  56. DataLayer::pushEcommerce('view_item', $this->product);
  57. // Redirect to the first product variant, if the requested product is a main product.
  58. if ($this->product->isMainProduct()) {
  59. foreach ($this->product->getChildren() as $child) {
  60. if ($child->getPublished()) {
  61. break;
  62. }
  63. }
  64. if($this->product->slug === $child->slug) {
  65. throw new Exception('Main product has same slug as variant');
  66. }
  67. return $this->redirect($productRouting->getRoute($child));
  68. }
  69. // Define product variants, if the requested product is a produt variant itself.
  70. if ($this->product->isProductVariant()) {
  71. $variants = $manager->getIndex(new ManagerData(array(
  72. $manager::OPTION_PARENT => $this->product->getParent()->getId(),
  73. $manager::OPTION_PUBLISHED => true,
  74. ), array('ordering' => $manager::ORDER_DIR_ASC)));
  75. }
  76. // Store product in the session.
  77. $max = $settingProvider->getValue($reference = 'recently_viewed_products_max', $domain = 'system');
  78. $stored = $request->getSession()->get(static::SESSION_RECENTLY_VIEWED, array());
  79. $id = $this->product->isProductVariant() ? $this->product->getParent()->getId() : $this->product->getId();
  80. if (empty($max)) {
  81. $stored = array();
  82. }
  83. else {
  84. array_unshift($stored, $id);
  85. $stored = array_unique($stored);
  86. }
  87. $stored = array_slice($stored, 0, $max);
  88. $request->getSession()->set(static::SESSION_RECENTLY_VIEWED, $stored);
  89. //dd($this->product);
  90. // Render view.
  91. return $this->render('@VisualMediaProduct/Product/view.html.twig', array(
  92. 'browser_title' => $this->product->browserTitle,
  93. 'meta_description' => $this->product->metaDescription,
  94. 'meta_keywords' => $this->product->metaKeywords,
  95. 'indexable' => $this->product->indexable,
  96. 'product' => $this->product,
  97. 'variants' => $variants ?? array(),
  98. 'hash' => $hash
  99. ));
  100. }
  101. /**
  102. * Ssi Recently Viewed
  103. *
  104. * @param Request $request
  105. *
  106. * @return Response
  107. */
  108. public function ssiRecentlyViewedAction(Request $request): Response
  109. {
  110. $manager = $this->get(BaseProductManager::class);
  111. $products = $mapped = array();
  112. // Get, map and sort the products.
  113. if (!empty($stored = $request->getSession()->get(static::SESSION_RECENTLY_VIEWED, array()))) {
  114. $result = $manager->getIndex(new ManagerData(array(
  115. $manager::OPTION_JOIN_PHOTOS => true,
  116. $manager::OPTION_IDS => $stored,
  117. ), array(), 4));
  118. foreach ($result as $product) {
  119. $mapped[$product->getId()] = $product;
  120. }
  121. foreach ($stored as $id) {
  122. $products[] = $mapped[$id] ?? null;
  123. }
  124. }
  125. // Render view.
  126. return $this->render('@VisualMediaProduct/Product/recently_viewed.html.twig', array(
  127. 'products' => $products,
  128. ));
  129. }
  130. }