Esempio n. 1
0
 public function actionAddProduct()
 {
     $sess = Yii::app()->user->getState('order_product');
     if (!empty($_POST['OrderProduct']['id']) && !empty($_POST['OrderProduct']['quantity'])) {
         $product = Product::model()->findByPk($_POST['OrderProduct']['id']);
         $orderproduct = new OrderProduct();
         $orderproduct->order_id = 0;
         $orderproduct->product_id = $product->id;
         $orderproduct->name = $product->getName();
         $orderproduct->model = $product->model;
         $orderproduct->quantity = $_POST['OrderProduct']['quantity'];
         $orderproduct->price = $product->price;
         $orderproduct->total = $orderproduct->quantity * $product->price;
         $orderproduct->tax = 0;
         if ($this->addProductOption($product->id) && $orderproduct->validate()) {
             $orderproduct->order_options = $this->_orderoption;
             $orderoptions = $this->_orderoption;
             if (!empty($sess)) {
                 $addnew = true;
                 foreach ($sess as $i => $cart) {
                     $match = $match2 = false;
                     $diff = $orderproduct->compare($cart);
                     if ($diff) {
                         $sess[$i]->quantity += $_POST['OrderProduct']['quantity'];
                         $addnew = false;
                         break;
                     }
                 }
                 if ($addnew) {
                     $sess[] = $orderproduct;
                 }
             } else {
                 $sess = array($orderproduct);
             }
             Yii::app()->user->setState('order_product', $sess);
             $this->renderPartial('_products', array('orderproduct' => $sess), false, true);
             Yii::app()->end();
         } else {
             Yii::app()->user->setFlash(TbHtml::ALERT_COLOR_ERROR, Yii::t('info', 'Please fill out all required fields for product options'));
             Yii::log("OrderCreateActionvaliid: " . CVarDumper::dumpAsString($orderproduct->getErrors()), CLogger::LEVEL_ERROR, "order.actions.create");
         }
     } else {
         Yii::app()->user->setFlash(TbHtml::ALERT_COLOR_ERROR, Yii::t('info', 'Please fill out all required fields'));
     }
     if (empty($sess)) {
         $sess = array();
     }
     $norderopt = array();
     if (isset($_POST['OrderProduct']['id'])) {
         $norderopt = $this->loadProductOption($_POST['OrderProduct']['id']);
     }
     $this->renderPartial('_products', array('orderproduct' => $sess, 'orderoption' => $norderopt));
 }