<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\EventSubscriber;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Common\EccubeConfig;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Twig\Environment;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class ProductDetailEventSubscriber implements EventSubscriberInterface
{
protected $eccubeConfig;
private $twig;
/**
* @var EntityManagerInterface
*/
protected $entityManager;
protected $urlGenerator;
/**
* ProductDetailEventSubscriber constructor.
*/
public function __construct(
EccubeConfig $eccubeConfig,
Environment $twig,
EntityManagerInterface $entityManager,
UrlGeneratorInterface $urlGenerator
) {
$this->eccubeConfig = $eccubeConfig;
$this->twig = $twig;
$this->entityManager = $entityManager;
$this->urlGenerator = $urlGenerator;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE => 'custProductDetail',
];
}
public function custProductDetail(EventArgs $event)
{
$Product = $event->getArgument('Product');
$isGroup = false;
foreach ($Product->getProductClasses() as $ProductClass) {
$SaleType = $ProductClass->getSaleType();
if ($SaleType->getId() == $this->eccubeConfig['group_product_sale_type_id']) {
$isGroup = true;
}
}
if ($Product->getCodeMin() == "group" || $Product->getCodeMax() == "group" || $isGroup) {
$url = $this->urlGenerator->generate('product_list', ['group_id' => $Product->getId()]);
header("Location: " . $url);
exit;
}
}
}