S
S
Sunnat Abdukhalilov2018-03-24 14:09:02
Yii
Sunnat Abdukhalilov, 2018-03-24 14:09:02

How to display qty data from ajax session without reloading the page?

I am writing an online store in yii2.
It remains to make sure that when adding, the quantity of goods next to the cart icon changes.
The goods themselves are added by Ajax. With this order.
The cart icon is in view, fixed to the right.
I added to the view

$session = Yii::$app->session;
$session->open();


And here is the cart icon itself
<div class="fix_cart"  onclick="return getCart()"id="fix_cart">
        <li class="header-cart dropdown default-dropdown">
            <a class="dropdown-toggle" data-toggle="dropdown"  aria-expanded="true">
                    <div class="header-btns-icon">
                      <i class="fa fa-shopping-cart"></i>
                          <span class="qty"><?=$_SESSION['cart.qty']?></span>
                      </div>
             </a>
       </li>
</div>


This is the session entry model:
class Cart extends ActiveRecord
{
    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,
                'name' => $product->title,
                'url' => $product->chpu,
                'price' => $product->price,
                'img' => $product->images->filePath
            ];
        }
        $_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;
    }
    public function recalc($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]);
    }
}


Here is the add item function itself:
$('.add-to-cart').on('click', function(e){
    e.preventDefault();
    var id = $(this).data('id'),
        qty = $('#qty').val();
    $.ajax({
        url: '/cart/add',
        data: {id: id, qty: qty},
        type: 'GET',
        success: function(res) {
            // showCart(res);
        },
        error: function () {
            alert('Error');
        }
    });
});

Initially, when adding to the cart, a modal window with a list of goods came out. showCart(res);
Since it is inconvenient for users, I commented on the modal window
and just made an animation in the direction of the cart + the number of goods next to the cart icon
. But without reloading the page, the quantity from the session is not updated

It is necessary that when adding a product
<span class="qty"><?=$_SESSION['cart.qty']?></span>

changed automatically without reloading the page.
Help me please!!!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question