<?php
namespace Plugin\PiaProductTopics;
use Eccube\Event\TemplateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PiaProductTopicsEvent implements EventSubscriberInterface
{
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Product/detail.twig' => 'onProductDetailRender',
];
}
/**
* 【商品詳細】に関連TOPICSを表示
* @param TemplateEvent $event
*/
public function onProductDetailRender(TemplateEvent $event)
{
$parameters = $event->getParameters();
$Product = $parameters["Product"];
// 1. 商品のカテゴリー一覧を取得
// 2. シリーズのカテゴリーがあればカテゴリー番号を取得
$cat_id = "";
$ProductCategories = $Product->getProductCategories();
foreach ($ProductCategories as $ProductCategory) {
$Category = $ProductCategory->getCategory();
$Parent = $Category->getParent();
if (!empty($Parent) && $Parent->getId()) {
if ($Parent->getId() == "50") {
$cat_id = $Category->getId();
}
}
}
// 3. 関連TOPICSのHTML確認
$topics_html = "";
if (!empty($cat_id)) {
$url = $_SERVER['DOCUMENT_ROOT'] . "/blog/topics_{$cat_id}.html";
if (is_file($url)) {
$topics_html = file_get_contents($url);
}
}
$parameters["topics_html"] = $topics_html;
$event->setParameters($parameters);
}
}