app/Customize/Controller/Block/CartController.php line 121

Open in your IDE?
  1. <?php
  2. namespace Customize\Controller\Block;
  3. use Customize\Controller\ProductController;
  4. use Customize\Repository\CustomProductRepository;
  5. use Eccube\Controller\AbstractController;
  6. use Eccube\Entity\BaseInfo;
  7. use Eccube\Entity\ProductClass;
  8. use Eccube\Event\EccubeEvents;
  9. use Eccube\Event\EventArgs;
  10. use Eccube\Repository\BaseInfoRepository;
  11. use Eccube\Repository\ProductClassRepository;
  12. use Eccube\Service\CartService;
  13. use Eccube\Service\OrderHelper;
  14. use Eccube\Service\PurchaseFlow\PurchaseContext;
  15. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  16. use Eccube\Service\PurchaseFlow\PurchaseFlowResult;
  17. use mysql_xdevapi\Exception;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. class CartController extends AbstractController
  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 ProductController
  41.      */
  42.     protected $productController;
  43.     /**
  44.      * @var CustomProductRepository
  45.      */
  46.     protected $customProductRepository;
  47.     /**
  48.      * CartController constructor.
  49.      *
  50.      * @param ProductClassRepository $productClassRepository
  51.      * @param CartService $cartService
  52.      * @param PurchaseFlow $cartPurchaseFlow
  53.      * @param BaseInfoRepository $baseInfoRepository
  54.      */
  55.     public function __construct(
  56.         ProductClassRepository $productClassRepository,
  57.         CartService $cartService,
  58.         PurchaseFlow $cartPurchaseFlow,
  59.         BaseInfoRepository $baseInfoRepository,
  60.         ProductController $productController,
  61.         CustomProductRepository $customProductRepository
  62.     ) {
  63.         $this->productClassRepository $productClassRepository;
  64.         $this->cartService $cartService;
  65.         $this->purchaseFlow $cartPurchaseFlow;
  66.         $this->baseInfo $baseInfoRepository->get();
  67.         $this->productController $productController;
  68.         $this->customProductRepository $customProductRepository;
  69.     }
  70.     /**
  71.      * @Route("/block/header", name="block_header")
  72.      * @Template("Block/header.twig")
  73.      */
  74.     public function index(Request $request)
  75.     {
  76.         // カートを取得して明細の正規化を実行
  77.         $Carts $this->cartService->getCarts();
  78.         // TODO itemHolderから取得できるように
  79.         $least = [];
  80.         $quantity = [];
  81.         $isDeliveryFree = [];
  82.         $totalPrice 0;
  83.         $totalQuantity 0;
  84.         foreach ($Carts as $Cart) {
  85.             $quantity[$Cart->getCartKey()] = 0;
  86.             $isDeliveryFree[$Cart->getCartKey()] = false;
  87.             if ($this->baseInfo->getDeliveryFreeQuantity()) {
  88.                 if ($this->baseInfo->getDeliveryFreeQuantity() > $Cart->getQuantity()) {
  89.                     $quantity[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeQuantity() - $Cart->getQuantity();
  90.                 } else {
  91.                     $isDeliveryFree[$Cart->getCartKey()] = true;
  92.                 }
  93.             }
  94.             if ($this->baseInfo->getDeliveryFreeAmount()) {
  95.                 if (!$isDeliveryFree[$Cart->getCartKey()] && $this->baseInfo->getDeliveryFreeAmount() <= $Cart->getTotalPrice()) {
  96.                     $isDeliveryFree[$Cart->getCartKey()] = true;
  97.                 } else {
  98.                     $least[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeAmount() - $Cart->getTotalPrice();
  99.                 }
  100.             }
  101.             $totalPrice += $Cart->getTotalPrice();
  102.             $totalQuantity += $Cart->getQuantity();
  103.         }
  104.         // カートが分割された時のセッション情報を削除
  105.         $request->getSession()->remove(OrderHelper::SESSION_CART_DIVIDE_FLAG);
  106.         $cartItems $this->cartService->getCarts(true);
  107.         $priorityProducts = [];
  108.         $colorOrSize = [];
  109.         if (!empty($cartItems)) {
  110.             foreach ($cartItems as $cartItem) {
  111.                 $cartItem $cartItem->getItems();
  112.                 foreach ($cartItem as $item) {
  113.                     $product $item->getProductClass()->getProduct();
  114.                     $colorOrSize[$product->getId()] = $this->customProductRepository->getColorOrSize($product->getId());
  115.                     if (!empty($colorOrSize[$product->getId()]))
  116.                     {
  117.                         $colorOrSize[$product->getId()] = $colorOrSize[$product->getId()][0];
  118.                     }
  119.                     $priorityProduct $this->customProductRepository->getPriorityFlagProduct($item->getProductClass()->getProduct()->getId());
  120.                     if (is_object($priorityProduct)) {
  121.                         $priorityProducts[$product->getId()] = $priorityProduct;
  122.                     }else{
  123.                         $priorityProducts[$product->getId()] = $product;
  124.                     }
  125.                 }
  126.             }
  127.         }
  128.         return [
  129.             'totalPrice' => $totalPrice,
  130.             'totalQuantity' => $totalQuantity,
  131.             // 空のカートを削除し取得し直す
  132.             'Carts' => $this->cartService->getCarts(true),
  133.             'PriorityProducts' => $priorityProducts,
  134.             'ColorOrSize' => $colorOrSize,
  135.             'least' => $least,
  136.             'quantity' => $quantity,
  137.             'is_delivery_free' => $isDeliveryFree,
  138.         ];
  139.     }
  140. }