82 lines
3.1KB

  1. <?php
  2. namespace Lc\ShopBundle\Services\Order;
  3. use Lc\ShopBundle\Context\OrderShopInterface;
  4. use Lc\ShopBundle\Model\OrderStatus;
  5. trait OrderUtilsCartTrait
  6. {
  7. public function getCartByUserOrCreateIt($user){
  8. $newOrderShop= $this->em->getRepository(OrderShopInterface::class)->findCartCurrent(['user' => $user]);
  9. if ($newOrderShop === null) {
  10. $newOrderShop = $this->createOrderShop(array(
  11. 'user' => $user,
  12. 'merchant' => $this->merchantUtils->getMerchantUser()
  13. ));
  14. }
  15. return $newOrderShop;
  16. }
  17. public function getCartCurrent($createIfNotExist = false)
  18. {
  19. $paramsSearchOrderShop = [];
  20. $user = $this->security->getUser();
  21. $visitor = $this->userUtils->getVisitorCurrent();
  22. $orderShop = null;
  23. $orderShopUser = null;
  24. $orderShopVisitor = null;
  25. if ($user) {
  26. $orderShopUser = $this->orderShopRepo->findCartCurrent([
  27. 'user' => $user
  28. ]);
  29. }
  30. if ($visitor) {
  31. $orderShopVisitor = $this->orderShopRepo->findCartCurrent([
  32. 'visitor' => $visitor
  33. ]);
  34. }
  35. if ($orderShopUser || $orderShopVisitor) {
  36. // merge
  37. if ($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor
  38. && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())
  39. && $orderShopUser->getOrderStatus()->getAlias() == OrderStatus::ALIAS_CART) {
  40. $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
  41. $this->utils->addFlash('success', "Votre panier visiteur vient d'être fusionné avec votre panier client.");
  42. } else {
  43. $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
  44. }
  45. // set user
  46. if ($orderShop && $user && !$orderShop->getUser()) {
  47. $orderShop->setUser($user);
  48. $orderShop->setVisitor(null) ;
  49. $this->em->persist($orderShop);
  50. $this->em->flush();
  51. }
  52. }
  53. if($createIfNotExist && !$orderShop) {
  54. $orderShop = $this->createOrderShop([
  55. 'user' => $user,
  56. 'visitor' => $visitor,
  57. 'merchant' => $this->merchantUtils->getMerchantCurrent()
  58. ]);
  59. }
  60. return $orderShop;
  61. }
  62. public function isCartAllowToBeOrder($order){
  63. return true;
  64. }
  65. }