Crinsane / LaravelShoppingcart

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

Gloudemans \ Shoppingcart \ Exceptions \ ShoppingcartInvalidRowIDException #36

Closed lstables closed 9 years ago

lstables commented 10 years ago

I'm getting the above message when trying to remove an item from my cart.

< a href="/cart/remove/{{ $cart_item->rowid }}">Remove

Which is wrapped in a foreach that comes from:

$carts = Cart::content();

And i'm adding content like so:

$cartContents = Cart::add(array('id' => randomNumber(), 'name' => $input['product_name'], 'qty' => 1, 'price' => $price));

But when I do Cart::remove($cart_item->rowid) i get the above error?

Any idea's?

Crinsane commented 10 years ago

Can you echo the result of $cart_item->rowid; and paste it here?

lstables commented 10 years ago

dd($cart_item->rowid);

returns

 string '63f3b79290994b2fcfffbe28f17e4f95' (length=32)
Crinsane commented 10 years ago

And result of dd(Cart::content()->toArray());?

lstables commented 10 years ago
array (size=3)
  '9ac92c0339d8391720227fb7fb982e49' => 
    array (size=7)
      'rowid' => string '9ac92c0339d8391720227fb7fb982e49' (length=32)
      'id' => int 6909907
      'name' => string 'V3000' (length=5)
      'qty' => int 1
      'price' => string '17500' (length=5)
      'options' => 
        array (size=0)
          empty
      'subtotal' => int 17500

obivously a different rowid as i refreshed.

Crinsane commented 10 years ago

Okay, well, we need do know if they are the same when you want to remove an item. Because the only time you could get this exception is when you try to update or remove an item that's not in the cart. So that's what we need to find out

lstables commented 10 years ago

Right ok i'll do the full checkout process again from start to finish and see what happens

Crinsane commented 10 years ago

Great. I've did a quick check myself, clean Laravel install, add a few items, tried to remove them, and everything works perfectly. So you might want to see if you don't do anything to the $rowid you're passing to the Cart::remove(); method.

But I'll keep this open for now

lstables commented 10 years ago

Ok great I'll try that, and update if needed.

lstables commented 10 years ago

Mmmm, I'm still getting the same error here! Even after clearing browser cache and starting a new Shopping Cart process from scratch.

lstables commented 10 years ago

Also adding more than one item doesn't get added to the cart... It only puts one item in.

Crinsane commented 10 years ago

Please give me more of your code, I need to recreate your setup to see if I can find the problem.

lstables commented 10 years ago

Controller method - http://laravel.io/bin/yQr2m Cart View Page - http://laravel.io/bin/komNX Remove Item (also doesn't appear to work) - http://laravel.io/bin/KdjJD Routes File - http://laravel.io/bin/ekbK6

Crinsane commented 10 years ago

I'm looking at this line:

$cartContents = Cart::add(array('id' => randomNumber(), 'name' => $input['product_name'], 'qty' => 1, 'price' => $price, 'delivery' => $input['delivery']));

What does randomNumber() do? (I guess create a random number, but does it work ok? What if you at random get the same two number for two different products?) And you're also adding an array item 'delivery', which is not in the package by default. Are you using a fork?

lstables commented 10 years ago

randomNumber() just generates a random number for customer ref, i want to add delivery in the array to get the value as each product has a different delivery charge.

Crinsane commented 10 years ago

I don't really think that's how the package works. The ID you have to give is the ID to the product. Each time you add the same product, it has to have the same id. Otherwise there's no way to know it's the same product. And if you want to add the delivery charge, add it as an option, not as another item on the array, because it will just be skipped and lost.

lstables commented 10 years ago

Right ok, i think I understand. What about removing items?

Crinsane commented 10 years ago

Don't see anything wrong with your code. But the same code is working perfectly for me, so I would just debug a bit more:

  1. add an item to the cart
  2. hit the remove route
  3. first dump the content of the cart before removing
  4. dump the rowid that's been passed to through the route
  5. Is this rowid in the cart content before the remove?
  6. do Cart::remove($rowid);
  7. Dump the cart content, is the rowid still there?

Not much more I can do about it really

lstables commented 10 years ago

Yeah the rowid is there, id is just a random number for each customer checkout, but yes I'll toy around with it a bit more and debug further.

Crinsane commented 10 years ago

Any progress been made on debugging this issue? I'd like to close this one if it's fixed, or work out a solution.

kertami commented 10 years ago

I have a similar problem, although on a local dev server, everything works fine, as soon as I put it online, it stops working...

I can still add products to the cart, just can't remove them

Crinsane commented 10 years ago

@kertami Okay, are all other 'session-related' things working okay? Could you give me some code samples to work with while debugging? Because I just can't reproduce this error...

kertami commented 10 years ago

@Crinsane sorry for the late response, but since this is most likely a server configuration issue, I'll try to solve it myself I will inform you if I find it

Crinsane commented 10 years ago

@kertami No problem. Only thing I can think of is some misconfiguration with the session stuff on the server. Please let me know if you've sorted this out!

I'll leave this open for now.

chenzm080 commented 10 years ago

when i add a goods to the cart ,and i want to get it now .how i know the rowId?

Crinsane commented 10 years ago

It's in the Collection you get when doing Cart::content() So on the page where you show you're cart contents and loop over the content, you have access to the rowid.

hexencoded commented 9 years ago

Try this: Cart::destroy();

Then: Cart::add(array('id' => randomNumber(), 'name' => $input['product_name'], 'qty' => 1, 'price' => $price, 'delivery' => $input['delivery']));

