modules/api/v1/controllers/CartItemController.php
/**
     * Creates a new CartItem model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
Creates a new CartItem model.
If creation is successful, the browser will be redirected to the 'view' page.
@return mixed
Creates a new CartItem model . If creation is successful the browser will be redirected to the ' view ' page . @return mixed
public function actionCreate()
    {
        $model = new CartItem();
        $postData = Yii::$app->request->post();
        $cartIteam['CartItem'] = $postData;
        $cartIteam['CartItem']['user_id'] = Yii::$app->user->identity->id;
        if ($model->load($cartIteam) && $model->validate()) {
            $cartIteamAlreadyAdded = CartItem::find()->where(['product_id' => $model->product_id, 'user_id' => $model->user_id, 'is_checkout' => CartItem::IS_CHECKOUT_NO, 'color' => $model->color, 'size' => $model->size])->one();
            if (!empty($cartIteamAlreadyAdded) && $cartIteamAlreadyAdded instanceof CartItem) {
                throw new BadRequestHttpException('You have already this product added to the cart"');
            }
            $productData = Product::find()->where(['id' => $model->product_id])->one();
            $basePrice = (!empty($productData) && $productData instanceof Product && !empty($productData->price)) ? $productData->price * $model->quantity : 0;
            $taxPrice = (!empty($productData) && $productData instanceof Product && !empty($productData->option_price)) ? $productData->option_price * $model->quantity : 0;
            $model->price = ($basePrice + $taxPrice);
            $model->save();
        }
        return $model;
    }
public function actionCreate ( ) { $ model = new CartItem ( ) ; $ postData = Yii :: $ app -> request -> post ( ) ; $ cartIteam [ 'CartItem' ] = $ postData ; $ cartIteam [ 'CartItem' ] [ 'user_id' ] = Yii :: $ app -> user -> identity -> id ; if ( $ model -> load ( $ cartIteam ) && $ model -> validate ( ) ) { $ cartIteamAlreadyAdded = CartItem :: find ( ) -> where ( [ 'product_id' => $ model -> product_id , 'user_id' => $ model -> user_id , 'is_checkout' => CartItem :: IS_CHECKOUT_NO , 'color' => $ model -> color , 'size' => $ model -> size ] ) -> one ( ) ; if ( ! empty ( $ cartIteamAlreadyAdded ) && $ cartIteamAlreadyAdded instanceof CartItem ) { throw new BadRequestHttpException ( 'You have already this product added to the cart"' ) ; } $ productData = Product :: find ( ) -> where ( [ 'id' => $ model -> product_id ] ) -> one ( ) ; $ basePrice = ( ! empty ( $ productData ) && $ productData instanceof Product && ! empty ( $ productData -> price ) ) ? $ productData -> price * $ model -> quantity : 0 ; $ taxPrice = ( ! empty ( $ productData ) && $ productData instanceof Product && ! empty ( $ productData -> option_price ) ) ? $ productData -> option_price * $ model -> quantity : 0 ; $ model -> price = ( $ basePrice + $ taxPrice ) ; $ model -> save ( ) ; } return $ model ; }
Creates a new CartItem model.
Creates a new CartItem model .