Hỏi về Session php: Tại sao function trong controller lại lấy dữ liệu của session cũ (trước khi update)

Em đang tạo 1giỏ hàng bằng sesion. Em có 1 hàm update số lượng sản phẩm giỏ hàng và reload lại tổng tiền và list các item trong giỏ hàng(button ở phần header).
Sau khi chạy thì đã update các thông tin vào session , đưa ra tổng tiền đúng nhưng list item thì lại không cập nhật mà vẫn lấy dữ liệu cũ.
Em đã thử dùng setTimeout($('#cart-button').load(),2000) để delay hàm ‘load()’ cho chạy chậm hơn hàm cập nhật số lượng xem nó có lấy ra dữ liệu mới không nhưng vẫn không được. Mọi người có thể chỉ em cách khắc phục được không ạ. Em cảm ơn.

P/s: em thử cho $this->ajaxReloadCart($cart->totalQty, $this->money($cart->totalPrice), $cart->items); vào function subOneItem thì lại được nhưng nó in ra cái list ở chỗ tổng tiền. Mọi người giải thích cho e với

Đây là hàm js:

$('#sub-1-{{$item['item']['id']}}').click(function(){
                    var qty = $('#qty-{{$item['item']['id']}}').val();
                    var int_qty = parseInt(qty);
                    if(int_qty > 1){
                        int_qty--;
                        $('input#qty-{{$item['item']['id']}}').val(int_qty);
                        $('#sub-total').load('sub-one/{{$item['item']['id']}}');
                        $('#cart-button').load('reload-mini');
                     }
                     if(int_qty <= 1){
                        int_qty = 1;
                     }

                });

Routes

Route::get('sub-one/{id}','PageController@subOneItem');
Route::get('reload-mini','PageController@reloadMiniCart');

Các hàm trong controller:

  • ajaxReloadCart :
public function ajaxReloadCart($totalQty, $totalPrice, $items){
        echo '  <button type="button" class="btn btn-default">
                    <a href="view-cart">
                        <i class="fa fa-shopping-cart fa-lg"></i>
                        <div class="item-number">'
                            .$totalQty.
                        '</div>
                    </a>
                </button>';
        echo   '<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <span class="caret"></span>
                    <span class="sr-only">Toggle Dropdown</span>
                </button>';
        echo '  <ul class="dropdown-menu cart-items-list" id="cart-items-list">';
        foreach($items as $item){
            $price = $this->money($item['item']['price']);
            echo '  <li>
                    <div class="row" style="margin-left:0; margin-top:10px;">
                        <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
                            <img src="upload/product/'.$item['item']->productimg->first()->name.'" width="100" />
                        </div>
                        <div class="col-xs-8 col-sm-8 col-md-8 col-lg-8">
                            <div class="row">
                                <a href="product/'.$item['item']['id'].'"><strong>'.$item['item']['name'].'</strong></a>
                            </div>
                            <div class="row">'
                                .$price.' X '.$item['qty'].
                            '</div>
                        </div>
                    </div>
                    </li>';
        }
        echo '  <li><a><strong>Subtotal:</strong>&nbsp; <div class="pull-right">'.$totalPrice.'</div></a></li>
                <li role="separator" class="divider"></li><li><a href="#">To Checkout</a></li></ul>';
    }
  • subOneItem :
 public function subOneItem($id){
        $product = Product::find($id);
        $oldCart = Session::has('cart') ? Session::get('cart') : null;
        $cart = new Cart($oldCart);

        $cart->subOne($product, $id);
        Session::put('cart',$cart);


        $totalPrice = $this->money($cart->totalPrice);
        // display mini cart and total price
        echo $totalPrice;

    }
  • reloadMiniCart :
    public function reloadMiniCart(){
        $oldCart = Session::has('cart') ? Session::get('cart') : null;
        $cart = new Cart($oldCart);
         $this->ajaxReloadCart($cart->totalQty, $this->money($cart->totalPrice), $cart->items);
    }

debug code thử xem Session::put(‘cart’,$cart); nó có put cart vào key cart không

1 Like

em dùng dd() để xem session thì nó đã update r. Nhưng vẫn không lấy được dữ liệu mới.
em thử cho $this->ajaxReloadCart($cart->totalQty, $this->money($cart->totalPrice), $cart->items); vào function subOneItem thì lại được nhưng nó in ra cái list ở chỗ tổng tiền

Ở đây người ta không viết ajax theo kiểu này :joy:

1 Like

code của đối tượng này ntn, có thể lỗi sai xuất phát từ đây

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?