app/Customize/Controller/ProductController.php line 130

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. namespace Customize\Controller;
  13. use Eccube\Entity\BaseInfo;
  14. use Eccube\Entity\Master\ProductStatus;
  15. use Eccube\Entity\Product;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Eccube\Form\Type\AddCartType;
  19. use Eccube\Form\Type\Master\ProductListMaxType;
  20. use Eccube\Form\Type\Master\ProductListOrderByType;
  21. use Eccube\Form\Type\SearchProductType;
  22. use Eccube\Repository\BaseInfoRepository;
  23. use Eccube\Repository\CustomerFavoriteProductRepository;
  24. use Eccube\Repository\Master\ProductListMaxRepository;
  25. use Eccube\Repository\ProductRepository;
  26. use Eccube\Repository\ProductCategoryRepository;
  27. use Eccube\Service\CartService;
  28. use Eccube\Service\PurchaseFlow\PurchaseContext;
  29. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  30. use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
  31. use Knp\Component\Pager\Paginator;
  32. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  33. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  34. use Symfony\Component\HttpFoundation\Request;
  35. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  36. use Symfony\Component\Routing\Annotation\Route;
  37. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  38. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  39. use Doctrine\ORM\EntityManagerInterface;
  40. //以下Customize様Use
  41. use Eccube\Controller\AbstractController;
  42. class ProductController extends AbstractController
  43. {
  44.     /**
  45.      * @var PurchaseFlow
  46.      */
  47.     protected $purchaseFlow;
  48.     /**
  49.      * @var CustomerFavoriteProductRepository
  50.      */
  51.     protected $customerFavoriteProductRepository;
  52.     /**
  53.      * @var CartService
  54.      */
  55.     protected $cartService;
  56.     /**
  57.      * @var ProductRepository
  58.      */
  59.     protected $productRepository;
  60.     /**
  61.      * @var BaseInfo
  62.      */
  63.     protected $BaseInfo;
  64.     /**
  65.      * @var AuthenticationUtils
  66.      */
  67.     protected $helper;
  68.     /**
  69.      * @var ProductListMaxRepository
  70.      */
  71.     protected $productListMaxRepository;
  72.     /**
  73.      * @var EntityManagerInterface
  74.      */
  75.     protected $entityManager;
  76.     private $title '';
  77.     /**
  78.      * ProductController constructor.
  79.      *
  80.      * @param PurchaseFlow $cartPurchaseFlow
  81.      * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  82.      * @param CartService $cartService
  83.      * @param ProductRepository $productRepository
  84.      * @param ProductCategoryRepository $productRepository
  85.      * @param BaseInfoRepository $baseInfoRepository
  86.      * @param AuthenticationUtils $helper
  87.      * @param ProductListMaxRepository $productListMaxRepository
  88.      */
  89.     public function __construct(
  90.         PurchaseFlow $cartPurchaseFlow,
  91.         CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  92.         CartService $cartService,
  93.         ProductRepository $productRepository,
  94.         ProductCategoryRepository $productCategoryRepository,
  95.         BaseInfoRepository $baseInfoRepository,
  96.         AuthenticationUtils $helper,
  97.         ProductListMaxRepository $productListMaxRepository,
  98.         EntityManagerInterface $entityManager
  99.     ) {
  100.         $this->purchaseFlow $cartPurchaseFlow;
  101.         $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  102.         $this->cartService $cartService;
  103.         $this->productRepository $productRepository;
  104.         $this->BaseInfo $baseInfoRepository->get();
  105.         $this->helper $helper;
  106.         $this->productListMaxRepository $productListMaxRepository;
  107.         $this->entityManager $entityManager;
  108.         $this->productCategoryRepository $productCategoryRepository;
  109.     }
  110.     /**
  111.      * 商品一覧画面.
  112.      *
  113.      * @Route("/products/list", name="product_list")
  114.      * @Template("Product/list.twig")
  115.      */
  116.     public function index(Request $requestPaginator $paginator)
  117.     {
  118.         // Doctrine SQLFilter
  119.         if ($this->BaseInfo->isOptionNostockHidden()) {
  120.             $this->entityManager->getFilters()->enable('option_nostock_hidden');
  121.         }
  122.         // handleRequestは空のqueryの場合は無視するため
  123.         if ($request->getMethod() === 'GET') {
  124.             $request->query->set('pageno'$request->query->get('pageno'''));
  125.         }
  126.         // searchForm
  127.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  128.         $builder $this->formFactory->createNamedBuilder(''SearchProductType::class);
  129.         if ($request->getMethod() === 'GET') {
  130.             $builder->setMethod('GET');
  131.         }
  132.         $event = new EventArgs(
  133.             [
  134.                 'builder' => $builder,
  135.             ],
  136.             $request
  137.         );
  138.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_INITIALIZE$event);
  139.         /* @var $searchForm \Symfony\Component\Form\FormInterface */
  140.         $searchForm $builder->getForm();
  141.         $searchForm->handleRequest($request);
  142.         // paginator
  143.         $searchData $searchForm->getData();
  144.         $qb $this->productRepository->getQueryBuilderBySearchData($searchData);
  145.         $event = new EventArgs(
  146.             [
  147.                 'searchData' => $searchData,
  148.                 'qb' => $qb,
  149.             ],
  150.             $request
  151.         );
  152.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_SEARCH$event);
  153.         $searchData $event->getArgument('searchData');
  154.         $query $qb->getQuery()
  155.             ->useResultCache(true$this->eccubeConfig['eccube_result_cache_lifetime_short']);
  156.         /** @var SlidingPagination $pagination */
  157.         $pagination $paginator->paginate(
  158.             $query,
  159.             !empty($searchData['pageno']) ? $searchData['pageno'] : 1,
  160.             !empty($searchData['disp_number']) ? $searchData['disp_number']->getId() : $this->productListMaxRepository->findOneBy([], ['sort_no' => 'ASC'])->getId()
  161.         );
  162.         $ids = [];
  163.         foreach ($pagination as $Product) {
  164.             $ids[] = $Product->getId();
  165.         }
  166.         $ProductsAndClassCategories $this->productRepository->findProductsWithSortedClassCategories($ids'p.id');
  167.         // addCart form
  168.         $forms = [];
  169.         foreach ($pagination as $Product) {
  170.             /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  171.             $builder $this->formFactory->createNamedBuilder(
  172.                 '',
  173.                 AddCartType::class,
  174.                 null,
  175.                 [
  176.                     'product' => $ProductsAndClassCategories[$Product->getId()],
  177.                     'allow_extra_fields' => true,
  178.                 ]
  179.             );
  180.             $addCartForm $builder->getForm();
  181.             $forms[$Product->getId()] = $addCartForm->createView();
  182.         }
  183.         // 表示件数
  184.         $builder $this->formFactory->createNamedBuilder(
  185.             'disp_number',
  186.             ProductListMaxType::class,
  187.             null,
  188.             [
  189.                 'required' => false,
  190.                 'allow_extra_fields' => true,
  191.             ]
  192.         );
  193.         if ($request->getMethod() === 'GET') {
  194.             $builder->setMethod('GET');
  195.         }
  196.         $event = new EventArgs(
  197.             [
  198.                 'builder' => $builder,
  199.             ],
  200.             $request
  201.         );
  202.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_DISP$event);
  203.         $dispNumberForm $builder->getForm();
  204.         $dispNumberForm->handleRequest($request);
  205.         // ソート順
  206.         $builder $this->formFactory->createNamedBuilder(
  207.             'orderby',
  208.             ProductListOrderByType::class,
  209.             null,
  210.             [
  211.                 'required' => false,
  212.                 'allow_extra_fields' => true,
  213.             ]
  214.         );
  215.         if ($request->getMethod() === 'GET') {
  216.             $builder->setMethod('GET');
  217.         }
  218.         $event = new EventArgs(
  219.             [
  220.                 'builder' => $builder,
  221.             ],
  222.             $request
  223.         );
  224.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_INDEX_ORDER$event);
  225.         $orderByForm $builder->getForm();
  226.         $orderByForm->handleRequest($request);
  227.         $Category $searchForm->get('category_id')->getData();
  228.         // print_r($Category);
  229.         /*
  230.          *mv画像のパスの設定
  231.          */
  232.          if(!empty($_GET['category_id'])){
  233.              $mv_path_pc "/html/template/raspia/img/low/low_cate_id_".$_GET['category_id'].".webp";
  234.              $mv_path_sp "/html/template/raspia/img/low/low_cate_id_".$_GET['category_id']."_sp.webp";
  235.          }else{
  236.              $mv_path_pc "/html/template/raspia/img/low/low_cate_allitem.webp";
  237.              $mv_path_sp "/html/template/raspia/img/low/low_cate_allitem_sp.webp";
  238.          }
  239.         //ファイルがある場合は対応する命名規則のファイルない場合はsample画像
  240.         if(!file_exists($_SERVER['DOCUMENT_ROOT'].$mv_path_pc)){
  241.             $mv_path_pc "/html/template/raspia/img/low/low_cate_noimage.webp";
  242.         }
  243.         if(!file_exists($_SERVER['DOCUMENT_ROOT'].$mv_path_sp)){
  244.             $mv_path_sp "/html/template/raspia/img/low/low_cate_noimage_sp.webp";
  245.         }
  246.         $Categories = [];
  247.         if($request->query->get('categories')){
  248.             $subtitle "";
  249.             $categories $this->getCategories(explode(',',$request->query->get('categories')));
  250.             $category_names = [];
  251.             foreach($categories as $key => $category){
  252.                 if($category!==null){
  253.                     $category_names[] = $category->getName();
  254.                 } else{
  255.                     return $this->redirect('https://raspia.com/products/list',301);
  256.                 }
  257.             }
  258.             $Categories['title'] = implode(" ",$category_names);
  259.             $Categories['keyword'] = implode(",",$category_names);
  260.         }
  261.         $subtitle $this->getPageTitle($searchData);
  262.         if($pagination->count() <= && isset($request->query->all()['categories'])){
  263.             if($request->query->all()['categories'] != ""
  264.             ){
  265.                 $ids explode(',',$request->query->all()['categories']);
  266.                 $highPriorityIds = [];
  267.                 foreach($ids as $key => $id){
  268.                     $category $this->getCategory($id);
  269.                     if(!empty($category->getParent())){
  270.                         if(in_array($category->getParent()->getId(),[12,24,44])){
  271.                             $highPriorityIds[] = $id;
  272.                             unset($ids[$key]);
  273.                         }
  274.                     }
  275.                 }
  276.                 $ignoreIds = [];
  277.                 foreach($pagination as $key => $product){
  278.                     $ignoreIds[] = $product->getId();
  279.                 }
  280.                 $HighPrioritySimilarProducts = [];
  281.                 foreach($highPriorityIds as $key => $id){
  282.                     $qb $this->entityManager->createQueryBuilder([]);
  283.                     $query $qb->select("p")
  284.                         ->from("Eccube\\Entity\\Product","p")
  285.                         ->innerJoin("p.ProductCategories","pc")
  286.                         ->andWhere("p.Status = 1")
  287.                         ->andWhere($qb->expr()->in('pc.Category'':Categories'))
  288.                         ->setParameter("Categories",[$id])
  289.                         ->groupBy("p.id")
  290.                         ;
  291.                     if(count($ignoreIds)) $query->andWhere($qb->expr()->notIn("p.id",$ignoreIds));
  292.                      $HighPrioritySimilarProducts array_merge($HighPrioritySimilarProducts,$query->getQuery()->getResult());
  293.                 }
  294.                 foreach($HighPrioritySimilarProducts as $key => $product){
  295.                     $ignoreIds[] = $product->getId();
  296.                 }
  297.                 $SimilarProducts = [];
  298.                 $max = (count($HighPrioritySimilarProducts) > 15)?015 count($HighPrioritySimilarProducts) ;
  299.                 $qb $this->entityManager->createQueryBuilder([]);
  300.                 $query $qb->select("p")
  301.                     ->from("Eccube\\Entity\\Product","p")
  302.                     ->innerJoin("p.ProductCategories","pc")
  303.                     ->andWhere($qb->expr()->in('pc.Category'':Categories'))
  304.                     ->andWhere('p.Status = 1')
  305.                     ->setParameter("Categories",$ids)
  306.                     ->groupBy("p.id")
  307.                     ;
  308.                 if(count($ignoreIds)) $query->andWhere($qb->expr()->notIn("p.id",$ignoreIds));
  309.                 $SimilarProducts $query->getQuery()->getResult();
  310.                 shuffle($SimilarProducts);
  311.                 // unset();
  312.                 $SimilarProducts array_unique($SimilarProducts);
  313.                 $SimilarProducts array_values($SimilarProducts);
  314.                 $SimilarProducts array_slice($SimilarProducts,0,$max);
  315.                 $SimilarProducts array_merge($HighPrioritySimilarProducts,$SimilarProducts);
  316.             }else{
  317.                 $SimilarProducts "";
  318.             }
  319.         }else{
  320.             $SimilarProducts "";
  321.         }
  322.         $Params $this->getParams();
  323.         $SearchKeywords implode(', ',$this->getSearchKeywords($Params));
  324.         return [
  325.             'subtitle' => $subtitle,
  326.             'pagination' => $pagination,
  327.             'mv_path_pc' => $mv_path_pc,
  328.             'mv_path_sp' => $mv_path_sp,
  329.             'search_form' => $searchForm->createView(),
  330.             'disp_number_form' => $dispNumberForm->createView(),
  331.             'order_by_form' => $orderByForm->createView(),
  332.             'forms' => $forms,
  333.             'Category' => $Category,
  334.             'Categories' => $Categories,
  335.             'SimilarProducts' => $SimilarProducts,
  336.             'Params' => $Params,
  337.             'SearchKeywords' => $SearchKeywords,
  338.         ];
  339.     }
  340.     private function getParams(){
  341.         // $Params = $_GET;
  342.         if(isset($_GET['category_id']) && !empty($_GET['category_id'])){
  343.           $_GET['multi_category'][$_GET['category_id']] = $_GET['category_id'];
  344.         }
  345.         if(isset($_GET['categories']) && !empty($_GET['categories'])){
  346.             $categories explode(',',$_GET['categories']);
  347.             foreach ($categories as $key => $category) {
  348.                 $_GET['multi_category'][$category] = $category;
  349.             }
  350.         }
  351.         // if(isset($Params['mu']))
  352.         return $_GET;
  353.     }
  354.     private function getSearchKeywords($params){
  355.         $qb $this->entityManager->createQueryBuilder([]);
  356.         $query $qb->select("c")
  357.             ->from("Eccube\\Entity\\Category","c")
  358.             ->getQuery();
  359.         $allCategories $query->getResult();
  360.         $keywords = [];
  361.         //カテゴリの取得
  362.         if(isset($params['multi_category'])  && !empty($params['multi_category'])){
  363.             foreach($allCategories as $key => $category){
  364.                 if(in_array($category->getId(),$params['multi_category'])){
  365.                     $keywords[] = $category->getName();
  366.                 }
  367.             }
  368.         }
  369.         //キーワード
  370.         if(isset($params['name']) && !empty($params['name'])){$keywords[] = $params['name'];}
  371.         //価格の絞り込み
  372.         if(isset($params['pricerange'])){
  373.             foreach($params['pricerange'] as $key => $pricerange){
  374.                 if(empty($pricerange)) break;
  375.                 $range explode('-',$pricerange);
  376.                 $keywords[] = number_format($range[0])."円 〜 ".number_format($range[1])."円";
  377.             }
  378.         }
  379.         if(count($keywords) == 0)$keywords[] ="全ての商品";
  380.         return $keywords;
  381.     }
  382.     private function getSearchCategories(){
  383.         $qb $this->entityManager->createQueryBuilder([]);
  384.         $query $qb->select("c")
  385.             ->from("Eccube\\Entity\\Category","c")
  386.             ->getQuery();
  387.         $allCategories $query->getResult();
  388.         $SearchCategories = [];
  389.         foreach( $allCategories as $keyu => $category ){
  390.             if(in_array($category->getId(),[7,137,14,113,115])){
  391.                 $SearchCategories['item'][$category->getId()] = $category;
  392.             }elseif($category->getId() == 24){
  393.                 $SearchCategories['jewely'][$category->getId()] = $category;
  394.             }elseif($category->getId() == 44){
  395.                 $SearchCategories['birthStone'][$category->getId()] = $category;
  396.             }elseif($category->getId() == 12){
  397.                 $SearchCategories['color'][$category->getId()] = $category;
  398.             }elseif($category->getId() == 6){
  399.                 $SearchCategories['series'][$category->getId()] = $category;
  400.             }elseif($category->getId() == 41){
  401.                 $SearchCategories['material'][$category->getId()] = $category;
  402.             }
  403.         }
  404.         return $SearchCategories;
  405.     }
  406.     /*
  407.      * カテゴリの名前を配列で取得
  408.      *
  409.      */
  410.     private function getCategories($category_ids = []){
  411.         $categories = [];
  412.         foreach($category_ids as $key => $category_id){
  413.             $qb $this->entityManager->createQueryBuilder([]);
  414.             $query $qb->select("c")
  415.                 ->from("Eccube\\Entity\\Category","c")
  416.                 ->where('c.id = :id')
  417.                 ->setParameter('id',$category_id)
  418.                 ->getQuery();
  419.             $categories[] = $query->getResult()[0];
  420.         }
  421.         return $categories;
  422.     }
  423.     /*
  424.      * カテゴリの名前を配列で取得
  425.      *
  426.      */
  427.     private function getCategory($category_id ""){
  428.         $qb $this->entityManager->createQueryBuilder([]);
  429.         $query $qb->select("c")
  430.             ->from("Eccube\\Entity\\Category","c")
  431.             ->where('c.id = :id')
  432.             ->setParameter('id',$category_id)
  433.             ->getQuery();
  434.         return $query->getResult()[0];
  435.     }
  436.     /**
  437.      * 商品詳細画面.
  438.      *
  439.      * @Route("/products/detail/{id}", name="product_detail", methods={"GET"}, requirements={"id" = "\d+"})
  440.      * @Template("Product/detail.twig")
  441.      * @ParamConverter("Product", options={"repository_method" = "findWithSortedClassCategories"})
  442.      *
  443.      * @param Request $request
  444.      * @param Product $Product
  445.      *
  446.      * @return array
  447.      */
  448.     public function detail(Request $requestProduct $Product)
  449.     {
  450.         if (!$this->checkVisibility($Product)) {
  451.             throw new NotFoundHttpException();
  452.         }
  453.         $builder $this->formFactory->createNamedBuilder(
  454.             '',
  455.             AddCartType::class,
  456.             null,
  457.             [
  458.                 'product' => $Product,
  459.                 'id_add_product_id' => false,
  460.             ]
  461.         );
  462.         $event = new EventArgs(
  463.             [
  464.                 'builder' => $builder,
  465.                 'Product' => $Product,
  466.             ],
  467.             $request
  468.         );
  469.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE$event);
  470.         $is_favorite false;
  471.         if ($this->isGranted('ROLE_USER')) {
  472.             $Customer $this->getUser();
  473.             $is_favorite $this->customerFavoriteProductRepository->isFavorite($Customer$Product);
  474.         }
  475.         //スライダー画像の
  476.         $SliderImages = [];
  477.         if($Product->getSlider()){
  478.             $SliderImages explode("\n",$Product->getSlider());
  479.         }
  480.         return [
  481.             'title' => $this->title,
  482.             'subtitle' => $Product->getName(),
  483.             'form' => $builder->getForm()->createView(),
  484.             'Product' => $Product,
  485.             'is_favorite' => $is_favorite,
  486.             'SliderImages' => $SliderImages,
  487.         ];
  488.     }
  489.     /**
  490.      * お気に入り追加.
  491.      *
  492.      * @Route("/products/add_favorite/{id}", name="product_add_favorite", requirements={"id" = "\d+"})
  493.      */
  494.     public function addFavorite(Request $requestProduct $Product)
  495.     {
  496.         $this->checkVisibility($Product);
  497.         $event = new EventArgs(
  498.             [
  499.                 'Product' => $Product,
  500.             ],
  501.             $request
  502.         );
  503.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_INITIALIZE$event);
  504.         if ($this->isGranted('ROLE_USER')) {
  505.             $Customer $this->getUser();
  506.             $this->customerFavoriteProductRepository->addFavorite($Customer$Product);
  507.             $this->session->getFlashBag()->set('product_detail.just_added_favorite'$Product->getId());
  508.             $event = new EventArgs(
  509.                 [
  510.                     'Product' => $Product,
  511.                 ],
  512.                 $request
  513.             );
  514.             $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE$event);
  515.             return $this->redirectToRoute('product_detail', ['id' => $Product->getId()]);
  516.         } else {
  517.             // 非会員の場合、ログイン画面を表示
  518.             //  ログイン後の画面遷移先を設定
  519.             $this->setLoginTargetPath($this->generateUrl('product_add_favorite', ['id' => $Product->getId()], UrlGeneratorInterface::ABSOLUTE_URL));
  520.             $this->session->getFlashBag()->set('eccube.add.favorite'true);
  521.             $event = new EventArgs(
  522.                 [
  523.                     'Product' => $Product,
  524.                 ],
  525.                 $request
  526.             );
  527.             $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_FAVORITE_ADD_COMPLETE$event);
  528.             return $this->redirectToRoute('mypage_login');
  529.         }
  530.     }
  531.     /**
  532.      * カートに追加.
  533.      *
  534.      * @Route("/products/add_cart/{id}", name="product_add_cart", methods={"POST"}, requirements={"id" = "\d+"})
  535.      */
  536.     public function addCart(Request $requestProduct $Product)
  537.     {
  538.         // エラーメッセージの配列
  539.         $errorMessages = [];
  540.         if (!$this->checkVisibility($Product)) {
  541.             throw new NotFoundHttpException();
  542.         }
  543.         $builder $this->formFactory->createNamedBuilder(
  544.             '',
  545.             AddCartType::class,
  546.             null,
  547.             [
  548.                 'product' => $Product,
  549.                 'id_add_product_id' => false,
  550.             ]
  551.         );
  552.         $event = new EventArgs(
  553.             [
  554.                 'builder' => $builder,
  555.                 'Product' => $Product,
  556.             ],
  557.             $request
  558.         );
  559.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_CART_ADD_INITIALIZE$event);
  560.         /* @var $form \Symfony\Component\Form\FormInterface */
  561.         $form $builder->getForm();
  562.         $form->handleRequest($request);
  563.         if (!$form->isValid()) {
  564.             throw new NotFoundHttpException();
  565.         }
  566.         $addCartData $form->getData();
  567.         log_info(
  568.             'カート追加処理開始',
  569.             [
  570.                 'product_id' => $Product->getId(),
  571.                 'product_class_id' => $addCartData['product_class_id'],
  572.                 'quantity' => $addCartData['quantity'],
  573.             ]
  574.         );
  575.         // カートへ追加
  576.         $this->cartService->addProduct($addCartData['product_class_id'], $addCartData['quantity']);
  577.         // 明細の正規化
  578.         $Carts $this->cartService->getCarts();
  579.         foreach ($Carts as $Cart) {
  580.             $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  581.             // 復旧不可のエラーが発生した場合は追加した明細を削除.
  582.             if ($result->hasError()) {
  583.                 $this->cartService->removeProduct($addCartData['product_class_id']);
  584.                 foreach ($result->getErrors() as $error) {
  585.                     $errorMessages[] = $error->getMessage();
  586.                 }
  587.             }
  588.             foreach ($result->getWarning() as $warning) {
  589.                 $errorMessages[] = $warning->getMessage();
  590.             }
  591.         }
  592.         $this->cartService->save();
  593.         log_info(
  594.             'カート追加処理完了',
  595.             [
  596.                 'product_id' => $Product->getId(),
  597.                 'product_class_id' => $addCartData['product_class_id'],
  598.                 'quantity' => $addCartData['quantity'],
  599.             ]
  600.         );
  601.         $event = new EventArgs(
  602.             [
  603.                 'form' => $form,
  604.                 'Product' => $Product,
  605.             ],
  606.             $request
  607.         );
  608.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_PRODUCT_CART_ADD_COMPLETE$event);
  609.         if ($event->getResponse() !== null) {
  610.             return $event->getResponse();
  611.         }
  612.         if ($request->isXmlHttpRequest()) {
  613.             // ajaxでのリクエストの場合は結果をjson形式で返す。
  614.             // 初期化
  615.             $done null;
  616.             $messages = [];
  617.             if (empty($errorMessages)) {
  618.                 // エラーが発生していない場合
  619.                 $done true;
  620.                 array_push($messagestrans('front.product.add_cart_complete'));
  621.             } else {
  622.                 // エラーが発生している場合
  623.                 $done false;
  624.                 $messages $errorMessages;
  625.             }
  626.             return $this->json(['done' => $done'messages' => $messages]);
  627.         } else {
  628.             // ajax以外でのリクエストの場合はカート画面へリダイレクト
  629.             foreach ($errorMessages as $errorMessage) {
  630.                 $this->addRequestError($errorMessage);
  631.             }
  632.             return $this->redirectToRoute('cart');
  633.         }
  634.     }
  635.     /**
  636.      * ページタイトルの設定
  637.      *
  638.      * @param  null|array $searchData
  639.      *
  640.      * @return str
  641.      */
  642.     protected function getPageTitle($searchData)
  643.     {
  644.         if (
  645.             (isset($searchData['name']) && !empty($searchData['name'])) ||
  646.             (isset($_GET['multi_category']) && !empty($_GET['multi_category']))
  647.         ) {
  648.             return trans('front.product.search_result');
  649.         } elseif (isset($searchData['category_id']) && $searchData['category_id']) {
  650.             return $searchData['category_id']->getName();
  651.         }else {
  652.             return trans('front.product.all_products');
  653.         }
  654.     }
  655.     /**
  656.      * 閲覧可能な商品かどうかを判定
  657.      *
  658.      * @param Product $Product
  659.      *
  660.      * @return boolean 閲覧可能な場合はtrue
  661.      */
  662.     protected function checkVisibility(Product $Product)
  663.     {
  664.         $is_admin $this->session->has('_security_admin');
  665.         // 管理ユーザの場合はステータスやオプションにかかわらず閲覧可能.
  666.         if (!$is_admin) {
  667.             // 在庫なし商品の非表示オプションが有効な場合.
  668.             // if ($this->BaseInfo->isOptionNostockHidden()) {
  669.             //     if (!$Product->getStockFind()) {
  670.             //         return false;
  671.             //     }
  672.             // }
  673.             // 公開ステータスでない商品は表示しない.
  674.             if ($Product->getStatus()->getId() !== ProductStatus::DISPLAY_SHOW) {
  675.                 return false;
  676.             }
  677.         }
  678.         return true;
  679.     }
  680. }