Answer the question
In order to leave comments, you need to log in
Plus or minus items in the cart. How to do a delete?
Hello!
Visually, this is the thing)
There is a display of goods in a modal window
<?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>
<span data-id="<?= $id ?>" type="submit" class="del-n"><span class="button__inc">-</span></span>
<?= $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?>
.del-n
- must remove 1 unit of goods, but does not remove .add-to-cart
- adds 1 unit. goods, made, works $('#cart .modal-body').on('click', '.del-n', function () {
var id = $(this).data('id'),
qty = $(this).prev('.js-qty').val();
$.ajax({
url: '/cart/del-pr',
data: {id: id, qty: qty},
type: 'GET',
success: function(res){
if(!res) alert('Ошибка!');
showCart(res);
},
error: function(){
alert('Error!');
}
});
});
public function actionDelPr(){
$id = Yii::$app->request->get('id');
$qty = (int)Yii::$app->request->get('qty');
$session =Yii::$app->session;
$session->open();
$cart = new Cart();
$cart->recalcp($id, $qty);
$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'] === 1;
$sumMinus = $_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
<span type="submit"
Is this a new word in web design?
did you check it there? There's undefined because there's nothing before it at the same level. Or rather an error in the console.
PS: You work with js - the console is always open, see what flies away with ajax. Then you would immediately notice the problem
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question