modules/api/v2/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();
            if (!empty($productData) && $productData instanceof Product && in_array($productData->status_id, [ProductStatus::STATUS_ARCHIVED, ProductStatus::STATUS_PENDING_APPROVAL])) {
                throw new Exception("This product will be available after some time, Please choose another product!");
            }
            $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->tax = $taxPrice;
            $modelSize = Sizes::find()->where(['id' => $model->size])->one();
            if (!empty($modelSize) && $modelSize instanceof Sizes) {
                $model->size_id = $modelSize->id;
                $model->size = $modelSize->size;
            } else {
                $model->size_id = $model->size;
                $model->size = "";
            }
            $model->product_name = (!empty($productData) && $productData instanceof Product && !empty($productData->name)) ? $productData->name : 0;
            $model->category_name = (!empty($productData) && $productData instanceof Product && !empty($productData->category) && $productData->category instanceof ProductCategory && !empty($productData->category->name)) ? $productData->category->name : 0;
            $model->subcategory_name = (!empty($productData) && $productData instanceof Product && !empty($productData->subCategory) && $productData->subCategory instanceof ProductCategory && !empty($productData->subCategory->name)) ? $productData->subCategory->name : 0;
            $model->seller_id = (!empty($productData) && $productData instanceof Product && !empty($productData->user_id)) ? $productData->user_id : 0;
            $model->save(false);
        }
        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 ( ) ; if ( ! empty ( $ productData ) && $ productData instanceof Product && in_array ( $ productData -> status_id , [ ProductStatus :: STATUS_ARCHIVED , ProductStatus :: STATUS_PENDING_APPROVAL ] ) ) { throw new Exception ( " This product will be available after some time, Please choose another product! " ) ; } $ 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 -> tax = $ taxPrice ; $ modelSize = Sizes :: find ( ) -> where ( [ 'id' => $ model -> size ] ) -> one ( ) ; if ( ! empty ( $ modelSize ) && $ modelSize instanceof Sizes ) { $ model -> size_id = $ modelSize -> id ; $ model -> size = $ modelSize -> size ; } else { $ model -> size_id = $ model -> size ; $ model -> size = " " ; } $ model -> product_name = ( ! empty ( $ productData ) && $ productData instanceof Product && ! empty ( $ productData -> name ) ) ? $ productData -> name : 0 ; $ model -> category_name = ( ! empty ( $ productData ) && $ productData instanceof Product && ! empty ( $ productData -> category ) && $ productData -> category instanceof ProductCategory && ! empty ( $ productData -> category -> name ) ) ? $ productData -> category -> name : 0 ; $ model -> subcategory_name = ( ! empty ( $ productData ) && $ productData instanceof Product && ! empty ( $ productData -> subCategory ) && $ productData -> subCategory instanceof ProductCategory && ! empty ( $ productData -> subCategory -> name ) ) ? $ productData -> subCategory -> name : 0 ; $ model -> seller_id = ( ! empty ( $ productData ) && $ productData instanceof Product && ! empty ( $ productData -> user_id ) ) ? $ productData -> user_id : 0 ; $ model -> save ( false ) ; } return $ model ; }
Creates a new CartItem model.
Creates a new CartItem model .
// Update Size data start // Update Size data end