Ejemplo n.º 1
0
 /**
  * add item to cart, it should have ['item_id','star_id','qty','props']
  * @return string
  */
 public function actionAdd()
 {
     $item_id = (int) Yii::$app->request->post('item_id');
     $star_id = (int) Yii::$app->request->post('star_id');
     $qty = (int) Yii::$app->request->post('qty');
     $data = Yii::$app->request->post('data');
     $props = Yii::$app->request->post('props');
     if ($props) {
         $tmp = explode(';', $props);
         $props = [];
         foreach ($tmp as $v) {
             $a = explode(':', $v);
             $props[$a[0]] = $v;
         }
         $sku = Sku::find()->where(['item_id' => $item_id, 'props' => Json::encode($props)])->one();
         $sku_id = $sku->sku_id;
     }
     $shoppingCartModel = new ShoppingCart();
     if ($shoppingCartModel->add($sku_id, $star_id, $qty, $data)) {
         return Json::encode(['message' => \Yii::t('app', 'add to cart success')]);
     } else {
         return Json::encode(['message' => \Yii::t('app', 'add to cart fail')]);
     }
 }