app/Customize/Controller/CartControllerExtends.php line 91

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