Crinsane / LaravelShoppingcart

A simple shopping cart implementation for Laravel
MIT License
3.67k stars 1.73k forks source link

After Cart::content() cart become empty #464

Open makzef opened 6 years ago

makzef commented 6 years ago

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)

loaiabdalslam commented 6 years ago

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

makzef commented 6 years ago

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

loaiabdalslam commented 6 years ago

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 :)

rahulinnctech commented 6 years ago

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

Dalot commented 5 years ago

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 commented 5 years ago

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);
}
nicoloubser commented 5 years ago

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.

dgavrilenko commented 4 years ago

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

abdulrehman3725 commented 3 years ago

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