setQuantity() public method

Will automatically add or remove item, if necessary.
public setQuantity ( Buyable $buyable, integer $quantity = 1, $filter = [] ) : boolean | OrderItem
$buyable Buyable
$quantity integer
return boolean | OrderItem false or the new/existing item
 /**
  * Sets the exact passed quantity.
  * Note: If no ?quantity=x is specified in URL, then quantity will be set to 1.
  *@return Mixed - if the request is AJAX, it returns JSON - CartResponse::ReturnCartData(); If it is not AJAX it redirects back to requesting page.
  */
 public function setquantityitem($request)
 {
     $this->cart->setQuantity($this->buyable(), $this->quantity(), $this->parameters());
     return $this->cart->setMessageAndReturn();
 }
Esempio n. 2
0
 public function executeAddToCart()
 {
     if ($this->getRequest()->isXmlHttpRequest()) {
         sfProjectConfiguration::getActive()->loadHelpers('Partial');
         $this->user = $this->getUser()->getRaykuUser();
         $item_id = $this->getRequestParameter('it');
         $quantity = 1;
         $this->item = ItemPeer::retrieveByPK($item_id);
         if (!$this->item->getIsActive()) {
             $this->item = NULL;
         }
         if ($this->item) {
             if ($this->item->getQuantity() > $quantity) {
                 $this->user = $this->getUser()->getRaykuUser();
                 $c = new Criteria();
                 $c->add(ShoppingCartPeer::IS_ACTIVE, true);
                 $c->add(ShoppingCartPeer::USER_ID, $this->user->getId());
                 $c->addDescendingOrderByColumn(ShoppingCartPeer::CREATED_AT);
                 $this->cart_items = ShoppingCartPeer::doSelect($c);
                 $tot_price = 0;
                 foreach ($this->cart_items as $cart_item) {
                     $tot_price = $tot_price + $cart_item->getTotalPrice() + $cart_item->getTotalShippingCharge();
                 }
                 $tot_price = $tot_price + $this->item->getPricePerUnit() * $quantity + $this->item->getShippingChargePerUnit() * $quantity;
                 if ($tot_price <= $this->user->getPoints()) {
                     $shopping_cart = new ShoppingCart();
                     $shopping_cart->setItemId($item_id);
                     $shopping_cart->setUserId($this->user->getId());
                     $shopping_cart->setQuantity($quantity);
                     $shopping_cart->setTotalPrice($this->item->getPricePerUnit() * $quantity);
                     $shopping_cart->setTotalShippingCharge($this->item->getShippingChargePerUnit() * $quantity);
                     $shopping_cart->setIsActive(true);
                     $shopping_cart->save();
                     $quantity_avail = $this->item->getQuantity();
                     $quantity_avail = $quantity_avail - $quantity;
                     $this->item->setQuantity($quantity_avail);
                     $this->item->save();
                     return $this->renderText(get_component('shop', 'cartBox'));
                 } else {
                     return $this->renderText('<span style="line-height:26px"><center>Sorry! Not enough RP!</center></span>' . get_component('shop', 'cartBox'));
                 }
             } else {
                 return $this->renderText('Sorry! Item out of stock' . get_component('shop', 'cartBox'));
             }
         } else {
             return $this->renderText('Sorry! No item is there' . get_component('shop', 'cartBox'));
         }
     }
 }