public function actionProceedToCheckout() { $model = new CheckoutForm(); $user = Yii::$app->user->identity; $total = 0; $session = Yii::$app->session; $products = $session['cart'] ?: []; foreach ($products as $product) { $total += $product['price'] * $product['quantity']; } if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($order = $model->proceedToCheckout($total)) { $ordersProductsForm = new OrdersProductsForm(); foreach ($products as $product) { $orderProducts = ['OrdersProductsForm' => ['orderId' => $order->id, 'productId' => $product['id'], 'price' => $product['price'], 'quantity' => $product['quantity']]]; if ($ordersProductsForm->load($orderProducts) && $ordersProductsForm->validate()) { if ($ordersProductsForm->saveOrdersProducts()) { Cart::delete($product['id']); } } } return $this->redirect('/product/index'); } } return $this->render('proceedToCheckout', ['model' => $model, 'user' => $user, 'total' => $total]); }