LaravelShoppingcart icon indicating copy to clipboard operation
LaravelShoppingcart copied to clipboard

After Cart::content() cart become empty

Open makzef opened this issue 6 years ago • 9 comments

Hi! I making some API application with Laravel and use this package in it. When I add some items to cart and store them - everything is all right (post route). But when I use other route to get all cart and use method Cart::content() or Cart::restore() it delete cart record from database, and when I trying to add something new to cart (post route), it only one product, all that was before gone.
There was a problem when i get all cart by Cart::content() second time, cart was empty. Looking for sollution I found in issues that problem was with session (API does not have session). I just move routes to the web, it solve the problem. But after that when you trying to add some more items, all previous was deleted.
Why Cart::content() and Cart::restore() delete cart record from database? How to fix this??? Please, help. And sorry for my English)

makzef avatar May 18 '18 12:05 makzef

hey man can you tell me how to create api from this package i need to add it to my andriod project :))

loaiabdalslam avatar May 18 '18 13:05 loaiabdalslam

I made some mistake, I mean that I use this package in my api application... I am not create api from this package. You can use laravel/passport or tymon/jwt, that can help to create api application)))

makzef avatar May 18 '18 13:05 makzef

i got idea for you and for me too , look you can make a small api for small Model you will create right now ok order,shop cart when user click on it add item and item's Data into table , then make him go to check out with this data $product->id $product->name $product->qty $product->price then you will get Total of all subtotal @foreach of them subtotal results and check out :)

loaiabdalslam avatar May 18 '18 14:05 loaiabdalslam

Cart item empty when I use cart::content()

rahulinnctech avatar May 23 '18 13:05 rahulinnctech

Cart::content() is returning empty for me as well. I am using Laravel 5.7

public function store(Request $request)
{
   $id = $request->product_id;
    $title = $request->title;
    $quantity = $request->quantity;
    $price = $request->price;
    Cart::add([
        'id' => $id, 
        'name' => $title, 
        'qty' => $quantity, 
        'price' => $price 
        ])->associate('App\Product');
    
    return response()->json("success",200); }
  

public function index() { $items = Cart::content(); return response()->json( $items, 200); }

Returning

Collection {#433 #items: [] }

Dalot avatar Feb 27 '19 08:02 Dalot

I was not able to solve this issue, I think along the way, the session was being destroyed, not sure why though. Maybe it has something to do with changing the page in Vuejs that somehow was trashing the session in the backend...

I just used the localStorage to save the data and just put the Cart::add() and Cart::content() on the same controller endpoint.

public function store(Request $request)
{
      $id = $request->product_id;
      $title = $request->title;
      $quantity = $request->quantity;
      $price = $request->price;

      Cart::add([
          'id' => $id,
          'name' => $title, 
           'qty' => $quantity, 
          'price' => $price 
      ]);

      $cartItems = Cart::content()->toArray();

      return response()->json($cartItems,200);
}


Dalot avatar Feb 27 '19 11:02 Dalot

I had the same issue. My problem was that my SESSION_DOMAIN was wrong(defaulted to nil). This caused my session to behave odd and my data not to persist in the session.

nicoloubser avatar Jul 11 '19 14:07 nicoloubser

you need to use a web route where the session is on,

dgavrilenko avatar Mar 14 '20 11:03 dgavrilenko

SESSION_DOMAIN

@nicoloubser can you please help me?

My laravel application is redirecting after payment. When I try to access cart::checkout() it is empty. I am on localhost. Please help

abdulrehman3725 avatar Jan 30 '21 20:01 abdulrehman3725