melihovv / laravel-shopping-cart

Laravel shopping cart package
MIT License
73 stars 29 forks source link

cart is empty #14

Closed fadedshadowx closed 4 years ago

fadedshadowx commented 4 years ago

Something wrong, I am using Laravel 7.

Using this code:

$cartItem = Cart::add('1', 'ProductName', '100', 1);
dd($cartItem);
$cartItems = Cart::content();
dd($cartItems);

I have:

Melihovv\ShoppingCart\CartItem {#1283 ▼
  -uniqueId: "027c91341fd5cf4d2579b49c4b6a90da"
  +id: "1"
  +name: "ProductName"
  +price: 100.0
  +quantity: 1
  +options: []
}

Illuminate\Support\Collection {#404 ▼
  #items: array:1 [▼
    "027c91341fd5cf4d2579b49c4b6a90da" => Melihovv\ShoppingCart\CartItem {#1283 ▼
      -uniqueId: "027c91341fd5cf4d2579b49c4b6a90da"
      +id: "1"
      +name: "ProductName"
      +price: 100.0
      +quantity: 1
      +options: []
    }
  ]
}

but after reload page, cart is empty. Table 'shopping_cart' is empty too.

My repository: `'repository' => \Melihovv\ShoppingCart\Repositories\ShoppingCartDatabaseRepository::class,

'database' => [

    'connection' => 'mysql',

    'table' => 'shopping_cart',

],`

What's wrong?

melihovv commented 4 years ago

Hello You need to call Cart::store($user->id); to persist you cart

fadedshadowx commented 4 years ago

I don't understand. Can you example full code for create cart and add to cart something? Why you are using $user->id, what if not logged?

melihovv commented 4 years ago

If user is not logged in then use something that can identify him. For example, his session id.

fadedshadowx commented 4 years ago

Ok, so:

Cart::add('1', 'ProductName', '100', 1);
Cart::store('example2');
$cartItems = Cart::content('example2');
dd($cartItems);

i have dumped cart and new record in db, but after use only

$cartItems = Cart::content('example2');
dd($cartItems);

still dd is empty, why? Can you reply me? Documentation does not contain these informations.

edit: ok, after use restore option can use content.

melihovv commented 4 years ago

Yes, you need to use Cart::restore(). Sorry that docs are not so clear. I have almost never used this package myself. I wrote it per one evening for my job interview and after that I have only been maintaining it. You always can send pull request with enhancements.