app/Customize/Controller/CartController.php line 85

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\Controller\AbstractController;
  14. use Eccube\Entity\BaseInfo;
  15. use Eccube\Entity\ProductClass;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Eccube\Repository\BaseInfoRepository;
  19. use Eccube\Repository\ProductClassRepository;
  20. use Eccube\Service\CartService;
  21. use Eccube\Service\PurchaseFlow\PurchaseContext;
  22. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  23. use Eccube\Service\PurchaseFlow\PurchaseFlowResult;
  24. use Eccube\Service\OrderHelper;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. class CartController extends AbstractController
  29. {
  30.     /**
  31.      * @var ProductClassRepository
  32.      */
  33.     protected $productClassRepository;
  34.     /**
  35.      * @var CartService
  36.      */
  37.     protected $cartService;
  38.     /**
  39.      * @var PurchaseFlow
  40.      */
  41.     protected $purchaseFlow;
  42.     /**
  43.      * @var BaseInfo
  44.      */
  45.     protected $baseInfo;
  46.     /**
  47.      * @var string 非会員情報を保持するセッションのキー
  48.      */
  49.     const SESSION_NON_MEMBER 'eccube.front.shopping.nonmember';
  50.     /**
  51.      * CartController constructor.
  52.      *
  53.      * @param ProductClassRepository $productClassRepository
  54.      * @param CartService $cartService
  55.      * @param PurchaseFlow $cartPurchaseFlow
  56.      * @param BaseInfoRepository $baseInfoRepository
  57.      */
  58.     public function __construct(
  59.         ProductClassRepository $productClassRepository,
  60.         CartService $cartService,
  61.         PurchaseFlow $cartPurchaseFlow,
  62.         BaseInfoRepository $baseInfoRepository
  63.     ) {
  64.         $this->productClassRepository $productClassRepository;
  65.         $this->cartService $cartService;
  66.         $this->purchaseFlow $cartPurchaseFlow;
  67.         $this->baseInfo $baseInfoRepository->get();
  68.     }
  69.     /**
  70.      * カート画面.
  71.      *
  72.      * @Route("/cart", name="cart")
  73.      * @Template("Cart/index.twig")
  74.      */
  75.     public function index(Request $request)
  76.     {
  77.         // セッションからcidを取得
  78.         $cid $this->session->get('cid');
  79.         // 非会員情報がセットされているかでボタンを分岐させる
  80.         $isNonmemberDataExists false;
  81.         $NonMember $this->session->get(self::SESSION_NON_MEMBER);
  82.         if($NonMember){
  83.             $isNonmemberDataExists true;
  84.         }
  85.         // カートを取得して明細の正規化を実行
  86.         $Carts $this->cartService->getCarts();
  87.         $this->execPurchaseFlow($Carts);
  88.         // TODO itemHolderから取得できるように
  89.         $least = [];
  90.         $quantity = [];
  91.         $isDeliveryFree = [];
  92.         $totalPrice 0;
  93.         $totalQuantity 0;
  94.         foreach ($Carts as $Cart) {
  95.             $quantity[$Cart->getCartKey()] = 0;
  96.             $isDeliveryFree[$Cart->getCartKey()] = false;
  97.             if ($this->baseInfo->getDeliveryFreeQuantity()) {
  98.                 if ($this->baseInfo->getDeliveryFreeQuantity() > $Cart->getQuantity()) {
  99.                     $quantity[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeQuantity() - $Cart->getQuantity();
  100.                 } else {
  101.                     $isDeliveryFree[$Cart->getCartKey()] = true;
  102.                 }
  103.             }
  104.             if ($this->baseInfo->getDeliveryFreeAmount()) {
  105.                 if (!$isDeliveryFree[$Cart->getCartKey()] && $this->baseInfo->getDeliveryFreeAmount() <= $Cart->getTotalPrice()) {
  106.                     $isDeliveryFree[$Cart->getCartKey()] = true;
  107.                 } else {
  108.                     $least[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeAmount() - $Cart->getTotalPrice();
  109.                 }
  110.             }
  111.             $totalPrice += $Cart->getTotalPrice();
  112.             $totalQuantity += $Cart->getQuantity();
  113.         }
  114.         // カートが分割された時のセッション情報を削除
  115.         $request->getSession()->remove(OrderHelper::SESSION_CART_DIVIDE_FLAG);
  116.         return [
  117.             'totalPrice' => $totalPrice,
  118.             'totalQuantity' => $totalQuantity,
  119.             // 空のカートを削除し取得し直す
  120.             'Carts' => $this->cartService->getCarts(true),
  121.             'least' => $least,
  122.             'quantity' => $quantity,
  123.             'is_delivery_free' => $isDeliveryFree,
  124.             'cid' => $cid,
  125.             'nonmember_data_exists' => $isNonmemberDataExists,
  126.         ];
  127.     }
  128.     /**
  129.      * @param $Carts
  130.      *
  131.      * @return \Symfony\Component\HttpFoundation\RedirectResponse
  132.      */
  133.     protected function execPurchaseFlow($Carts)
  134.     {
  135.         /** @var PurchaseFlowResult[] $flowResults */
  136.         $flowResults array_map(function ($Cart) {
  137.             $purchaseContext = new PurchaseContext($Cart$this->getUser());
  138.             return $this->purchaseFlow->validate($Cart$purchaseContext);
  139.         }, $Carts);
  140.         // 復旧不可のエラーが発生した場合はカートをクリアして再描画
  141.         $hasError false;
  142.         foreach ($flowResults as $result) {
  143.             if ($result->hasError()) {
  144.                 $hasError true;
  145.                 foreach ($result->getErrors() as $error) {
  146.                     $this->addRequestError($error->getMessage());
  147.                 }
  148.             }
  149.         }
  150.         if ($hasError) {
  151.             $this->cartService->clear();
  152.             return $this->redirectToRoute('cart');
  153.         }
  154.         $this->cartService->save();
  155.         foreach ($flowResults as $index => $result) {
  156.             foreach ($result->getWarning() as $warning) {
  157.                 if ($Carts[$index]->getItems()->count() > 0) {
  158.                     $cart_key $Carts[$index]->getCartKey();
  159.                     $this->addRequestError($warning->getMessage(), "front.cart.${cart_key}");
  160.                 } else {
  161.                     // キーが存在しない場合はグローバルにエラーを表示する
  162.                     $this->addRequestError($warning->getMessage());
  163.                 }
  164.             }
  165.         }
  166.     }
  167.     /**
  168.      * カート明細の加算/減算/削除を行う.
  169.      *
  170.      * - 加算
  171.      *      - 明細の個数を1増やす
  172.      * - 減算
  173.      *      - 明細の個数を1減らす
  174.      *      - 個数が0になる場合は、明細を削除する
  175.      * - 削除
  176.      *      - 明細を削除する
  177.      *
  178.      * @Route(
  179.      *     path="/cart/{operation}/{productClassId}",
  180.      *     name="cart_handle_item",
  181.      *     methods={"PUT"},
  182.      *     requirements={
  183.      *          "operation": "up|down|remove",
  184.      *          "productClassId": "\d+"
  185.      *     }
  186.      * )
  187.      */
  188.     public function handleCartItem($operation$productClassId)
  189.     {
  190.         log_info('カート明細操作開始', ['operation' => $operation'product_class_id' => $productClassId]);
  191.         $this->isTokenValid();
  192.         /** @var ProductClass $ProductClass */
  193.         $ProductClass $this->productClassRepository->find($productClassId);
  194.         if (is_null($ProductClass)) {
  195.             log_info('商品が存在しないため、カート画面へredirect', ['operation' => $operation'product_class_id' => $productClassId]);
  196.             return $this->redirectToRoute('cart');
  197.         }
  198.         // 明細の増減・削除
  199.         switch ($operation) {
  200.             case 'up':
  201.                 $this->cartService->addProduct($ProductClass1);
  202.                 break;
  203.             case 'down':
  204.                 $this->cartService->addProduct($ProductClass, -1);
  205.                 break;
  206.             case 'remove':
  207.                 $this->cartService->removeProduct($ProductClass);
  208.                 break;
  209.         }
  210.         // カートを取得して明細の正規化を実行
  211.         $Carts $this->cartService->getCarts();
  212.         $this->execPurchaseFlow($Carts);
  213.         log_info('カート演算処理終了', ['operation' => $operation'product_class_id' => $productClassId]);
  214.         return $this->redirectToRoute('cart');
  215.     }
  216.     /**
  217.      * カートをロック状態に設定し、購入確認画面へ遷移する.
  218.      *
  219.      * @Route("/cart/buystep/{cart_key}", name="cart_buystep", requirements={"cart_key" = "[a-zA-Z0-9]+[_][\x20-\x7E]+"})
  220.      */
  221.     public function buystep(Request $request$cart_key)
  222.     {
  223.         $Carts $this->cartService->getCart();
  224.         if (!is_object($Carts)) {
  225.             return $this->redirectToRoute('cart');
  226.         }
  227.         // FRONT_CART_BUYSTEP_INITIALIZE
  228.         $event = new EventArgs(
  229.             [],
  230.             $request
  231.         );
  232.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_CART_BUYSTEP_INITIALIZE$event);
  233.         $this->cartService->setPrimary($cart_key);
  234.         $this->cartService->save();
  235.         // FRONT_CART_BUYSTEP_COMPLETE
  236.         $event = new EventArgs(
  237.             [],
  238.             $request
  239.         );
  240.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_CART_BUYSTEP_COMPLETE$event);
  241.         if ($event->hasResponse()) {
  242.             return $event->getResponse();
  243.         }
  244.         return $this->redirectToRoute('shopping');
  245.     }
  246. }