orderUtils = $orderUtils ;
$this->userUtils = $userUtils ;
$this->router = $router ;
$this->productFamilyRepository = $this->em->getRepository($this->em->getClassMetaData(ProductFamilyInterface::class)->getName()) ;
$this->orderProductRepository = $this->em->getRepository($this->em->getClassMetaData(OrderProductInterface::class)->getName()) ;
}
public function addProductFamily(Request $request)
{
$user = $this->security->getUser() ;
$visitor = $this->userUtils->getVisitorCurrent() ;
$return = [] ;
$data = $request->request->all() ;
if(isset($data['order_products']['id_product_family'])) {
$idProductFamily = $data['order_products']['id_product_family'] ;
$this->productFamily = $this->productFamilyRepository->find($idProductFamily) ;
// alerte si cookies non acceptés
if (!$user && !$visitor) {
$this->addFlash('error', 'Vous devez accepter les cookies ou vous connecter pour ajouter un produit.');
return false ;
}
if($this->productFamily) {
$form = $this->createForm(OrderProductsType::class, ['id_product_family' => $this->productFamily->getId()]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$orderShop = $this->orderUtils->getCartCurrent() ;
$data = $form->getData() ;
foreach($data as $orderProduct) {
if($orderProduct instanceof OrderProductInterface) {
$addOrderProduct = $this->orderUtils->addOrderProduct($orderShop, $orderProduct) ;
if($addOrderProduct && $orderProduct->getQuantityOrder() > 0) {
$this->orderProducts[] = $orderProduct ;
}
}
}
}
}
}
return new JsonResponse($return) ;
}
public function deleteReductionCart(Request $request)
{
$id = $request->get('id') ;
$orderReductionCartRepository = $this->em->getRepository($this->em->getClassMetadata(OrderReductionCartInterface::class)->getName()) ;
$orderReductionCart = $orderReductionCartRepository->findOneById((int) $id) ;
$orderShop = $this->orderUtils->getCartCurrent() ;
if($orderReductionCart && $orderShop->getOrderReductionCarts() && $orderShop->getOrderReductionCarts()->contains($orderReductionCart)) {
$this->em->remove($orderReductionCart) ;
$this->em->flush() ;
$this->addFlash('success', 'La réduction a bien été supprimée de votre panier.') ;
}
else {
$this->addFlash('error', 'Une erreur est survenue lors de la suppression de la réduction. ') ;
}
$referer = $request->headers->get('referer');
if($referer) {
return $this->redirect($referer);
}
else {
return $this->redirectToRoute('frontend_order_cart');
}
}
}