app/Plugin/NZCartButtonControl42/EventSubscriber/TemplateEventSubscriber.php line 83

Open in your IDE?
  1. <?php
  2. namespace Plugin\NZCartButtonControl42\EventSubscriber;
  3. use Eccube\Event\TemplateEvent;
  4. use Plugin\NZCartButtonControl42\Repository\CartButtonControlRepository;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. class TemplateEventSubscriber implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @var CartButtonControlRepository
  10.      */
  11.     protected $cartButtonControlRepository;
  12.     public function __construct(CartButtonControlRepository $cartButtonControlRepository)
  13.     {
  14.         $this->cartButtonControlRepository $cartButtonControlRepository;
  15.     }
  16.     /**
  17.      * @return array
  18.      */
  19.     public static function getSubscribedEvents()
  20.     {
  21.         return [
  22.             'Product/list.twig' => 'onProductList',
  23.             'Product/detail.twig' => 'onProductDetail',
  24.         ];
  25.     }
  26.     /**
  27.      * 商品一覧ページでのカートボタン制御
  28.      *
  29.      * @param TemplateEvent $event
  30.      */
  31.     public function onProductList(TemplateEvent $event)
  32.     {
  33.         $parameters $event->getParameters();
  34.         
  35.         // カテゴリが設定されているか確認
  36.         if (isset($parameters['Category']) && $parameters['Category']) {
  37.             $Category $parameters['Category'];
  38.             $categoryId $Category->getId();
  39.             
  40.             // この カテゴリの設定を取得
  41.             $control $this->cartButtonControlRepository->findByCategoryId($categoryId);
  42.             
  43.             if ($control && $control->getHideCartButton()) {
  44.                 $event->setParameter('hide_cart_button'true);
  45.             }
  46.         }
  47.         
  48.         // 検索結果ページの場合、各商品のカテゴリをチェック
  49.         if (isset($parameters['pagination']) && $parameters['pagination']) {
  50.             $hideCartButtons = [];
  51.             
  52.             foreach ($parameters['pagination'] as $Product) {
  53.                 $shouldHide false;
  54.                 
  55.                 foreach ($Product->getProductCategories() as $ProductCategory) {
  56.                     $categoryId $ProductCategory->getCategory()->getId();
  57.                     $control $this->cartButtonControlRepository->findByCategoryId($categoryId);
  58.                     
  59.                     if ($control && $control->getHideCartButton()) {
  60.                         $shouldHide true;
  61.                         break;
  62.                     }
  63.                 }
  64.                 
  65.                 $hideCartButtons[$Product->getId()] = $shouldHide;
  66.             }
  67.             
  68.             $event->setParameter('hide_cart_buttons'$hideCartButtons);
  69.         }
  70.     }
  71.     /**
  72.      * 商品詳細ページでのカートボタン制御
  73.      *
  74.      * @param TemplateEvent $event
  75.      */
  76.     public function onProductDetail(TemplateEvent $event)
  77.     {
  78.         $parameters $event->getParameters();
  79.         
  80.         if (isset($parameters['Product']) && $parameters['Product']) {
  81.             $Product $parameters['Product'];
  82.             $shouldHide false;
  83.             
  84.             // 商品が所属するすべてのカテゴリをチェック
  85.             foreach ($Product->getProductCategories() as $ProductCategory) {
  86.                 $categoryId $ProductCategory->getCategory()->getId();
  87.                 $control $this->cartButtonControlRepository->findByCategoryId($categoryId);
  88.                 
  89.                 if ($control && $control->getHideCartButton()) {
  90.                     $shouldHide true;
  91.                     break;
  92.                 }
  93.             }
  94.             
  95.             if ($shouldHide) {
  96.                 $event->setParameter('hide_cart_button'true);
  97.             }
  98.         }
  99.     }
  100. }