Browse Source

Gestion suivi panier User / Visitor

master
Guillaume 4 years ago
parent
commit
c01c36ed37
2 changed files with 54 additions and 11 deletions
  1. +1
    -1
      ShopBundle/Repository/OrderShopRepository.php
  2. +53
    -10
      ShopBundle/Services/OrderUtils.php

+ 1
- 1
ShopBundle/Repository/OrderShopRepository.php View File



public function findCartCurrent($params) public function findCartCurrent($params)
{ {
$query = $this->createQueryBuilder('e') ;
$query = $this->findByMerchantQuery() ;


if(isset($params['user'])) { if(isset($params['user'])) {
$query->andWhere('e.user = :user')->setParameter('user', $params['user']) ; $query->andWhere('e.user = :user')->setParameter('user', $params['user']) ;

+ 53
- 10
ShopBundle/Services/OrderUtils.php View File

use Lc\ShopBundle\Context\OrderShopInterface; use Lc\ShopBundle\Context\OrderShopInterface;
use Lc\ShopBundle\Context\ProductFamilyUtilsInterface; use Lc\ShopBundle\Context\ProductFamilyUtilsInterface;
use Lc\ShopBundle\Context\UserInterface; use Lc\ShopBundle\Context\UserInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;


class OrderUtils class OrderUtils
protected $orderShopRepo; protected $orderShopRepo;
protected $priceUtils ; protected $priceUtils ;
protected $productFamilyUtils ; protected $productFamilyUtils ;
protected $session ;


public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils, public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils,
MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils)
MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils,
SessionInterface $session)
{ {
$this->em = $em; $this->em = $em;
$this->security = $security; $this->security = $security;
$this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName()); $this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName());
$this->priceUtils = $priceUtils ; $this->priceUtils = $priceUtils ;
$this->productFamilyUtils = $productFamilyUtils ; $this->productFamilyUtils = $productFamilyUtils ;
$this->session = $session ;
} }


public function getCartCurrent() public function getCartCurrent()
$user = $this->security->getUser(); $user = $this->security->getUser();
$visitor = $this->userUtils->getVisitorCurrent(); $visitor = $this->userUtils->getVisitorCurrent();


$orderShop = null ;
$orderShopUser = null ;
$orderShopVisitor = null ;

if ($user) { if ($user) {
$paramsSearchOrderShop['user'] = $user;
$orderShopUser = $this->orderShopRepo->findCartCurrent([
'user' => $user
]);
} }


if ($visitor) { if ($visitor) {
$paramsSearchOrderShop['visitor'] = $visitor;
$orderShopVisitor = $this->orderShopRepo->findCartCurrent([
'visitor' => $visitor
]);
} }


$orderShop = $this->orderShopRepo->findCartCurrent($paramsSearchOrderShop);
if($orderShopUser || $orderShopVisitor) {
if($orderShopUser && $orderShopVisitor && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())) {
$this->session->getFlashBag()->add('success', "Votre panier visiteur vient d'être fusionné avec votre panier client.") ;
}
$orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor) ;
}


if (!$orderShop) {
$orderShop = $this->createOrderShop([
'user' => $user,
'visitor' => $visitor,
'merchant' => $this->merchantUtils->getMerchantCurrent()
]);
if(!$user && !$visitor)
{
$this->session->getFlashBag()->add('error', 'Vous devez accepter les cookies ou vous connecter pour créer un panier.') ;
}
else {
if (!$orderShop) {
$orderShop = $this->createOrderShop([
'user' => $user,
'visitor' => $visitor,
'merchant' => $this->merchantUtils->getMerchantCurrent()
]);
}
} }


return $orderShop; return $orderShop;
return $text ; return $text ;
} }


public function mergeOrderShops($orderShop1, $orderShop2)
{
if($orderShop1 && $orderShop2) {

foreach($orderShop2->getOrderProducts() as $orderProduct) {
$this->addOrderProduct($orderShop1, $orderProduct);
$this->em->remove($orderProduct) ;
}

$this->em->remove($orderShop2) ;
$this->em->persist($orderShop1);
$this->em->flush() ;

return $orderShop1 ;
}
else {
return ($orderShop1) ? $orderShop1 : $orderShop2 ;
}
}

} }

Loading…
Cancel
Save