I am trying to make an order with symfony but it doesn’t work as it should. I also let a few of my co-workers look at it but they also didn’t know how to fix it. Can somebody please help me out.
I tried to make an order but for some reason it gives me a blank screen.
trying to make an order with symfony but it doesn’t work
this is the part that serves for putting an item in my shoppingcart:
#[Route('/makeorder/{id}', name: 'app_makeorder')]
public function makeOrder(EntityManagerInterface $em, Request $request, int $id): Response
{
$product = $em->getRepository(Product::class)->find($id);
$form = $this->createFormBuilder()
->add('amount', IntegerType::class, [
'required' => true,
'data' => 1,
'label' => 'aantal'
])
->add('Opslaan', SubmitType::class)
->getForm();
$session = $request->getSession();
$form->handleRequest($request);
if($form->isSubmitted()) {
if(!$session->get('order')) {
$session->set('order', []);
}
$amount = $form->get('amount')->getData();
$order=$session->get('order');
$order[]=[$id, $amount];
$session->set('order', $order);
$this->addFlash('succes', 'product toegevoegd');
return $this->redirectToRoute('app_winkelwagen');
}
return $this->render('winkelwagen/order.html.twig', [
'product' => $product,
'form' => $form
]);
twig file that belongs to above symfony code for putting an item in the shoppingcart:
<div class="card">
<img src="{{ product.image }}" class="card-img-top" alt="product">
<div class="card-body">
<h5 class="card-title"> {{ product.name }}</h5>
<p class="card-text"> {{ product.description }}</p>
<p class="card-text"> {{ product.price }}</p>
{{ form(form) }}
</div>
</div>
this is the code that shows the items in the shopping cart:
#[Route('/show/winkelwagen',name:'show_winkelwagen')]
public function showWinkelwagen(EntityManagerInterface $em,Request $request)
{
$order=$request->getSession()->get('order');
if(!$order) {
$this->addFlash('danger','Je hebt geen producten');
return $this->redirectToRoute('app_winkelwagen');
}
$orderlinesLines=[];
foreach ($order as $line) {
$orderLine=new OrderLine();
$product=$em->getRepository(Product::class)->find($line[0]);
$orderLine->setProduct($product);
$orderLine->setAmount($line[1]);
$orderLines[]=$orderLine;
}
return $this->render('winkelwagen/winkelwagen.html.twig',[
'orderLines'=>$orderLines,
]);
}
twig file that belongs to above symfony code for showing the items in the shopping cart:
{% for line in orderLines %}
<div class="card">
<img src="..." class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title"> {{ line.product.name }}</h5>
<p class="card-text"> {{ line.product.description }}</p>
<p class="card-text"> {{ line.product.price }}</p>
<h4>{{ line.amount }}</h4>
</div>
</div>
{% endfor %}
<a class="btn btn-danger" href="{{ path('clear_winkelwagen') }}">leeg winkelwagen</a>
<a class="btn btn-success" href="{{ path('order_winkelwagen')}}">bestel</a>
this is the code that clears the entire shoppingcart:
#[Route('/clear/winkelwagen',name:'clear_winkelwagen')]
public function clearWinkelwagen(EntityManagerInterface $em,Request $request): Response
{
$order=$request->getSession()->get('order');
if($order) {
$request->getSession('order')->clear();
$this->addFlash('danger','winkelwagen leeg!');
}
return $this->redirectToRoute('app_winkelwagen');
}
this is the code that lets you order the items in the shoppingcart:
#[Route('/order/winkelwagen',name:'order_winkelwagen')]
public function orderWinkelwagen(EntityManagerInterface $em,Request $request):Response
{
$product=$request->getSession()->get('order');
if(!$product) {
$this->addFlash('danger','Je hebt geen producten');
return $this->redirectToRoute('app_order');
}
$order=new Order();
$order->setDate(new DateTime('now'));
$order->setStatus('In behandeling');
foreach ($product as $line) {
$orderLine=new OrderLine();
$product=$em->getRepository(Product::class)->find($line[0]);
$orderLine->setProduct($product);
$orderLine->setAmount($line[1]);
$orderLine->setPurchase($order);
$em->persist($orderLine);
}
$em->persist($order);
$em->flush();
$request->getSession('order')->clear();
$this->addFlash('success','De bestelling is voltooid');
return $this->redirectToRoute('app_winkelwagen');
}
vitalseal20 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.