src/Flexy/FrontBundle/Themes/CoWorking/Service/ShoppingCartService.php line 21
<?php
// src/Service/ShoppingCartService.php
namespace App\Flexy\FrontBundle\Themes\CoWorking\Service;
use App\Flexy\ShopBundle\Entity\Order\Order;
use App\Flexy\ShopBundle\Entity\Order\OrderItem;
use App\Flexy\ShopBundle\Entity\Product\Product;
use Symfony\Component\HttpFoundation\RequestStack;
use Doctrine\ORM\EntityManagerInterface;
class ShoppingCartService
{
private $session;
public function __construct(private RequestStack $requestStack, private readonly EntityManagerInterface $entityManagerInterface)
{
$this->session = $this->requestStack->getSession();
if (!$this->session->has('cart_items')) {
$this->session->set('cart_items', []);
}
}
public function addItem($item,$calculateQty=true)
{
$cartItems = $this->session->get('cart_items');
$id = $item['id'];
if (isset($cartItems[$id])) {
if($calculateQty){
$cartItems[$id]['quantity'] += 1;
}else{
$cartItems[$id]['price'] = $item['price'];
}
} else {
$cartItems[$id] = [
'id' => $item['id'],
'name' => $item['name'],
'price' => $item['price'],
'image' => $item['image'],
'quantity' => 1,
];
}
$this->session->set('cart_items', $cartItems);
}
public function removeItem($id)
{
$cartItems = $this->session->get('cart_items');
if (isset($cartItems[$id])) {
unset($cartItems[$id]);
$this->session->set('cart_items', $cartItems);
}
}
public function updateName($newId,$name,$cat){
$cartItems = $this->session->get('cart_items');
foreach ($cartItems as $key => &$cartItem) {
if ($cartItem['id'] == $cat) {
$cartItem['id'] = $newId;
$cartItem['name'] = $name;
$cartItem['image'] = 'vide';
if ($key != $newId) {
$cartItems[$newId] = $cartItems[$key];
unset($cartItems[$key]);
}
}else if( $cartItem['image'] == 'vide') {
//$cartItem['id'] = $newId;
$cartItem['name'] = $name;
}
// Si l'ID change, mettez à jour également la clé du tableau associatif
/* if ($key != $newId) {
$cartItems[$newId] = $cartItems[$key];
unset($cartItems[$key]);
} */
}
$this->session->set('cart_items', $cartItems);
}
public function updateQuantity($id, $quantity)
{
$cartItems = $this->session->get('cart_items');
if (isset($cartItems[$id])) {
$cartItems[$id]['quantity'] = $quantity;
$this->session->set('cart_items', $cartItems);
}
}
public function createOrder(){
$newOrder = new Order();
//
if(array_key_exists("firstName",$this->getCartExtraInfo())){
$newOrder->setFirstName($this->getCartExtraInfo()["firstName"]);
}
if(array_key_exists("lastName",$this->getCartExtraInfo())){
$newOrder->setFirstName($this->getCartExtraInfo()["lastName"]);
}
foreach($this->getCartItems() as $singleCartItem){
$newOrderItem = new OrderItem();
$product = $this->entityManagerInterface->getRepository(Product::class)->find((int)$singleCartItem["id"]);
$newOrderItem->setProduct($product);
$newOrderItem->setQuantity(1);
$newOrderItem->setPrice($singleCartItem["price"]);
$newOrderItem->setDescription($singleCartItem["name"]);
//
$newOrder->addOrderItem($newOrderItem);
$this->entityManagerInterface->persist($newOrderItem);
}
//dd($this->getCartItems() );
$this->entityManagerInterface->persist($newOrder);
$this->entityManagerInterface->flush();
$this->session->remove('cart_items');
return $newOrder;
}
public function setCartExtraInfo(array $extraInfo)
{
$this->session->set('cart_extra_info', $extraInfo);
}
public function addExtraInfo(array $extraInfo)
{
// Retrieve existing extra info or initialize an empty array
$existingExtraInfo = $this->session->get('cart_extra_info', []);
// Merge the new extra info with the existing data
$mergedExtraInfo = array_merge($existingExtraInfo, $extraInfo);
// Store the merged extra info back into the session
$this->session->set('cart_extra_info', $mergedExtraInfo);
}
// You can also add a method to retrieve the extra info if needed
public function getCartExtraInfo()
{
return $this->session->get('cart_extra_info', []);
}
public function getCartItems()
{
return $this->session->get('cart_items');
}
}