Из вьюхи передаю через ajax id товара в метод контроллера и получаю объект товара по его id:
Код: Выделить всё
public function actionAdd($id) {
$product = Product::findOne($id);
$session = Yii::$app->session;
$session->open();
$cart = new Cart();
$cart->addToCart($product);
$this->layout = false;
return $this->render('cart-modal', compact('session'));
}
Код: Выделить всё
public function addToCart($product, $qty = 1) {
if(isset($_SESSION['cart'][$product->id])) {
$_SESSION['cart'][$product->id]['qty'] += $qty;
} else {
$_SESSION['cart'][$product->id] = [
'qty' => $qty,
'title' => $product->title,
'price' => $product->price,
'image' => $product->image,
];
}
$_SESSION['cart.qty'] = isset($_SESSION['cart.qty']) ? $_SESSION['cart.qty'] + $qty : $qty;
$_SESSION['cart.sum'] = isset($_SESSION['cart.sum']) ? $_SESSION['cart.sum'] + $qty * $product->price : $qty * $product->price;
}
Не могу найти в чем у меня ошибка, прошу помочь.
Спасибо.