i need create function changes value gets backend , sends new value backend...
this created far. current stock of product(which 50 in example): check if stock larger quantity of product , if increase quantity 1, , remove 1 value stock. @ end save cart , save new stock(i think). not work , still having 50 in stock.
public function addaction($id) { $em = $this->getdoctrine()->getmanager(); $product = $em->getrepository('mpshopbundle:product')->find($id); $qtyavailable = $product->getstock(); // check cart $session = $this->getrequest()->getsession(); $cart = $session->get('cart', array()); if( isset($cart[$id]) ) { // check if array has productid if ( $qtyavailable > $cart[ $id ]) { $cart[ $id ] = $cart[ $id ] + 1; $qtyavailable = $qtyavailable - 1; } else { return $this->redirect($this->generateurl('cart')); } } else { // if doesnt make 1 $cart = $session->get('cart', array()); $cart[$id] = 1; } $product->setstock('qtyavailable', $qtyavailable); $session->set('cart', $cart); return $this->redirect( $this->generateurl('cart') ); }
usually need give value doctrine entity setter.
$product->setstock('qtyavailable', $qtyavailable);
should be
$product->setstock($qtyavailable);
if don't have custom code in setstock() method.
also, need call
$em->persist($product); $em->flush(); // runs query on database.
after set new stock.
Comments
Post a Comment