app/Plugin/PiaProductTopics/PiaProductTopicsEvent.php line 24

Open in your IDE?
  1. <?php
  2. namespace Plugin\PiaProductTopics;
  3. use Eccube\Event\TemplateEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. class PiaProductTopicsEvent implements EventSubscriberInterface
  6. {
  7.   /**
  8.    * @return array
  9.    */
  10.   public static function getSubscribedEvents()
  11.   {
  12.     return [
  13.       'Product/detail.twig' => 'onProductDetailRender',
  14.     ];
  15.   }
  16.   /**
  17.    * 【商品詳細】に関連TOPICSを表示
  18.    * @param TemplateEvent $event
  19.    */
  20.   public function onProductDetailRender(TemplateEvent $event)
  21.   {
  22.     $parameters $event->getParameters();
  23.     $Product $parameters["Product"];
  24.     // 1. 商品のカテゴリー一覧を取得
  25.     // 2. シリーズのカテゴリーがあればカテゴリー番号を取得
  26.     $cat_id "";
  27.     $ProductCategories $Product->getProductCategories();
  28.     foreach ($ProductCategories as $ProductCategory) {
  29.       $Category $ProductCategory->getCategory();
  30.       $Parent $Category->getParent();
  31.       if (!empty($Parent) && $Parent->getId()) {
  32.         if ($Parent->getId() == "50") {
  33.           $cat_id $Category->getId();
  34.         }
  35.       }
  36.     }
  37.     // 3. 関連TOPICSのHTML確認
  38.     $topics_html "";
  39.     if (!empty($cat_id)) {
  40.       $url $_SERVER['DOCUMENT_ROOT'] . "/blog/topics_{$cat_id}.html";
  41.       if (is_file($url)) {
  42.         $topics_html file_get_contents($url);
  43.       }
  44.     }
  45.     $parameters["topics_html"] = $topics_html;
  46.     $event->setParameters($parameters);
  47.   }
  48. }