wojodesign / simplecart-js

A simple javascript shopping cart that easily integrates with your current website.
simplecartjs.org
1.79k stars 490 forks source link

Check if the product is in the cart on different pages #339

Open RuneVini opened 11 years ago

RuneVini commented 11 years ago

Hello all, I have a product page where it shows a list of products and another page with your details and through these two pages I need to check if the product is already in cart.

But when I add a product to cart on a given page normally it includes, the problem is when I try to add the same product on another page, it is showing up in the cart with duplicate names being sure that the issue was a warning that the item already in the cart and provide only the accretion or reducing the quantity.

I use v3.0 and i try follow instructions in the issue #202 but i no have sucess.

Any help is welcome. Thanks in advance!

dicentiu commented 11 years ago

i think these products aren't exactly the same. if you use different names (in container with class "item-name"), simpleCart will get those as separate products. so if item_name is the same for the products, when you click the product on a different page, the quantity will increase.

brett has provided an answer in the last comment on that issue

simpleCart.bind( 'beforeAdd' , function( item ){ // return false if the item is in the cart, // so it won't be added again return !simpleCart.hasItem( item ); });

RuneVini commented 11 years ago

Thanks for reply.

When I use the "item_name" even being on different pages of the same name it's duplicates the name when access the cart.

I would like to issue a warning when a product already in the cart. But I am not able to make this comparison.

Any tips?

dicentiu commented 11 years ago

what's inside item_name must be the same for both in order to products be the same

it seems that hasItem has been replaced by has

simpleCart.ready(function(){

simpleCart.bind( 'beforeAdd' , function( item ){ // return false if the item is in the cart, // so it won't be added again if ( simpleCart.has(item) ) alert('product already in the cart');

return !simpleCart.has( item ); });

});

also, read this http://simplecartjs.org/documentation/simplecart-find

RuneVini commented 11 years ago

Thanks for replying again.

In the matter of checking if the product is already in the cart worked. Thank you!

But when I try to add the same product to different pages it duplicates the cart anyway.

Practically the same problem of #257 issue

Any tips on that?

dicentiu commented 11 years ago

has() method checks all the properties of that item to verify that has been aded before. so if has() finds a different price or name it will consider different products, which is not always the case.

instead has() you should use find() to search for name of the item (or whatever property you consider that products are the same)

simpleCart.ready(function(){

simpleCart.bind( 'beforeAdd' , function( item ){ // return false if the item is in the cart, // so it won't be added again

var found = simpleCart.find({ name: item.get('name') });

if(found.length){ alert('product already in the cart'); return false;}

return true; });

});

RuneVini commented 11 years ago

Thanks for responding.

I tried using the "item.get (" name ")" but even though the product with the same name, yet doubles the cart.

Do you have any idea how I could get a given database (the id, for example) and stores it in a variable and pass through the pages?

Any tips? Thanks in advance.