Controller/Buyer/CartController.php
public function addProductAction(Product $product, $quantity, Request $request)
    {
        // Find OrderCollection
        $orderCollection = $this->getCurrentCart();

        $quantity = array_key_exists('quantity', $_POST) ? (int) $_POST['quantity'] : $quantity;

        try
        {
            if ('-' == $request->get('remove'))
            {
                $quantity = -1 * $quantity;
            }

            // Add Product to OrderCollection
            $lineItem = $orderCollection->addProduct($product, $quantity);

            // Persisting cascades to Order and OrderCollection
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($lineItem);
            $em->flush();

            if ($quantity > 0) {
                $notice = 'Added '.$quantity.' '
                    .$product->getUnitForNumber($quantity).' of '
                    .$product->getName().' to your cart';
            } else {
                $quantity = abs($quantity);
                $notice = 'Removed '.$quantity.' '
                    .$product->getUnitForNumber($quantity).' of '
                    .$product->getName().' from your cart';
            }

            // Set flash message
            $this->get('session')->getFlashBag()->add('notice', $notice);

            // Save the OrderCollection to the session
            $this->getRequest()->getSession()->set('cart_id', $orderCollection->getId());
        }
        catch (\Exception $e)
        {
            // could not add Product to cart
        }

        $last_route = $request->get('referer');

        if (in_array($last_route, array('_welcome')))
        {
            return $this->redirect($this->generateUrl($last_route));
        }

        return $this->redirect($this->generateUrl('Profile_product_show', array(
            'product_slug' => $product->getSlug(),
            'slug'         => $product->getSeller()->getSlug(),
        )));
    }
public function addProductAction(Product $product, $quantity, Request $request)
    {
        // Find OrderCollection
        $orderCollection = $this->getCurrentCart();

        $quantity = array_key_exists('quantity', $_POST) ? (int) $_POST['quantity'] : $quantity;

        try
        {
            if ('-' == $request->get('remove'))
            {
                $quantity = -1 * $quantity;
            }

            // Add Product to OrderCollection
            $lineItem = $orderCollection->addProduct($product, $quantity);

            // Persisting cascades to Order and OrderCollection
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($lineItem);
            $em->flush();

            if ($quantity > 0) {
                $notice = 'Added '.$quantity.' '
                    .$product->getUnitForNumber($quantity).' of '
                    .$product->getName().' to your cart';
            } else {
                $quantity = abs($quantity);
                $notice = 'Removed '.$quantity.' '
                    .$product->getUnitForNumber($quantity).' of '
                    .$product->getName().' from your cart';
            }

            // Set flash message
            $this->get('session')->getFlashBag()->add('notice', $notice);

            // Save the OrderCollection to the session
            $this->getRequest()->getSession()->set('cart_id', $orderCollection->getId());
        }
        catch (\Exception $e)
        {
            // could not add Product to cart
        }

        $last_route = $request->get('referer');

        if (in_array($last_route, array('_welcome')))
        {
            return $this->redirect($this->generateUrl($last_route));
        }

        return $this->redirect($this->generateUrl('Profile_product_show', array(
            'product_slug' => $product->getSlug(),
            'slug'         => $product->getSeller()->getSlug(),
        )));
    }
public function addProductAction ( Product $ product , $ quantity , Request $ request ) { // Find OrderCollection $ orderCollection = $ this -> getCurrentCart ( ) ; $ quantity = array_key_exists ( 'quantity' , $ _POST ) ? ( int ) $ _POST [ 'quantity' ] : $ quantity ; try { if ( '-' == $ request -> get ( 'remove' ) ) { $ quantity = - 1 * $ quantity ; } // Add Product to OrderCollection $ lineItem = $ orderCollection -> addProduct ( $ product , $ quantity ) ; // Persisting cascades to Order and OrderCollection $ em = $ this -> getDoctrine ( ) -> getEntityManager ( ) ; $ em -> persist ( $ lineItem ) ; $ em -> flush ( ) ; if ( $ quantity > 0 ) { $ notice = 'Added ' . $ quantity . ' ' . $ product -> getUnitForNumber ( $ quantity ) . ' of ' . $ product -> getName ( ) . ' to your cart' ; } else { $ quantity = abs ( $ quantity ) ; $ notice = 'Removed ' . $ quantity . ' ' . $ product -> getUnitForNumber ( $ quantity ) . ' of ' . $ product -> getName ( ) . ' from your cart' ; } // Set flash message $ this -> get ( 'session' ) -> getFlashBag ( ) -> add ( 'notice' , $ notice ) ; // Save the OrderCollection to the session $ this -> getRequest ( ) -> getSession ( ) -> set ( 'cart_id' , $ orderCollection -> getId ( ) ) ; } catch ( \ Exception $ e ) { // could not add Product to cart } $ last_route = $ request -> get ( 'referer' ) ; if ( in_array ( $ last_route , array ( '_welcome' ) ) ) { return $ this -> redirect ( $ this -> generateUrl ( $ last_route ) ) ; } return $ this -> redirect ( $ this -> generateUrl ( 'Profile_product_show' , array ( 'product_slug' => $ product -> getSlug ( ) , 'slug' => $ product -> getSeller ( ) -> getSlug ( ) , ) ) ) ; }
add Product to Cart

@author Tom Haskins-Vaughan <tom@harvestcloud.com>
@since  2012-04-09

@Route("/cart/add-product/{id}/{quantity}")
@ParamConverter("product", class="HarvestCloudCoreBundle:Product")

@param  Product  $product
@param  Request $request
add Product to Cart