/** * @Route("/event/{hash}/shops/{shopName}/add-to-cart/{productId}", name="_add_to_cart") * @Template() */ public function addToCartAction($hash, $productId, $shopName) { $em = $this->getDoctrine()->getManager(); $user = $this->getUser(); $product = $em->getRepository('DTRBundle:Product')->find($productId); $event = $em->getRepository('DTRBundle:Event')->findByHash($hash); $member = $em->getRepository('DTRBundle:Member')->findByEventUser($event[0], $user); $item = $em->getRepository('DTRBundle:Item')->findByProductAndMember($product, $member); if ($item) { $item->setQuantity($item->getQuantity() + 1); } else { $item = new Item(); $item->setProduct($product); $item->setMember($member); $member->addItem($item); $em->persist($item); } // if ($item) { // $item->setQuantity($item->getQuantity()+1); // } else { // $item = new Item(); // $item->setProduct($product); // $item->setMember($member); // $em->persist($item); // } $em->flush(); return $this->forward('DTRBundle:Default:shop', array('hash' => $hash, 'shopName' => $shopName)); }
/** * Add item * * @param \DTR\DTRBundle\Entity\Item $item * * @return Member */ public function addItem(Item $item) { $this->items[] = $item; $item->setMember($this); $price = $item->getProduct()->getPrice(); $this->increaseDebt($price); $this->increaseTotalPrice($price); return $this; }