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

@@ -20,7 +20,7 @@ class OrderShopRepository extends BaseRepository implements DefaultRepositoryInt

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

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

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

@@ -9,6 +9,7 @@ use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Lc\ShopBundle\Context\OrderShopInterface;
use Lc\ShopBundle\Context\ProductFamilyUtilsInterface;
use Lc\ShopBundle\Context\UserInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Security;

class OrderUtils
@@ -20,9 +21,11 @@ class OrderUtils
protected $orderShopRepo;
protected $priceUtils ;
protected $productFamilyUtils ;
protected $session ;

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->security = $security;
@@ -31,6 +34,7 @@ class OrderUtils
$this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName());
$this->priceUtils = $priceUtils ;
$this->productFamilyUtils = $productFamilyUtils ;
$this->session = $session ;
}

public function getCartCurrent()
@@ -40,22 +44,41 @@ class OrderUtils
$user = $this->security->getUser();
$visitor = $this->userUtils->getVisitorCurrent();

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

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

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;
@@ -256,4 +279,24 @@ class OrderUtils
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