app/Customize/Controller/CartController.php line 131

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\CartController as BaseCartController;
  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\OrderHelper;
  22. use Eccube\Service\PurchaseFlow\PurchaseContext;
  23. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  24. use Eccube\Service\PurchaseFlow\PurchaseFlowResult;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Eccube\Entity\CartItem;
  29. use Customize\Repository\Master\CsuProcessingRepository;
  30. use Customize\Repository\Master\CsuEmbRepository;
  31. use Customize\Repository\Master\CsuEmbPositionRepository;
  32. use Customize\Repository\Master\CsuEmbColorRepository;
  33. use Customize\Repository\Master\CsuEmbFontRepository;
  34. use Customize\Repository\Master\CsuEmbSizeRepository;
  35. use Customize\Repository\Master\CsuEmbDesignRepository;
  36. use Customize\Repository\Master\CsuPrnRepository;
  37. use Customize\Repository\Master\CsuPrnPositionRepository;
  38. use Customize\Repository\Master\CsuPrnColorNumRepository;
  39. use Customize\Repository\Master\CsuPrnDesignRepository;
  40. class CartController extends BaseCartController
  41. {
  42.     /**
  43.      * @var ProductClassRepository
  44.      */
  45.     protected $productClassRepository;
  46.     /**
  47.      * @var CartService
  48.      */
  49.     protected $cartService;
  50.     /**
  51.      * @var PurchaseFlow
  52.      */
  53.     protected $purchaseFlow;
  54.     /**
  55.      * @var BaseInfo
  56.      */
  57.     protected $baseInfo;
  58.     /**
  59.      * CartController constructor.
  60.      *
  61.      * @param ProductClassRepository $productClassRepository
  62.      * @param CartService $cartService
  63.      * @param PurchaseFlow $cartPurchaseFlow
  64.      * @param BaseInfoRepository $baseInfoRepository
  65.      */
  66.     public function __construct(
  67.         ProductClassRepository $productClassRepository,
  68.         CartService $cartService,
  69.         PurchaseFlow $cartPurchaseFlow,
  70.         BaseInfoRepository $baseInfoRepository
  71.     ) {
  72.         $this->productClassRepository $productClassRepository;
  73.         $this->cartService $cartService;
  74.         $this->purchaseFlow $cartPurchaseFlow;
  75.         $this->baseInfo $baseInfoRepository->get();
  76.     }
  77.     /**
  78.      * カート画面.
  79.      *
  80.      * @Route("/cart", name="cart", methods={"GET"})
  81.      * @Template("Cart/index.twig")
  82.      */
  83.     public function index(Request $request)
  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.             // 'cart_ex' => (isset($_SESSION['cart_ex'])) ? $_SESSION['cart_ex'] : array('product_subtotal'=>'', 'product_custom'=>''),
  125.         ];
  126.     }
  127.     /**
  128.      * @param $Carts
  129.      *
  130.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
  131.      */
  132.     protected function execPurchaseFlow($Carts)
  133.     {
  134.         /** @var PurchaseFlowResult[] $flowResults */
  135.         $flowResults array_map(function ($Cart) {
  136.             $purchaseContext = new PurchaseContext($Cart$this->getUser());
  137.             return $this->purchaseFlow->validate($Cart$purchaseContext);
  138.         }, $Carts);
  139.         // 復旧不可のエラーが発生した場合はカートをクリアして再描画
  140.         $hasError false;
  141.         foreach ($flowResults as $result) {
  142.             if ($result->hasError()) {
  143.                 $hasError true;
  144.                 foreach ($result->getErrors() as $error) {
  145.                     $this->addRequestError($error->getMessage());
  146.                 }
  147.             }
  148.         }
  149.         if ($hasError) {
  150.             $this->cartService->clear();
  151.             return $this->redirectToRoute('cart');
  152.         }
  153.         $this->cartService->save();
  154.         foreach ($flowResults as $index => $result) {
  155.             foreach ($result->getWarning() as $warning) {
  156.                 if ($Carts[$index]->getItems()->count() > 0) {
  157.                     $cart_key $Carts[$index]->getCartKey();
  158.                     $this->addRequestError($warning->getMessage(), "front.cart.${cart_key}");
  159.                 } else {
  160.                     // キーが存在しない場合はグローバルにエラーを表示する
  161.                     $this->addRequestError($warning->getMessage());
  162.                 }
  163.             }
  164.         }
  165.         return null;
  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. }