Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active October 14, 2020 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurozumi/81dd1832e2eec1a5ca41536fd9dcfda3 to your computer and use it in GitHub Desktop.
Save kurozumi/81dd1832e2eec1a5ca41536fd9dcfda3 to your computer and use it in GitHub Desktop.
イベントで商品IDをクッキーの保存・取得を行う
<?php
namespace Customize\EventSubscriber;
use Customize\Service\CheckedProduct;
use Eccube\Common\EccubeConfig;
use Eccube\Event\TemplateEvent;
use Eccube\Repository\ProductRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* 商品詳細ページにアクセスしたときに閲覧履歴の保存と取得をする
*
* Class ProductSubscriber
* @package Customize\EventSubscriber
*/
class ProductSubscriber implements EventSubscriberInterface
{
/**
* @var ProductRepository
*/
private $productRepository;
/**
* @var CheckedProduct
*/
private $checkedProduct;
/**
* @var EccubeConfig
*/
private $eccubeConfig;
public function __construct(
ProductRepository $productRepository,
CheckedProduct $checkedProduct,
EccubeConfig $eccubeConfig
)
{
$this->productRepository = $productRepository;
$this->checkedProduct = $checkedProduct;
$this->eccubeConfig = $eccubeConfig;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
// TODO: Implement getSubscribedEvents() method.
return [
KernelEvents::RESPONSE => 'onKernelResponse',
'Product/detail.twig' => 'onTemplateProductDetail'
];
}
/**
* クッキーに商品IDを保存
*
* @param FilterResponseEvent $event
* @throws \Exception
*/
public function onKernelResponse(FilterResponseEvent $event)
{
if(false === $event->isMasterRequest()) {
return;
}
if($event->getRequest()->get('_route') !== 'product_detail') {
return;
}
if($product_id = $event->getRequest()->get('id')) {
$Product = $this->productRepository->find($product_id);
if(null === $Product) {
return;
}
// 商品IDを追加
$this->checkedProduct->addProduct($Product);
// 保持期間1ヶ月
$expire = new \DateTime();
$expire->modify("1 month");
// Cookie作成
$cookie = new Cookie(
$this->checkedProduct->getCookieName(),
implode(',', $this->checkedProduct->getProducts()->toArray()),
$expire,
$this->eccubeConfig['env(ECCUBE_COOKIE_PATH)']
);
$response = $this->checkedProduct->setCookie($event->getResponse(), $cookie);
// Cookieをレスポンスにセット
$event->setResponse($response);
}
}
/**
* クッキーから商品IDを取得してTwigに値を渡す
*
* @param TemplateEvent $event
*/
public function onTemplateProductDetail(TemplateEvent $event)
{
$checkedProducts = [];
if($products = $this->checkedProduct->getProducts()) {
foreach($products as $product) {
if($product = $this->productRepository->find($product)) {
$checkedProducts[] = $product;
}
}
}
$event->setParameter('checkedProducts', $checkedProducts);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment