Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

83 lines
3.2KB

  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. $merchant = $this->merchantUtils->getMerchantCurrent() ;
  55. $orderShop = $this->createOrderShop([
  56. 'user' => $user,
  57. 'visitor' => $visitor,
  58. 'merchant' => $merchant,
  59. ]);
  60. }
  61. return $orderShop;
  62. }
  63. public function isCartAllowToBeOrder($order){
  64. return true;
  65. }
  66. }