app/Customize/Service/CartService.php line 205

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\Service;
  13. use Customize\Repository\Master\MtbPrintRepository;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Doctrine\ORM\UnitOfWork;
  16. use Eccube\Entity\Cart;
  17. use Eccube\Entity\CartItem;
  18. use Eccube\Entity\Customer;
  19. use Eccube\Entity\ItemHolderInterface;
  20. use Eccube\Entity\OrderItem;
  21. use Eccube\Entity\ProductClass;
  22. use Eccube\Repository\BaseInfoRepository;
  23. use Eccube\Repository\CartItemRepository;
  24. use Eccube\Repository\CartRepository;
  25. use Eccube\Repository\ClassCategoryRepository;
  26. use Eccube\Repository\ClassNameRepository;
  27. use Eccube\Repository\OrderRepository;
  28. use Eccube\Repository\ProductClassRepository;
  29. use Eccube\Service\Cart\CartItemAllocator;
  30. use Eccube\Service\Cart\CartItemComparator;
  31. use Customize\Service\TaxRuleServiceCustom as TaxRuleService;
  32. use Eccube\Util\StringUtil;
  33. use Symfony\Component\DependencyInjection\ContainerInterface;
  34. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  35. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  36. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  37. use Eccube\Repository\ProductRepository;
  38. class CartService
  39. {
  40.     /**
  41.      * @var Cart[]
  42.      */
  43.     protected $carts;
  44.     /**
  45.      * @var SessionInterface
  46.      */
  47.     protected $session;
  48.     /**
  49.      * @var \Doctrine\ORM\EntityManagerInterface
  50.      */
  51.     protected $entityManager;
  52.     /**
  53.      * @var ItemHolderInterface
  54.      *
  55.      * @deprecated
  56.      */
  57.     protected $cart;
  58.     /**
  59.      * @var ProductClassRepository
  60.      */
  61.     protected $productClassRepository;
  62.     /**
  63.      * @var CartRepository
  64.      */
  65.     protected $cartRepository;
  66.     /**
  67.      * @var CartItemComparator
  68.      */
  69.     protected $cartItemComparator;
  70.     /**
  71.      * @var CartItemAllocator
  72.      */
  73.     protected $cartItemAllocator;
  74.     /**
  75.      * @var OrderRepository
  76.      */
  77.     protected $orderRepository;
  78.     /**
  79.      * @var TokenStorageInterface
  80.      */
  81.     protected $tokenStorage;
  82.     /**
  83.      * @var AuthorizationCheckerInterface
  84.      */
  85.     protected $authorizationChecker;
  86.     protected $cartItemRepository;
  87.     protected $mtbPrintRepository;
  88.     protected $container;
  89.     protected $userRate;
  90.     protected $taxRuleService;
  91.     protected $productRepository;
  92.     /**
  93.      * CartService constructor.
  94.      */
  95.     public function __construct(
  96.         SessionInterface $session,
  97.         EntityManagerInterface $entityManager,
  98.         ProductClassRepository $productClassRepository,
  99.         CartRepository $cartRepository,
  100.         CartItemComparator $cartItemComparator,
  101.         CartItemAllocator $cartItemAllocator,
  102.         OrderRepository $orderRepository,
  103.         TokenStorageInterface $tokenStorage,
  104.         AuthorizationCheckerInterface $authorizationChecker,
  105.         CartItemRepository $cartItemRepository,
  106.         MtbPrintRepository $mtbPrintRepository,
  107.         ContainerInterface $container,
  108.         BaseInfoRepository $baseInfoRepository,
  109.         TaxRuleService $taxRuleService,
  110.         ProductRepository $productRepository
  111.     ) {
  112.         $this->session $session;
  113.         $this->entityManager $entityManager;
  114.         $this->productClassRepository $productClassRepository;
  115.         $this->cartRepository $cartRepository;
  116.         $this->cartItemComparator $cartItemComparator;
  117.         $this->cartItemAllocator $cartItemAllocator;
  118.         $this->orderRepository $orderRepository;
  119.         $this->tokenStorage $tokenStorage;
  120.         $this->authorizationChecker $authorizationChecker;
  121.         $this->cartItemRepository $cartItemRepository;
  122.         $this->mtbPrintRepository $mtbPrintRepository;
  123.         $this->container $container;
  124.         $this->userRate $baseInfoRepository->get()->getOptionUserRate();
  125.         $this->taxRuleService $taxRuleService;
  126.         $this->productRepository $productRepository;
  127.     }
  128.     /**
  129.      * 現在のカートの配列を取得する.
  130.      *
  131.      * 本サービスのインスタンスのメンバーが空の場合は、DBまたはセッションからカートを取得する
  132.      *
  133.      * @param bool $empty_delete true の場合、商品明細が空のカートが存在した場合は削除する
  134.      *
  135.      * @return Cart[]
  136.      */
  137.     public function getCarts($empty_delete false)
  138.     {
  139.         if (null !== $this->carts) {
  140.             if ($empty_delete) {
  141.                 $cartKeys = [];
  142.                 foreach (array_keys($this->carts) as $index) {
  143.                     $Cart $this->carts[$index];
  144.                     if ($Cart->getItems()->count() > 0) {
  145.                         $cartKeys[] = $Cart->getCartKey();
  146.                     } else {
  147.                         $this->entityManager->remove($this->carts[$index]);
  148.                         $this->entityManager->flush();
  149.                         unset($this->carts[$index]);
  150.                     }
  151.                 }
  152.                 $this->session->set('cart_keys'$cartKeys);
  153.             }
  154.             return $this->carts;
  155.         }
  156.         if ($this->getUser()) {
  157.             $this->carts $this->getPersistedCarts();
  158.         } else {
  159.             $this->carts $this->getSessionCarts();
  160.         }
  161.         return $this->carts;
  162.     }
  163.     /**
  164.      * 永続化されたカートを返す
  165.      *
  166.      * @return Cart[]
  167.      */
  168.     public function getPersistedCarts()
  169.     {
  170.         return $this->cartRepository->findBy(['Customer' => $this->getUser()]);
  171.     }
  172.     /**
  173.      * セッションにあるカートを返す
  174.      *
  175.      * @return Cart[]
  176.      */
  177.     public function getSessionCarts()
  178.     {
  179.         $cartKeys $this->session->get('cart_keys', []);
  180.         if (empty($cartKeys)) {
  181.             return [];
  182.         }
  183.         return $this->cartRepository->findBy(['cart_key' => $cartKeys], ['id' => 'ASC']);
  184.     }
  185.     /**
  186.      * 会員が保持する永続化されたカートと、非会員時のカートをマージする.
  187.      */
  188.     public function mergeFromPersistedCart()
  189.     {
  190.         $persistedCarts $this->getPersistedCarts();
  191.         $sessionCarts $this->getSessionCarts();
  192.         $CartItems = [];
  193.         // 永続化されたカートとセッションのカートが同一の場合はマージしない #4574
  194.         $cartKeys $this->session->get('cart_keys', []);
  195.         if ((count($persistedCarts) > 0) && !in_array($persistedCarts[0]->getCartKey(), $cartKeystrue)) {
  196.             foreach ($persistedCarts as $Cart) {
  197.                 $CartItems $this->mergeCartItems($Cart->getCartItems(), $CartItems);
  198.             }
  199.         }
  200.         // セッションにある非会員カートとDBから取得した会員カートをマージする.
  201.         foreach ($sessionCarts as $Cart) {
  202.             $CartItems $this->mergeCartItems($Cart->getCartItems(), $CartItems);
  203.         }
  204.         $this->restoreCarts($CartItems);
  205.     }
  206.     /**
  207.      * @return Cart|null
  208.      */
  209.     public function getCart()
  210.     {
  211.         $Carts $this->getCarts();
  212.         if (empty($Carts)) {
  213.             return null;
  214.         }
  215.         $cartKeys $this->session->get('cart_keys', []);
  216.         $Cart null;
  217.         if (count($cartKeys) > 0) {
  218.             foreach ($Carts as $cart) {
  219.                 if ($cart->getCartKey() === current($cartKeys)) {
  220.                     $Cart $cart;
  221.                     break;
  222.                 }
  223.             }
  224.         } else {
  225.             $Cart $Carts[0];
  226.         }
  227.         return $Cart;
  228.     }
  229.     /**
  230.      * @param CartItem[] $cartItems
  231.      *
  232.      * @return CartItem[]
  233.      */
  234.     protected function mergeAllCartItems($cartItems = [])
  235.     {
  236.         /** @var CartItem[] $allCartItems */
  237.         $allCartItems = [];
  238.         foreach ($this->getCarts() as $Cart) {
  239.             $allCartItems $this->mergeCartItems($Cart->getCartItems(), $allCartItems);
  240.         }
  241.         return $this->mergeCartItems($cartItems$allCartItems);
  242.     }
  243.     /**
  244.      * @param $cartItems
  245.      * @param $allCartItems
  246.      *
  247.      * @return array
  248.      */
  249.     protected function mergeCartItems($cartItems$allCartItems)
  250.     {
  251.         foreach ($cartItems as $item) {
  252.             $itemExists false;
  253.             $idProduct $item->getProductClass()->getProduct()->getId();
  254.             $product $this->productRepository->find($idProduct);
  255.             foreach ($allCartItems as $itemInArray) {
  256.                 // 同じ明細があればマージする
  257.                 if ($this->cartItemComparator->compare($item$itemInArray)) {
  258.                     $itemInArray->setQuantity($itemInArray->getQuantity() + $item->getQuantity());
  259.                     $itemInArray->setProductPrintType($item->getProductPrintType());
  260.                     $itemInArray->setProductPrintFeeType($item->getProductPrintFeeType());
  261.                     $itemInArray->setProductPrintFeePrice($item->getProductPrintFeePrice());
  262.                     $itemInArray->setProductPlateTypePrint($item->getProductPlateTypePrint());
  263.                     $itemInArray->setProductPrintColor($item->getProductPrintColor());
  264.                     $itemInArray->setIsPrint($item->getIsPrint());
  265.                     $itemInArray->setIsRepurchase($item->getIsRepurchase());
  266.                     $itemInArray->setProductCodeByColor($item->getProductCodeByColor());
  267.                     $itemExists true;
  268.                     break;
  269.                 }
  270.             }
  271.             if (!$itemExists) {
  272.                 $allCartItems[] = $item;
  273.             }
  274.         }
  275.         return $allCartItems;
  276.     }
  277.     protected function restoreCarts($cartItems)
  278.     {
  279.         foreach ($this->getCarts() as $Cart) {
  280.             foreach ($Cart->getCartItems() as $i) {
  281.                 $this->entityManager->remove($i);
  282.                 $this->entityManager->flush();
  283.             }
  284.             $this->entityManager->remove($Cart);
  285.             $this->entityManager->flush();
  286.         }
  287.         $this->carts = [];
  288.         /** @var Cart[] $Carts */
  289.         $Carts = [];
  290.         foreach ($cartItems as $item) {
  291.             $allocatedId $this->cartItemAllocator->allocate($item);
  292.             $cartKey $this->createCartKey($allocatedId$this->getUser());
  293.             if (isset($Carts[$cartKey])) {
  294.                 $Cart $Carts[$cartKey];
  295.                 $Cart->addCartItem($item);
  296.                 $item->setCart($Cart);
  297.             } else {
  298.                 /** @var Cart $Cart */
  299.                 $Cart $this->cartRepository->findOneBy(['cart_key' => $cartKey]);
  300.                 if ($Cart) {
  301.                     foreach ($Cart->getCartItems() as $i) {
  302.                         $this->entityManager->remove($i);
  303.                         $this->entityManager->flush();
  304.                     }
  305.                     $this->entityManager->remove($Cart);
  306.                     $this->entityManager->flush();
  307.                 }
  308.                 $Cart = new Cart();
  309.                 $Cart->setCartKey($cartKey);
  310.                 $Cart->addCartItem($item);
  311.                 $item->setCart($Cart);
  312.                 $Carts[$cartKey] = $Cart;
  313.             }
  314.         }
  315.         $this->carts array_values($Carts);
  316.     }
  317.     protected function isGranted($attribute$subject null): bool
  318.     {
  319.         if (!$this->container->has('security.authorization_checker')) {
  320.             throw new \LogicException(
  321.                 'The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".'
  322.             );
  323.         }
  324.         return $this->container->get('security.authorization_checker')->isGranted($attribute$subject);
  325.     }
  326.     public function getTaxRuleService()
  327.     {
  328.         return $this->taxRuleService;
  329.     }
  330.     /**
  331.      * カートに商品を追加します.
  332.      *
  333.      * @param $ProductClass ProductClass 商品規格
  334.      * @param $quantity int 数量
  335.      *
  336.      * @return bool 商品を追加できた場合はtrue
  337.      */
  338.     public function addProduct($ProductClass$quantity 1$print = [])
  339.     {
  340.         if (!$ProductClass instanceof ProductClass) {
  341.             $ProductClassId $ProductClass;
  342.             $ProductClass $this->entityManager
  343.                 ->getRepository(ProductClass::class)
  344.                 ->find($ProductClassId);
  345.             if (is_null($ProductClass)) {
  346.                 return false;
  347.             }
  348.         }
  349.         $ClassCategory1 $ProductClass->getClassCategory1();
  350.         if ($ClassCategory1 && !$ClassCategory1->isVisible()) {
  351.             return false;
  352.         }
  353.         $ClassCategory2 $ProductClass->getClassCategory2();
  354.         if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
  355.             return false;
  356.         }
  357.         $newItem = new CartItem();
  358.         $newItem->setQuantity($quantity);
  359.         $price 0;
  360. //        if($this->isGranted('ROLE_USER') == true) {
  361. //            $rate = $this->userRate/100;
  362. //            $price = floor($ProductClass->getPrice02() - round($ProductClass->getPrice02() * $rate));
  363. //            $priceTax = $this->taxRuleService->getTax($price, $ProductClass->getProduct(), $ProductClass);
  364. //            $price = round($price + $priceTax);
  365. //        } else {
  366. //            $price = $ProductClass->getPrice02IncTax();
  367. //        }
  368.         $price $ProductClass->getPrice02();
  369.         $newItem->setPrice($price);
  370.         $newItem->setProductClass($ProductClass);
  371.         if (count($print) > 0) {
  372.             if (isset($print['update_quantity']) && $print['update_quantity']) {
  373.                 if(isset($print['cartItemId']) && $print['cartItemId'] > 0) {
  374.                     $dataCartItem $this->cartItemRepository->findOneBy(
  375.                         ['Cart' => $this->getCart(), 'ProductClass' => $ProductClass'id' => $print['cartItemId']]
  376.                     );
  377.                 } else {
  378.                     $dataCartItem $this->cartItemRepository->findOneBy(
  379.                         ['Cart' => $this->getCart(), 'ProductClass' => $ProductClass]
  380.                     );
  381.                 }
  382.                 if ($dataCartItem) {
  383.                     $print = [
  384.                         'product_print_type' => $dataCartItem['product_print_type'],
  385.                         'product_print_fee_type' => $dataCartItem['product_print_fee_type'],
  386.                         'product_print_fee_price' => $dataCartItem['product_print_fee_price'],
  387.                         'product_plate_type_print' => $dataCartItem['product_plate_type_print'],
  388.                         'product_print_color' => $dataCartItem['product_print_color'],
  389.                         'is_print' => $dataCartItem['is_print'],
  390.                         'is_repurchase' => $dataCartItem['is_repurchase'],
  391.                         'quantity' => $dataCartItem['quantity'],
  392.                     ];
  393.                 }
  394.             } else {
  395.                 $dataCartItem $this->cartItemRepository->findOneBy(
  396.                     ['Cart' => $this->getCart(), 'ProductClass' => $ProductClass'product_print_color' => $print['product_print_color']]
  397.                 );
  398.                 if($dataCartItem) {
  399.                     $quantity $quantity + (int)$dataCartItem->getQuantity();
  400.                 }
  401.             }
  402.             $product_print_fee_price $print['product_print_fee_price'];
  403.             if ($this->getCart()) {
  404.                 $newQuantity = (isset($print['quantity']) && $print['quantity']) ? $print['quantity'] + $quantity $quantity;
  405.                 if ($print['product_print_fee_type'] == 1) {
  406.                     $dataMtbPrint $this->mtbPrintRepository->findBy(
  407.                         [
  408.                             'product_print_fee_type' => $print['product_print_fee_type'],
  409.                             'discriminator_type' => 'fee_print_name'
  410.                         ]
  411.                     );
  412.                     foreach ($dataMtbPrint as $value) {
  413.                         if ($value['product_print_fee_type_max'] > 0) {
  414.                             if ($newQuantity >= $value['product_print_fee_type_min'] && $newQuantity <= $value['product_print_fee_type_max']) {
  415.                                 $product_print_fee_price $value['price'];
  416.                                 break;
  417.                             }
  418.                         } else {
  419.                             $product_print_fee_price $value['price'];
  420.                         }
  421.                     }
  422.                 } elseif ($print['product_print_fee_type'] == 3) {
  423.                     $dataMtbPrint $this->mtbPrintRepository->findBy(
  424.                         [
  425.                             'product_print_fee_type' => $print['product_print_fee_type'],
  426.                             'discriminator_type' => 'fee_print_name'
  427.                         ]
  428.                     );
  429.                     foreach ($dataMtbPrint as $value) {
  430.                         if ($value['product_print_fee_type_max'] > 0) {
  431.                             $product_print_fee_price 50;
  432.                         } else {
  433.                             if ($newQuantity >= $value['product_print_fee_type_min']) {
  434.                                 $product_print_fee_price $value['price'];
  435.                                 break;
  436.                             }
  437.                         }
  438.                     }
  439.                 }
  440.             }
  441.             $newItem->setProductPrintType($print['product_print_type']);
  442.             $newItem->setProductPrintFeeType($print['product_print_fee_type']);
  443.             $newItem->setProductPrintFeePrice($product_print_fee_price);
  444.             $newItem->setProductPlateTypePrint($print['product_plate_type_print']);
  445.             $newItem->setProductPrintColor($print['product_print_color']);
  446.             $newItem->setIsPrint($print['is_print']);
  447.             $newItem->setIsRepurchase($print['is_repurchase']);
  448.             $newItem->setProductCodeByColor(
  449.                 (isset($print['product_code_by_color'])) ? $print['product_code_by_color'] : ''
  450.             );
  451.         }
  452.         $allCartItems $this->mergeAllCartItems([$newItem]);
  453.         $this->restoreCarts($allCartItems);
  454.         return true;
  455.     }
  456.     public function removeProduct($ProductClass$cartItemId 0)
  457.     {
  458.         if (!$ProductClass instanceof ProductClass) {
  459.             $ProductClassId $ProductClass;
  460.             $ProductClass $this->entityManager
  461.                 ->getRepository(ProductClass::class)
  462.                 ->find($ProductClassId);
  463.             if (is_null($ProductClass)) {
  464.                 return false;
  465.             }
  466.         }
  467.         if($cartItemId == 0) {
  468.             $removeItem = new CartItem();
  469.             $removeItem->setPrice($ProductClass->getPrice02IncTax());
  470.             $removeItem->setProductClass($ProductClass);
  471.         }
  472.         $allCartItems $this->mergeAllCartItems();
  473.         $foundIndex = -1;
  474.         foreach ($allCartItems as $index => $itemInCart) {
  475.             if($cartItemId == 0) {
  476.                 $cartProductClassId $itemInCart->getProductClass()->getId();
  477.                 $removeProductClassId $removeItem->getProductClass()->getId();
  478.                 //          if ($this->cartItemComparator->compare($itemInCart, $removeItem)) { caohv remove because not working
  479.                 if ($cartProductClassId == $removeProductClassId) {
  480.                     $foundIndex $index;
  481.                     break;
  482.                 }
  483.             } else {
  484.                 if ($itemInCart->getId() == $cartItemId) {
  485.                     $foundIndex $index;
  486.                     break;
  487.                 }
  488.             }
  489.         }
  490.         array_splice($allCartItems$foundIndex1);
  491.         $this->restoreCarts($allCartItems);
  492.         return true;
  493.     }
  494.     public function save()
  495.     {
  496.         $cartKeys = [];
  497.         $data_fee_print_all_price $this->mtbPrintRepository->findOneBy(
  498.             ['product_print_fee_type' => 4'discriminator_type' => 'fee_print_all']
  499.         );
  500.         if($this->carts) {
  501.             foreach ($this->carts as &$Cart) {
  502.                 $totalPrice 0;
  503.                 $fee_print_all_price 0;
  504.                 $platePrint = [];
  505.                 $product_print_fee_price 0;
  506.                 foreach ($Cart->getCartItems() as $cartItem) {
  507.                     $pricePrint 0;
  508.                     if ($cartItem->getIsPrint() == 1) {
  509.                         if ($cartItem->getIsRepurchase() == 0) {
  510.                             $platePrint[$cartItem->getProductClass()->getId() . $cartItem->getProductPrintColor()] = 1;
  511.                         }
  512.                         $newQuantity $cartItem->getQuantity();
  513.                         if ($cartItem->getProductPrintFeeType() == 1) {
  514.                             $dataMtbPrint $this->mtbPrintRepository->findBy(
  515.                                 [
  516.                                     'product_print_fee_type' => $cartItem->getProductPrintFeeType(),
  517.                                     'discriminator_type' => 'fee_print_name'
  518.                                 ]
  519.                             );
  520.                             foreach ($dataMtbPrint as $value) {
  521.                                 if ($value['product_print_fee_type_max'] > 0) {
  522.                                     if ($newQuantity >= $value['product_print_fee_type_min'] && $newQuantity <= $value['product_print_fee_type_max']) {
  523.                                         $product_print_fee_price $value['price'];
  524.                                         break;
  525.                                     }
  526.                                 } else {
  527.                                     $product_print_fee_price $value['price'];
  528.                                 }
  529.                             }
  530.                         } elseif ($cartItem->getProductPrintFeeType() == 3) {
  531.                             $dataMtbPrint $this->mtbPrintRepository->findBy(
  532.                                 [
  533.                                     'product_print_fee_type' => $cartItem->getProductPrintFeeType(),
  534.                                     'discriminator_type' => 'fee_print_name'
  535.                                 ]
  536.                             );
  537.                             foreach ($dataMtbPrint as $value) {
  538.                                 if ($value['product_print_fee_type_max'] > 0) {
  539.                                     $product_print_fee_price 50;
  540.                                 } else {
  541.                                     if ($newQuantity >= $value['product_print_fee_type_min']) {
  542.                                         $product_print_fee_price $value['price'];
  543.                                         break;
  544.                                     }
  545.                                 }
  546.                             }
  547.                         } else {
  548.                             if($cartItem->getProductClass()) {
  549.                                 $product_print_fee_price $cartItem->getProductClass()->getProduct()->getProductPrintFeePrice();
  550.                             }
  551.                         }
  552.                         $cartItem->setProductPrintFeePrice($product_print_fee_price);
  553.                         $pricePrint $product_print_fee_price $cartItem->getQuantity();
  554.                     }
  555.                     $totalPrice $totalPrice + ($cartItem->getTotalPrice() + $pricePrint);
  556.                 }
  557.                 if (count($platePrint) > 0) {
  558.                     $fee_print_all_price env('FEE_PRINT_PRICE'8000) * count($platePrint);
  559.                     if ($data_fee_print_all_price) {
  560.                         $fee_print_all_price = (int)$data_fee_print_all_price->getPrice() * count($platePrint);
  561.                     }
  562.                 }
  563.                 $Cart->setFeePrintAllQuantity(count($platePrint));
  564.                 $Cart->setFeePrintAllPrice($fee_print_all_price);
  565.                 $Cart->setTotalPrice($totalPrice $fee_print_all_price);
  566.                 $Cart->setDeliveryFeeTotal(0);
  567.                 $Cart->setCustomer($this->getUser());
  568.                 $this->entityManager->persist($Cart);
  569.                 foreach ($Cart->getCartItems() as $item) {
  570.                     $this->entityManager->persist($item);
  571.                 }
  572.                 $this->entityManager->flush();
  573.                 $cartKeys[] = $Cart->getCartKey();
  574.             }
  575.             $this->session->set('cart_keys'$cartKeys);
  576.         }
  577.         return;
  578.     }
  579.     /**
  580.      * @param string $pre_order_id
  581.      *
  582.      * @return \Eccube\Service\CartService
  583.      */
  584.     public function setPreOrderId($pre_order_id)
  585.     {
  586.         $this->getCart()->setPreOrderId($pre_order_id);
  587.         return $this;
  588.     }
  589.     /**
  590.      * @return string|null
  591.      */
  592.     public function getPreOrderId()
  593.     {
  594.         $Cart $this->getCart();
  595.         if (!empty($Cart)) {
  596.             return $Cart->getPreOrderId();
  597.         }
  598.         return null;
  599.     }
  600.     /**
  601.      * @return \Eccube\Service\CartService
  602.      */
  603.     public function clear()
  604.     {
  605.         $Carts $this->getCarts();
  606.         if (!empty($Carts)) {
  607.             $removed $this->getCart();
  608.             if ($removed && UnitOfWork::STATE_MANAGED === $this->entityManager->getUnitOfWork()->getEntityState(
  609.                     $removed
  610.                 )) {
  611.                 $this->entityManager->remove($removed);
  612.                 $this->entityManager->flush();
  613.                 $cartKeys = [];
  614.                 foreach ($Carts as $key => $Cart) {
  615.                     // テーブルから削除されたカートを除外する
  616.                     if ($Cart == $removed) {
  617.                         unset($Carts[$key]);
  618.                     }
  619.                     $cartKeys[] = $Cart->getCartKey();
  620.                 }
  621.                 $this->session->set('cart_keys'$cartKeys);
  622.                 // 注文完了のカートキーをセッションから削除する
  623.                 $this->session->remove('cart_key');
  624.                 $this->carts $this->cartRepository->findBy(['cart_key' => $cartKeys], ['id' => 'ASC']);
  625.             }
  626.         }
  627.         return $this;
  628.     }
  629.     /**
  630.      * @param CartItemComparator $cartItemComparator
  631.      */
  632.     public function setCartItemComparator($cartItemComparator)
  633.     {
  634.         $this->cartItemComparator $cartItemComparator;
  635.     }
  636.     /**
  637.      * カートキーで指定したインデックスにあるカートを優先にする
  638.      *
  639.      * @param string $cartKey カートキー
  640.      */
  641.     public function setPrimary($cartKey)
  642.     {
  643.         $Carts $this->getCarts();
  644.         $primary $Carts[0];
  645.         $index 0;
  646.         foreach ($Carts as $key => $Cart) {
  647.             if ($Cart->getCartKey() === $cartKey) {
  648.                 $index $key;
  649.                 $primary $Carts[$index];
  650.                 break;
  651.             }
  652.         }
  653.         $prev $Carts[0];
  654.         array_splice($Carts01, [$primary]);
  655.         array_splice($Carts$index1, [$prev]);
  656.         $this->carts $Carts;
  657.         $this->save();
  658.     }
  659.     protected function getUser()
  660.     {
  661.         if (null === $token $this->tokenStorage->getToken()) {
  662.             return;
  663.         }
  664.         if (!is_object($user $token->getUser())) {
  665.             // e.g. anonymous authentication
  666.             return;
  667.         }
  668.         return $user;
  669.     }
  670.     /**
  671.      * @param string $allocatedId
  672.      */
  673.     protected function createCartKey($allocatedIdCustomer $Customer null)
  674.     {
  675.         if ($Customer instanceof Customer) {
  676.             return $Customer->getId() . '_' $allocatedId;
  677.         }
  678.         if ($this->session->has('cart_key_prefix')) {
  679.             return $this->session->get('cart_key_prefix') . '_' $allocatedId;
  680.         }
  681.         do {
  682.             $random StringUtil::random(32);
  683.             $cartKey $random '_' $allocatedId;
  684.             $Cart $this->cartRepository->findOneBy(['cart_key' => $cartKey]);
  685.         } while ($Cart);
  686.         $this->session->set('cart_key_prefix'$random);
  687.         return $cartKey;
  688.     }
  689. }