I am exploring my paths with Symfony2, creating a small e-commerce website for the family wine importer. Gradually, I understand the concept of Symfony2, but when I move on to creating a set of carts, Iβm not quite sure what will be the right way (at least according to Sf2 standards) to implement this.
i made a simple set of session-based telegrams.
My problem is that when I add the product to the basket, then it works until the product identifier is 0-9 and the quantity of the product is increased automatically, but after the product identifier is 10, the quantity will be equal to the product identifier while it should be equal. Product information arrives when we want to receive product information.
I hope this is not a very broad question. I know very well that really reliable shopping basket implementation is a little work.
My code is here:
CartController.php
<?php namespace Webmuch\CartBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Component\HttpFoundation\Response; use Webmuch\ProductBundle\Entity\Product; /** * @Route("/cart") */ class CartController extends Controller { /** * @Route("/", name="cart") */ public function indexAction() { // get the cart from the session $session = $this->getRequest()->getSession(); // $cart = $session->set('cart', ''); $cart = $session->get('cart', array()); // $cart = array_keys($cart); // print_r($cart); die; // fetch the information using query and ids in the cart if( $cart != '' ) { foreach( $cart as $id => $quantity ) { $productIds[] = $id; } if( isset( $productIds ) ) { $em = $this->getDoctrine()->getEntityManager(); $product = $em->getRepository('WebmuchProductBundle:Product')->findById( $productIds ); } else { return $this->render('WebmuchCartBundle:Cart:index.html.twig', array( 'empty' => true, )); } return $this->render('WebmuchCartBundle:Cart:index.html.twig', array( 'product' => $product, )); } else { return $this->render('WebmuchCartBundle:Cart:index.html.twig', array( 'empty' => true, )); } } /** * @Route("/add/{id}", name="cart_add") */ public function addAction($id) { // fetch the cart $em = $this->getDoctrine()->getEntityManager(); $product = $em->getRepository('WebmuchProductBundle:Product')->find($id); //print_r($product->getId()); die; $session = $this->getRequest()->getSession(); $cart = $session->get('cart', array()); // check if the $id already exists in it. if ( $product == NULL ) { $this->get('session')->setFlash('notice', 'This product is not available in Stores'); return $this->redirect($this->generateUrl('cart')); } else { if( isset($cart[$id]) ) { $qtyAvailable = $product->getQuantity(); if( $qtyAvailable >= $cart[$id]['quantity'] + 1 ) { $cart[$id]['quantity'] = $cart[$id]['quantity'] + 1; } else { $this->get('session')->setFlash('notice', 'Quantity exceeds the available stock'); return $this->redirect($this->generateUrl('cart')); } } else { // if it doesnt make it 1 $cart = $session->get('cart', array()); $cart[$id] = $id; $cart[$id]['quantity'] = 1; } $session->set('cart', $cart); return $this->redirect($this->generateUrl('cart')); } } /** * @Route("/remove/{id}", name="cart_remove") */ public function removeAction($id) { // check the cart $session = $this->getRequest()->getSession(); $cart = $session->get('cart', array()); // if it doesn't exist redirect to cart index page. end if(!$cart) { $this->redirect( $this->generateUrl('cart') ); } // check if the $id already exists in it. if( isset($cart[$id]) ) { // if it does ++ the quantity $cart[$id]['quantity'] = '0'; unset($cart[$id]); //echo $cart[$id]['quantity']; die(); } else { $this->get('session')->setFlash('notice', 'Go to hell'); return $this->redirect( $this->generateUrl('cart') ); } $session->set('cart', $cart); // redirect(index page) $this->get('session')->setFlash('notice', 'This product is Remove'); return $this->redirect( $this->generateUrl('cart') ); } }
index.html.twig:
{% block body %} <h1>"FLAIRBAG" SHOPPING-CART</h1> <ul class="thumbnails"> {% if empty %} <h5>Your shopping cart is empty.</h5> {% endif %} {% set cart = app.session.get('cart') %} {% if product %} <ul class="thumbnails"> {% if app.session.hasFlash('notice') %} <divclass="flash-notice"> {{app.session.flash('notice') }} {{ app.session.removeFlash('notice') }} </div> {% endif %} {% for key, item in cart %} <p>ID:{{ key }}</p> <p>Quantity:{{ item }}</p> <button class="btn btn-primary"><a href="{{ path('cart_remove', {'id': key}) }}">Remove</a></button> {% for item in product %} <p>{{ item.title }}</p> <p>{{ item.preview }}</p> {% endfor %} {% endfor %} </ul> {% endif %} </ul> <a href="{{ path('products') }}">Products</a> {% endblock %}
Please help me with this.
Thanks! I appreciate your help.