bumbummen99 / LaravelShoppingcart

A simple shopping cart implementation for Laravel
MIT License
507 stars 235 forks source link

Undefined index: weight #20

Closed ghost closed 5 years ago

ghost commented 5 years ago

Hello,

I have a problem whan I want to add product on my cart. I have this error Undefined index: weight on /vendor/bumbummen99/shoppingcart/src/CartItem.php

`public static function fromArray(array $attributes) { $options = array_get($attributes, 'options', []);

    return new self($attributes['id'], $attributes['name'], $attributes['price'], $attributes['weight'], $options);
}`

This is my public function: ` public function addToCart() { $product = Product::find(request()->product_id);

    $cartItem = Cart::add([
        'id' => $product->id,
        'name' => $product->name,
        'price' => $product->price,
        'quantity' => request()->quantity
    ]);

    Cart::associate($cartItem->rowId, 'App\Product');

    return redirect()->route('cart')->with('success', true)->with('message', 'Votre produit à bien été ajouté au panier');
}`

Do you have idea how to fix that?

Thanks

ShapesGraphicStudio commented 5 years ago

Hello @c-bringer ,

I'm having the same issue, migrating from gloudemans/shoppingcart to bumbummen99/shoppingcart to be able to update from Laravel 5.7. to 5.8..

No issue on a fresh install I did 2 months ago though.

On the old site I have an add to cart function looking the same as yours, on the new website it looks like :

Cart::add($product, 1, ['type' => 'Product']);

I fixed this by adding 'weight' => 0 like :

$cartItem = Cart::add([
        'id' => $product->id,
        'name' => $product->name,
        'price' => $product->price,
        'weight' => 0,
        'quantity' => request()->quantity
    ]);

    Cart::associate($cartItem->rowId, 'App\Product');

    return redirect()->route('cart')->with('success', true)->with('message', 'Votre produit à bien été ajouté au panier');

But I'm not sure it should be mandatory as this should have been handled by :

public function add($id, $name = null, $qty = null, $price = null, $weight = 0, array $options = [])

More info would be appreciated.

bumbummen99 commented 5 years ago

Yes, this version of the package does include Crisane/LaravelShoppingcart#502 and requires to set a weight, although this should in fact be handled by the default parameter. I will take a look into this.

ghost commented 5 years ago

Ok thanks everyone for your help!