app/Customize/Controller/CustomProductController.php line 243

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  * 
  12.  *  * 2023/11/29
  13.  * カートに商品がある場合は購入不可にする為
  14.  * カートに商品があるか判断するサービスを追加。
  15.  * 2024/02/20 k.saito
  16.  *  スペルのミスを修正
  17.  *  existsProductCheck ⇒ existsProductCheckFromCartService
  18.  * 2024/04/19 k.saito
  19.  *  詳細画面のキャッシュを無効にする
  20.  * 
  21.  */
  22. namespace Customize\Controller;
  23. use Composer\Util\Http\Response;
  24. use Eccube\Entity\BaseInfo;
  25. use Eccube\Entity\Master\ProductStatus;
  26. use Eccube\Entity\Product;
  27. use Eccube\Event\EccubeEvents;
  28. use Eccube\Event\EventArgs;
  29. use Eccube\Form\Type\AddCartType;
  30. use Eccube\Form\Type\SearchProductType;
  31. use Eccube\Repository\BaseInfoRepository;
  32. use Eccube\Repository\CustomerFavoriteProductRepository;
  33. use Eccube\Repository\Master\ProductListMaxRepository;
  34. use Eccube\Repository\ProductRepository;
  35. use Eccube\Service\CartService;
  36. use Eccube\Service\PurchaseFlow\PurchaseContext;
  37. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  38. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  39. use Knp\Component\Pager\PaginatorInterface;
  40. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  41. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  42. use Symfony\Component\HttpFoundation\Request;
  43. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  44. use Symfony\Component\Routing\Annotation\Route;
  45. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  46. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  47. //追加
  48. use Eccube\Controller\ProductController;
  49. use Customize\Service\existsProductCheckFromCartService//カートに商品が入っているか判断する
  50. class CustomProductController extends ProductController
  51. {
  52.     /**
  53.      * @var PurchaseFlow
  54.      */
  55.     protected $purchaseFlow;
  56.     /**
  57.      * @var CustomerFavoriteProductRepository
  58.      */
  59.     protected $customerFavoriteProductRepository;
  60.     /**
  61.      * @var CartService
  62.      */
  63.     protected $cartService;
  64.     /**
  65.      * @var ProductRepository
  66.      */
  67.     protected $productRepository;
  68.     /**
  69.      * @var BaseInfo
  70.      */
  71.     protected $BaseInfo;
  72.     /**
  73.      * @var AuthenticationUtils
  74.      */
  75.     protected $helper;
  76.     /**
  77.      * @var ProductListMaxRepository
  78.      */
  79.     protected $productListMaxRepository;
  80.     private $title '';
  81.     //追加
  82.     /**
  83.      * @var existsProductCheckFromCartService
  84.      */
  85.     protected $existsProductCheckFromCartService;
  86.     /**
  87.      * ProductController constructor.
  88.      *
  89.      * @param PurchaseFlow $cartPurchaseFlow
  90.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  91.      * @param CartService $cartService
  92.      * @param ProductRepository $productRepository
  93.      * @param BaseInfoRepository $baseInfoRepository
  94.      * @param AuthenticationUtils $helper
  95.      * @param ProductListMaxRepository $productListMaxRepository
  96.      */
  97.     public function __construct(
  98.         PurchaseFlow $cartPurchaseFlow,
  99.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  100.         CartService $cartService,
  101.         ProductRepository $productRepository,
  102.         BaseInfoRepository $baseInfoRepository,
  103.         AuthenticationUtils $helper,
  104.         ProductListMaxRepository $productListMaxRepository,
  105.         existsProductCheckFromCartService $existsProductCheckFromCartService
  106.     ) {
  107.         $this->purchaseFlow $cartPurchaseFlow;
  108.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  109.         $this->cartService $cartService;
  110.         $this->productRepository $productRepository;
  111.         $this->BaseInfo $baseInfoRepository->get();
  112.         $this->helper $helper;
  113.         $this->productListMaxRepository $productListMaxRepository;
  114.         $this->existsProductCheckFromCartService $existsProductCheckFromCartService;
  115.     }
  116.     /**
  117.      * 商品詳細画面.
  118.      *
  119.      * @Route("/products/detail/{id}", name="product_detail", methods={"GET"}, requirements={"id" = "\d+"})
  120.      * @Template("Product/detail.twig")
  121.      * @ParamConverter("Product", options={"repository_method" = "findWithSortedClassCategories"})
  122.      *
  123.      * @param Request $request
  124.      * @param Product $Product
  125.      *
  126.      * @return array
  127.      */
  128.     public function detail(Request $requestProduct $Product)
  129.     {
  130.         //追加 カートに商品があるかチェックする
  131.         $existsProduct $this->existsProductCheckFromCartService->check();
  132.         if (!$this->checkVisibility($Product)) {
  133.             throw new NotFoundHttpException();
  134.         }
  135.         $builder $this->formFactory->createNamedBuilder(
  136.             '',
  137.             AddCartType::class,
  138.             null,
  139.             [
  140.                 'product' => $Product,
  141.                 'id_add_product_id' => false,
  142.             ]
  143.         );
  144.         $event = new EventArgs(
  145.             [
  146.                 'builder' => $builder,
  147.                 'Product' => $Product,
  148.             ],
  149.             $request
  150.         );
  151.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE);
  152.         $is_favorite false;
  153.         if ($this->isGranted('ROLE_USER')) {
  154.             $Customer $this->getUser();
  155.             $is_favorite $this->customerFavoriteProductRepository->isFavorite($Customer$Product);
  156.         }
  157.         /*
  158.         return [
  159.             'response' => $response,
  160.             'title' => $this->title,
  161.             'subtitle' => $Product->getName(),
  162.             'form' => $builder->getForm()->createView(),
  163.             'Product' => $Product,
  164.             'is_favorite' => $is_favorite,
  165.             'existsProduct' => $existsProduct, //追加 カートに商品がある場合はtrue
  166.         ];
  167.         */
  168.         // キャッシュを無効にする
  169.         $data = [
  170.             'title' => $this->title,
  171.             'subtitle' => $Product->getName(),
  172.             'form' => $builder->getForm()->createView(),
  173.             'Product' => $Product,
  174.             'is_favorite' => $is_favorite,
  175.             'existsProduct' => $existsProduct//追加 カートに商品がある場合はtrue
  176.         ];
  177.         $response $this->render('Product/detail.twig'$data);
  178.         // レスポンスヘッダにキャッシュを無効にする設定を追加
  179.         $response->headers->set('Cache-Control''no-cache, no-store, must-revalidate');
  180.         $response->headers->set('Pragma''no-cache');
  181.         $response->headers->set('Expires''0');
  182.         // 設定したレスポンスを返す
  183.         return $response;
  184.     }
  185.     /**
  186.      * ページタイトルの設定
  187.      *
  188.      * @param  array|null $searchData
  189.      *
  190.      * @return str
  191.      */
  192.     protected function getPageTitle($searchData)
  193.     {
  194.         if (isset($searchData['name']) && !empty($searchData['name'])) {
  195.             return trans('front.product.search_result');
  196.         } elseif (isset($searchData['category_id']) && $searchData['category_id']) {
  197.             return $searchData['category_id']->getName();
  198.         } else {
  199.             return trans('front.product.all_products');
  200.         }
  201.     }
  202.     /**
  203.      * 閲覧可能な商品かどうかを判定
  204.      *
  205.      * @param Product $Product
  206.      *
  207.      * @return boolean 閲覧可能な場合はtrue
  208.      */
  209.     protected function checkVisibility(Product $Product)
  210.     {
  211.         $is_admin $this->session->has('_security_admin');
  212.         // 管理ユーザの場合はステータスやオプションにかかわらず閲覧可能.
  213.         if (!$is_admin) {
  214.             // 在庫なし商品の非表示オプションが有効な場合.
  215.             // if ($this->BaseInfo->isOptionNostockHidden()) {
  216.             //     if (!$Product->getStockFind()) {
  217.             //         return false;
  218.             //     }
  219.             // }
  220.             // 公開ステータスでない商品は表示しない.
  221.             if ($Product->getStatus()->getId() !== ProductStatus::DISPLAY_SHOW) {
  222.                 return false;
  223.             }
  224.         }
  225.         return true;
  226.     }
  227. }