S
S
sdgs4s4 .2018-07-17 12:56:53
JavaScript
sdgs4s4 ., 2018-07-17 12:56:53

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

There are js additions
$('.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!');
            }
        });
    });

Adding if already in cart
$('#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!');
            }
        });
    });

Removing a position completely
$('#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!');
            }
        });
    });

And also controllers
Zhmyak
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'));
    }

Model for deleting the entire position
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]);
    }


How to make it so that with a quantity of 4 pieces, delete 1 unit, and not the entire position?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Exploding, 2018-07-17
@Encoderast

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 question

Ask a Question

731 491 924 answers to any question