app/Plugin/NZCartButtonControl42/EventSubscriber/ResponseSubscriber.php line 52

Open in your IDE?
  1. <?php
  2. namespace Plugin\NZCartButtonControl42\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Eccube\Request\Context;
  5. use Plugin\NZCartButtonControl42\Repository\CartButtonControlRepository;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class ResponseSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var CartButtonControlRepository
  13.      */
  14.     protected $cartButtonControlRepository;
  15.     /**
  16.      * @var Context
  17.      */
  18.     protected $context;
  19.     /**
  20.      * @var EntityManagerInterface
  21.      */
  22.     protected $entityManager;
  23.     public function __construct(
  24.         CartButtonControlRepository $cartButtonControlRepository,
  25.         Context $context,
  26.         EntityManagerInterface $entityManager
  27.     ) {
  28.         $this->cartButtonControlRepository $cartButtonControlRepository;
  29.         $this->context $context;
  30.         $this->entityManager $entityManager;
  31.     }
  32.     /**
  33.      * @return array
  34.      */
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             KernelEvents::RESPONSE => ['onResponse', -100], // 優先度を下げてProductContact42の後に実行
  39.         ];
  40.     }
  41.     /**
  42.      * @param ResponseEvent $event
  43.      */
  44.     public function onResponse(ResponseEvent $event)
  45.     {
  46.         if (!$event->isMainRequest()) {
  47.             return;
  48.         }
  49.         // 管理画面は除外
  50.         if ($this->context->isAdmin()) {
  51.             return;
  52.         }
  53.         $request $event->getRequest();
  54.         $route $request->attributes->get('_route');
  55.         // 商品一覧ページまたは商品詳細ページのみ対象
  56.         if (!in_array($route, ['product_list''product_detail'])) {
  57.             return;
  58.         }
  59.         $response $event->getResponse();
  60.         $content $response->getContent();
  61.         // カテゴリIDを取得
  62.         $categoryId null;
  63.         $productId null;
  64.         if ($route === 'product_list') {
  65.             $categoryId $request->attributes->get('category_id');
  66.         } elseif ($route === 'product_detail') {
  67.             $productId $request->attributes->get('id');
  68.         }
  69.         $shouldHide false;
  70.         // 商品一覧の場合
  71.         if ($categoryId) {
  72.             $control $this->cartButtonControlRepository->findByCategoryId($categoryId);
  73.             if ($control && $control->getHideCartButton()) {
  74.                 $shouldHide true;
  75.             }
  76.         }
  77.         // 商品詳細の場合
  78.         if ($productId) {
  79.             $Product $this->entityManager->getRepository(\Eccube\Entity\Product::class)->find($productId);
  80.             
  81.             if ($Product) {
  82.                 foreach ($Product->getProductCategories() as $ProductCategory) {
  83.                     $catId $ProductCategory->getCategory()->getId();
  84.                     $control $this->cartButtonControlRepository->findByCategoryId($catId);
  85.                     
  86.                     if ($control && $control->getHideCartButton()) {
  87.                         $shouldHide true;
  88.                         break;
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.         // カートボタンを非表示にする
  94.         // ProductContact42のお問い合わせボタン(.contact)は除外
  95.         if ($shouldHide) {
  96.             $script '<script>
  97.                 $(function() {
  98.                     var hideCartButtons = function() {
  99.                         $("#form1 .ec-productRole__btn").each(function() {
  100.                             var $btn = $(this);
  101.                             
  102.                             // お気に入りボタンを含む要素はスキップ
  103.                             if ($btn.find(".ec-blockBtn--favorite").length > 0) {
  104.                                 return;
  105.                             }
  106.                             
  107.                             // お問い合わせボタンを含む要素はスキップ
  108.                             if ($btn.find("button.contact").length > 0) {
  109.                                 return;
  110.                             }
  111.                             
  112.                             // それ以外の全てのボタン(カート・品切れボタン)を削除
  113.                             $btn.remove();
  114.                         });
  115.                         
  116.                         // 一覧ページ用
  117.                         $(".ec-shelfGrid .ec-productRole__btn, .ec-shelfGrid__item .ec-productRole__btn").each(function() {
  118.                             var $btn = $(this);
  119.                             if ($btn.find("button.contact").length === 0 && 
  120.                                 $btn.find(".ec-blockBtn--favorite").length === 0) {
  121.                                 $btn.remove();
  122.                             }
  123.                         });
  124.                     };
  125.                     
  126.                     // 初回実行
  127.                     hideCartButtons();
  128.                 });
  129.             </script>';
  130.             
  131.             // </body>タグの直前にスクリプトを挿入
  132.             $content str_replace('</body>'$script '</body>'$content);
  133.             $response->setContent($content);
  134.         }
  135.     }
  136. }