/** * Finds the Cart model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Cart the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { $where = ['id' => $id, 'status' => [Cart::CART_SOLD, Cart::CART_REFUNDED]]; $user = \Yii::$app->user->identity; if (!$user->admin) { $where['customer_id'] = $user->getId(); } return Cart::findOne($where); }
public function actionLoadCart() { $return = []; $total = 0; if (isset(Yii::$app->user->id)) { $cart = Cart::findOne(['user_id' => Yii::$app->user->id, 'status' => 'CREATED']); if ($cart) { foreach ($cart->productscarts as $k => $productCart) { $aux = []; $aux['name'] = $productCart->product->name; $aux['value'] = $productCart->quantity; $aux['won'] = $productCart->won; $return[$productCart->product->id] = $aux; $total += $productCart->product->price * $productCart->quantity; } $return['total'] = $total; } } else { $cart = Yii::$app->session['cart']; if ($cart) { foreach ($cart as $k => $ca) { $aux = []; $product = Product::findOne($k); $aux['name'] = $product->name; $aux['value'] = $ca; $aux['won'] = 'NO'; $return[$product->id] = $aux; $total += $product->price * $ca; } $return['total'] = $total; } } echo json_encode($return); }