S
S
sdgs4s4 .2018-07-18 07:49:00
Yii
sdgs4s4 ., 2018-07-18 07:49:00

Plus or minus items in the cart. How to do a delete?

Hello!
Visually, this is the thing)
5b4ec6a0717c2107417133.jpeg
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
And this is js, controllers, models for deletion, I can’t understand what needs to be done here
The problem is that when deleting it deletes the position of the goods completely, regardless of 4 pcs or 1
JS
$('#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!');
        }
    });
});

Controller
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'));
    }

Model
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

1 answer(s)
M
Maxim Timofeev, 2018-07-18
@Encoderast

<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 question

Ask a Question

731 491 924 answers to any question