Example #1
0
 /**
  *  Add a new product to shopping cart
  */
 public function addToCart()
 {
     // avoid search engines adding items to cart...
     if ($this->request->get('csid') && $this->request->get('csid') != session_id()) {
         return new RawResponse();
     }
     ActiveRecordModel::beginTransaction();
     if (!$this->request->get('count')) {
         $this->request->set('count', 1);
     }
     if ($id = $this->request->get('id')) {
         $res = $this->addProductToCart($id);
         if ($res instanceof ActionRedirectResponse) {
             if ($this->isAjax()) {
                 return new JSONResponse(array('__redirect' => $this->application->getActionRedirectResponseUrl($res)));
             } else {
                 return $res;
             }
         }
         if ($res->count->get() < $this->request->get('count')) {
             $this->setErrorMessage($this->makeText('_add_to_cart_quant_error', array(Product::getInstanceByID($id)->getName($this->getRequestLanguage()), $res->count->get(), $this->request->get('count'))));
         }
         $this->setMessage($this->makeText('_added_to_cart', array(Product::getInstanceByID($id)->getName($this->getRequestLanguage()))));
     }
     if ($ids = $this->request->get('productIDs')) {
         $added = false;
         foreach ($ids as $id) {
             $res = $this->addProductToCart($id, 'product_' . $id . '_');
             if ($res instanceof ActionRedirectResponse) {
                 //return $res;
             } else {
                 if ($res) {
                     $added = true;
                 }
             }
         }
         if ($added) {
             $this->setMessage($this->translate('_selected_to_cart'));
         }
     }
     if (!$this->user->isAnonymous()) {
         $this->order->setUser($this->user);
     }
     $this->order->mergeItems();
     SessionOrder::save($this->order);
     ActiveRecordModel::commit();
     if (!$this->isAjax()) {
         if ($this->config->get('SKIP_CART')) {
             return new ActionRedirectResponse('checkout', 'index');
         } else {
             return new ActionRedirectResponse('order', 'index', array('query' => 'return=' . $this->request->get('return')));
         }
     } else {
         return $this->cartUpdate();
     }
 }