<?phpnamespace Plugin\NZCartButtonControl42\EventSubscriber;use Doctrine\ORM\EntityManagerInterface;use Eccube\Request\Context;use Plugin\NZCartButtonControl42\Repository\CartButtonControlRepository;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpKernel\Event\ResponseEvent;use Symfony\Component\HttpKernel\KernelEvents;class ResponseSubscriber implements EventSubscriberInterface{ /** * @var CartButtonControlRepository */ protected $cartButtonControlRepository; /** * @var Context */ protected $context; /** * @var EntityManagerInterface */ protected $entityManager; public function __construct( CartButtonControlRepository $cartButtonControlRepository, Context $context, EntityManagerInterface $entityManager ) { $this->cartButtonControlRepository = $cartButtonControlRepository; $this->context = $context; $this->entityManager = $entityManager; } /** * @return array */ public static function getSubscribedEvents() { return [ KernelEvents::RESPONSE => ['onResponse', -100], // 優先度を下げてProductContact42の後に実行 ]; } /** * @param ResponseEvent $event */ public function onResponse(ResponseEvent $event) { if (!$event->isMainRequest()) { return; } // 管理画面は除外 if ($this->context->isAdmin()) { return; } $request = $event->getRequest(); $route = $request->attributes->get('_route'); // 商品一覧ページまたは商品詳細ページのみ対象 if (!in_array($route, ['product_list', 'product_detail'])) { return; } $response = $event->getResponse(); $content = $response->getContent(); // カテゴリIDを取得 $categoryId = null; $productId = null; if ($route === 'product_list') { $categoryId = $request->attributes->get('category_id'); } elseif ($route === 'product_detail') { $productId = $request->attributes->get('id'); } $shouldHide = false; // 商品一覧の場合 if ($categoryId) { $control = $this->cartButtonControlRepository->findByCategoryId($categoryId); if ($control && $control->getHideCartButton()) { $shouldHide = true; } } // 商品詳細の場合 if ($productId) { $Product = $this->entityManager->getRepository(\Eccube\Entity\Product::class)->find($productId); if ($Product) { foreach ($Product->getProductCategories() as $ProductCategory) { $catId = $ProductCategory->getCategory()->getId(); $control = $this->cartButtonControlRepository->findByCategoryId($catId); if ($control && $control->getHideCartButton()) { $shouldHide = true; break; } } } } // カートボタンを非表示にする // ProductContact42のお問い合わせボタン(.contact)は除外 if ($shouldHide) { $script = '<script> $(function() { var hideCartButtons = function() { $("#form1 .ec-productRole__btn").each(function() { var $btn = $(this); // お気に入りボタンを含む要素はスキップ if ($btn.find(".ec-blockBtn--favorite").length > 0) { return; } // お問い合わせボタンを含む要素はスキップ if ($btn.find("button.contact").length > 0) { return; } // それ以外の全てのボタン(カート・品切れボタン)を削除 $btn.remove(); }); // 一覧ページ用 $(".ec-shelfGrid .ec-productRole__btn, .ec-shelfGrid__item .ec-productRole__btn").each(function() { var $btn = $(this); if ($btn.find("button.contact").length === 0 && $btn.find(".ec-blockBtn--favorite").length === 0) { $btn.remove(); } }); }; // 初回実行 hideCartButtons(); }); </script>'; // </body>タグの直前にスクリプトを挿入 $content = str_replace('</body>', $script . '</body>', $content); $response->setContent($content); } }}