Finally: Cart::remove($id);

Crinsane commented 9 years ago

@hexencoded Where did the $id variable come from?

hexencoded commented 9 years ago

In my case: Cart controller - http://pastebin.com/MRZY9sqM

id getting from HTML: <td><a href="#" data-loading-text="Удаление..." data-id="{{{ $row->id }}}" class="cart-remove"><i class="fa fa-trash"></i> Удалить</a></td>

Javascript: $('.cart-remove').on('click', function () { var btn = $(this); btn.button('loading');

    var id = $(this).data('id');

    $.ajax({
        url: "/cart/" + id + "/remove",
        type: 'DELETE',
        success: function(data) {
             $('#cart').text('Корзина (' + data.data.count + ')');
            btn.button('reset');
        }
    });
});

Removing items does not working correctly.

Crinsane commented 9 years ago

@hexencoded

Okay, I see your problem. What you're misunderstanding is the fact that $row->id is not the same as $row->rowId.

The id is just the ID you set yourself when you added the item to the cart. Usually the primary key of the item. The rowId is a unique ID, generated by the cart when you add an item. It's used to identify unique items in the cart. You can have multiple rows in the cart with the same id but their rowId will be different (because they have different options for instance)

What you want to do is in your HTML snippet:

<td>
    <a href="#" data-loading-text="Удаление..." data-id="{{{ $row->rowId }}}" class="cart-remove"><i class="fa fa-trash"></i> Удалить</a>
</td>
hexencoded commented 9 years ago

Thanks! Now this work perfectly.

elbakly commented 8 years ago

Just an update here for those who are in development when you use Cart::destroy(); or Cart::remove(); or any of cart functions you cant follow that by dd(); and expect it to be updated in your session

MicroDreamIT commented 8 years ago

@Crinsane cart remove is not working properly, http://46.101.177.93/cart, in here two plan or two product can not co-exist. i dont know why this is happening....

joshuahadi commented 6 years ago

Hi, I use integer for 'total' column in my database. I put 'total'=>Cart::subtotal() in my controller and what I get in my database for this 'total' column is only 1 digit of it`s front value (for example it should get 500000, but it only get 5). How to overcome this problem? what to change?thanks a lot

bagmorgirish commented 6 years ago

Error:- Class Gloudemans\Shoppingcart\Cart does not exist

I am already added "use cart;" in controller and i am currently use laravel 5.4 version @Crinsane please suggest me something for that

zahidshuvo12 commented 6 years ago

Hi @Crinsane , I use integer for 'total' column in my database. I put 'total'=>Cart::subtotal() in my controller and what I get in my database for this 'total' column is only 1 digit of it`s front value (for example it should get 500000, but it only get 5). How to overcome this problem? what to change?thanks a lot

TumiDebrah commented 6 years ago

Hi @Crinsane, it would be really awesome if you added conditions such as discounts,coupons.sales etc.

inzi-niloy commented 6 years ago

I'm getting this msg : Using version ^2.5 for gloudemans/shoppingcart ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Your requirements could not be resolved to an installable set of packages.

Problem 1

nazmulhaquep commented 5 years ago

@Crinsane I add shipping cost like: $delivery = 30; $count = Cart::count(); if($count==0){ $data['options']['delivery'] = $delivery; } Cart::add($data); When i print_r(Cart::content()); It shows delivery charge but delivery charge don't added with the Cart::total(); How can i add this? Need help!

rahim3070 commented 5 years ago

I have face the same problem. Additional # will be added after row_id for first row of my Gloudemans Shopping cart on update. What can I do ?

My demo url is - http://localhost/magic_shop/cart?_token=De1K9HIxNXk7o9AqdxfeHQJGz2kCx8F0tURtAoFl&qty=1&row_id=f754bf78868fbd1a07e2607314283270#

zakir7dipu commented 4 years ago

Hi. I am facing a problem. I want to use this shopping cart in one restaurant business. But in this case, my product have my extra item, like I want to add cart a pizza but with this pizza I need extra chis, extra chicken ect. Now how to add cart multiple extra item with one product item?

sujilkumarkm commented 3 years ago

${{ Session::get('coupon')['balance'] }}

This is the correct use in blade file , before instead of "Session" I used "Cart" that's why it thrown error, now it's working perfect

Controller code given below for adding coupon.

public function Coupon(Request $request) { $coupon = $request->coupon; $check = DB::table('coupons')->where('coupon',$coupon)->first(); if($check) { Session::put('coupon',[

            'name' => $check->coupon,
            'discount' => $check->discount,
            'balance' => Cart::Subtotal()-$check->discount,

            ]);

            $notification=array(
            'messege'=>'Coupon applied successfully',
            'alert-type'=>'success'
             );
           return Redirect()->back()->with($notification);

    }
    else
    {
        $notification=array(
            'messege'=>'Invalid coupon',
            'alert-type'=>'warning'
             );
           return Redirect()->back()->with($notification);
    }
}

Blade part.

<ul class="list-group col-lg-4" style="float: right;">
                        <li class="list-group-item">Subtotal : <span style="float: right;">${{ Session::get('coupon')['balance'] }}</span></li>
                        <li class="list-group-item">Coupon : <span style="float: right;">525</span></li>
                        <li class="list-group-item">Shipping Charge : <span style="float: right;">525</span></li>
                        <li class="list-group-item">Vat : <span style="float: right;">525</span></li>
                        <li class="list-group-item">Total : <span style="float: right;">525</span></li>

                    </ul>