Answer the question
In order to leave comments, you need to log in
How to remove one item in the cart?
Hello!
There is a basket:
<?php foreach($session['cart'] as $id => $item):?>
<tr>
<td><?= \yii\helpers\Html::img($item['img'], ['alt' => $item['name'], 'height' => 50]) ?></td>
<td><?= $item['name']?></td>
<td><?= $item['qty']?><span data-id="<?= $id ?>" type="submit" class="add-to-cart"><span class="button__inc">+</span></span></td>
<td><?= $item['price']?></td>
<td><span data-id="<?= $id?>" class="glyphicon glyphicon-remove text-danger del-item" aria-hidden="true"></span></td>
</tr>
<?php endforeach?>
$('.add-to-cart').on('click', function (e) {
e.preventDefault();
var id = $(this).data('id'),
qty = $(this).prev('.js-qty').val();
$.ajax({
url: '/cart/add',
data: {id: id, qty: qty},
type: 'GET',
success: function(res){
if(!res) alert('Ошибка!');
showCart(res);
},
error: function(){
alert('Error!');
}
});
});
$('#cart .modal-body').on('click', '.add-to-cart', function () {
var id = $(this).data('id'),
qty = $(this).prev('.js-qty').val();
$.ajax({
url: '/cart/add',
data: {id: id, qty: qty},
type: 'GET',
success: function(res){
if(!res) alert('Ошибка!');
showCart(res);
},
error: function(){
alert('Error!');
}
});
});
$('#cart .modal-body').on('click', '.del-item', function(){
var id = $(this).data('id');
$.ajax({
url: '/cart/del-item',
data: {id: id},
type: 'GET',
success: function(res){
if(!res) alert('Ошибка!');
showCart(res);
},
error: function(){
alert('Error!');
}
});
});
public function actionAdd(){
$id = Yii::$app->request->get('id');
$qty = (int)Yii::$app->request->get('qty');
$qty = !$qty ? 1 : $qty;
$product = Product::findOne($id);
if(empty($product)) return false;
$session =Yii::$app->session;
$session->open();
$cart = new Cart();
$cart->addToCart($product, $qty);
if( !Yii::$app->request->isAjax ){
return $this->redirect(Yii::$app->request->referrer);
}
$this->layout = false;
return $this->render('cart-modal', compact('session'));
}
public function actionClear(){
$session =Yii::$app->session;
$session->open();
$session->remove('cart');
$session->remove('cart.qty');
$session->remove('cart.sum');
$this->layout = false;
return $this->render('cart-modal', compact('session'));
}
public function actionDelItem(){
$id = Yii::$app->request->get('id');
$session =Yii::$app->session;
$session->open();
$cart = new Cart();
$cart->recalc($id);
$this->layout = false;
return $this->render('cart-modal', compact('session'));
}
public function recalcp($id){
if(!isset($_SESSION['cart'][$id])) return false;
$qtyMinus = $_SESSION['cart'][$id]['qty'];
$sumMinus = $_SESSION['cart'][$id]['qty'] * $_SESSION['cart'][$id]['price'];
$_SESSION['cart.qty'] -= $qtyMinus;
$_SESSION['cart.sum'] -= $sumMinus;
unset($_SESSION['cart'][$id]);
}
Answer the question
In order to leave comments, you need to log in
Maybe enough of these "pluses" and "dashes" near the inputs? There is also a normal spinEdit in the form of input type="number", where you can both freely specify the required number, and click with the mouse, while in the field parameters you can set min and max values, step and all this will be correctly processed by the browser, and you only have to pass the value to the function of the type updateItemCart(id, qty) at the blur event: updateItemCart(id, qty)
. from 1 m / p.
data-id="<?= $id?>" - I would put it on tr as the main position container, and it would be easier to search for child elements.
and remove type="submit" from span-a, then preventDefault is not needed, and there is no form there, which means that the code will become even more valid.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question