public function actionAdd()
    {
        $model = new CartForm();
        $post = \Yii::$app->request->post();

        if ($model->load(Yii::$app->request->post())) {
            if ($model->validate()) {

                $additionalProductForm = [];
                $productAdditionalProducts = ProductAdditionalProduct::find()->where(['product_id' => $model->productId])
                    ->indexBy('additional_product_id')->all();
                foreach ($productAdditionalProducts as $key => $productAdditionalProduct) {
                    $additionalProductForm[$key] = new AdditionalProductForm();
                }

                if (!empty($productAdditionalProducts)) {
                    Model::loadMultiple($additionalProductForm, $post);
                    Model::validateMultiple($additionalProductForm);
                }

                Module::$cart->add($model->productId, $model->count,
                    json_encode($model->attribute_value_id), $additionalProductForm, $model->combinationId
                );

                if (\Yii::$app->request->isAjax) {
                    $data = [
                        'orderCounterValue' => Module::$cart->getOrderItemsCount(),
                        'totalCost' => \Yii::$app->formatter->asCurrency(Module::$cart->getTotalCost())
                    ];
                    Yii::$app->response->format = Response::FORMAT_JSON;
                    
                    return Json::encode($data);
                }

                Yii::$app->getSession()->setFlash('success',
                    Yii::t('cart', 'You have successfully added this product to cart')
                );
            } else {
                Yii::$app->log->logger->log($model->errors, Logger::LEVEL_ERROR, 'application.shop.product');
                Yii::$app->session->setFlash('error',
                    Yii::t('cart', 'An error occurred while adding a product, please try again later')
                );
            }
        }

        if (\Yii::$app->request->isAjax) {
            return false;
        }

        return $this->redirect(Yii::$app->request->referrer);
    }
public function actionAdd()
    {
        $model = new CartForm();
        $post = \Yii::$app->request->post();

        if ($model->load(Yii::$app->request->post())) {
            if ($model->validate()) {

                $additionalProductForm = [];
                $productAdditionalProducts = ProductAdditionalProduct::find()->where(['product_id' => $model->productId])
                    ->indexBy('additional_product_id')->all();
                foreach ($productAdditionalProducts as $key => $productAdditionalProduct) {
                    $additionalProductForm[$key] = new AdditionalProductForm();
                }

                if (!empty($productAdditionalProducts)) {
                    Model::loadMultiple($additionalProductForm, $post);
                    Model::validateMultiple($additionalProductForm);
                }

                Module::$cart->add($model->productId, $model->count,
                    json_encode($model->attribute_value_id), $additionalProductForm, $model->combinationId
                );

                if (\Yii::$app->request->isAjax) {
                    $data = [
                        'orderCounterValue' => Module::$cart->getOrderItemsCount(),
                        'totalCost' => \Yii::$app->formatter->asCurrency(Module::$cart->getTotalCost())
                    ];
                    Yii::$app->response->format = Response::FORMAT_JSON;
                    
                    return Json::encode($data);
                }

                Yii::$app->getSession()->setFlash('success',
                    Yii::t('cart', 'You have successfully added this product to cart')
                );
            } else {
                Yii::$app->log->logger->log($model->errors, Logger::LEVEL_ERROR, 'application.shop.product');
                Yii::$app->session->setFlash('error',
                    Yii::t('cart', 'An error occurred while adding a product, please try again later')
                );
            }
        }

        if (\Yii::$app->request->isAjax) {
            return false;
        }

        return $this->redirect(Yii::$app->request->referrer);
    }
public function actionAdd ( ) { $ model = new CartForm ( ) ; $ post = \ Yii :: $ app -> request -> post ( ) ; if ( $ model -> load ( Yii :: $ app -> request -> post ( ) ) ) { if ( $ model -> validate ( ) ) { $ additionalProductForm = [ ] ; $ productAdditionalProducts = ProductAdditionalProduct :: find ( ) -> where ( [ 'product_id' => $ model -> productId ] ) -> indexBy ( 'additional_product_id' ) -> all ( ) ; foreach ( $ productAdditionalProducts as $ key => $ productAdditionalProduct ) { $ additionalProductForm [ $ key ] = new AdditionalProductForm ( ) ; } if ( ! empty ( $ productAdditionalProducts ) ) { Model :: loadMultiple ( $ additionalProductForm , $ post ) ; Model :: validateMultiple ( $ additionalProductForm ) ; } Module :: $ cart -> add ( $ model -> productId , $ model -> count , json_encode ( $ model -> attribute_value_id ) , $ additionalProductForm , $ model -> combinationId ) ; if ( \ Yii :: $ app -> request -> isAjax ) { $ data = [ 'orderCounterValue' => Module :: $ cart -> getOrderItemsCount ( ) , 'totalCost' => \ Yii :: $ app -> formatter -> asCurrency ( Module :: $ cart -> getTotalCost ( ) ) ] ; Yii :: $ app -> response -> format = Response :: FORMAT_JSON ; return Json :: encode ( $ data ) ; } Yii :: $ app -> getSession ( ) -> setFlash ( 'success' , Yii :: t ( 'cart' , 'You have successfully added this product to cart' ) ) ; } else { Yii :: $ app -> log -> logger -> log ( $ model -> errors , Logger :: LEVEL_ERROR , 'application.shop.product' ) ; Yii :: $ app -> session -> setFlash ( 'error' , Yii :: t ( 'cart' , 'An error occurred while adding a product, please try again later' ) ) ; } } if ( \ Yii :: $ app -> request -> isAjax ) { return false ; } return $ this -> redirect ( Yii :: $ app -> request -> referrer ) ; }
Adds product to cart
@return array|bool|\yii\web\Response
Adds product to cart