Page 1 of 1
Prevent adding same item twice in basket
Posted: Fri Aug 16, 2019 4:33 pm
by fredJ
OC 3.0.2.0. I have some modules but it shouldn't matter.
In the last few days, two customers made orders that included the same item twice.
I think it might be that they added the item first without being logged in, but I don't know.
In the second case, the item now has a stock value of -1. This should not be possible.
Can I prevent users from adding the exact same item twice? (they can add the items if one has extra selections). In any case it should NEVER be possible to make order that makes the stock value lower than the minimum value.
Is there some simple line to add in the code?
Re: Prevent adding same item twice in basket
Posted: Fri Aug 16, 2019 6:12 pm
by paulfeakins
In /catalog/controller/checkout/cart.php there is a function called add(). You could add some logic here to not add products that are already added. We'd be happy to help if you're not a developer.
Re: Prevent adding same item twice in basket
Posted: Fri Aug 16, 2019 6:49 pm
by thekrotek
I can't even imagine, how can one have 2 same items in the cart. When you add the same item, its quantity is simply increases. This is a standard behavior for all webshops.
Re: Prevent adding same item twice in basket
Posted: Fri Aug 16, 2019 6:50 pm
by fredJ
Thank you.
I'm thinking it might be best to have the logic in the checkout rather than in the cart.
I found there is already some logic:
controller/checkout.php
Code: Select all
// Validate minimum quantity requirements.
$products = $this->cart->getProducts();
foreach ($products as $product) {
$product_total = 0;
foreach ($products as $product_2) {
if ($product_2['product_id'] == $product['product_id']) {
$product_total += $product_2['quantity'];
}
}
if ($product['minimum'] > $product_total) {
$this->response->redirect($this->url->link('checkout/cart'));
}
}
but what does this do?
$product['minimum'] is 1, I assume. It is the standard.
In the case of a custumer ordering the same item twice, $product_total will be 2. Otherwise it will be 1. How can $product['minimum'] be higher than this value?
Isn't there any validation somewhere to prevent a customer from buying more of an item than there is stock?
Re: Prevent adding same item twice in basket
Posted: Fri Aug 16, 2019 6:53 pm
by fredJ
thekrotek wrote: ↑Fri Aug 16, 2019 6:49 pm
I can't even imagine, how can one have 2 same items in the cart. When you add the same item, its quantity is simply increases. This is a standard behavior for all webshops.
I don't know how it is possible either but it has happened in my shop several times now.
I can't recreate it either. I don't know what the costumers do.
Re: Prevent adding same item twice in basket
Posted: Fri Aug 16, 2019 7:20 pm
by letxobnav
It has to do with the way OC checks stock levels in the cart.
It does this on product per cart line item instead of on product cart-wide.
Example of how this goes with "optional" options:
So if I have 2 in stock of item A and item A has 2 options, I can add item A 3 times to the cart.
1) Item A qty=1 without any option
2) Item A qty=1 with option 1
3) Item A qty=1 with option 2
This is fine as they should be separate cart line items with their own pricing but it is not fine how OC handles the stock level check.
OC checks each line item product separately against the stock level and each is ok because each line item quantity of item A is below stock level of 2.
So no warning about not having enough stock, I can just checkout 3 items A when I only have 2, I could even make one line item qty=2 and I am still good.
So you need to alter the way OC checks the stocklevels and make sure it takes all cart line items with the same product into account.
I personally have changed the cart class: system/library/cart/cart.php
Code: Select all
// Stock
if (!$product_query->row['quantity'] || ($product_query->row['quantity'] < $cart['quantity'])) {
$stock = false;
}
into:
Code: Select all
// Stock
// Change[CARTCLASS[28] check all cart items and add qty of same product
$ptotal = 0;
foreach ($cart_query->rows as $cart_3) {
if ($cart_3['product_id'] == $cart['product_id']) $ptotal = $ptotal + $cart_3['quantity'];
}
// Change[CARTCLASS[28] check against total of product in cart regardless of line items
if (!$product_query->row['quantity'] || ($product_query->row['quantity'] < $cart['quantity']) || ($product_query->row['quantity'] < $ptotal)) {
$stock = false;
}
Not sure if this is also valid for "mandatory" options, the required kind, as I have not checked those as I do not have those, better check yourself.
Re: Prevent adding same item twice in basket
Posted: Fri Aug 16, 2019 7:23 pm
by thekrotek
fredJ wrote: ↑Fri Aug 16, 2019 6:53 pm
I don't know how it is possible either but it has happened in my shop several times now.
I can't recreate it either. I don't know what the costumers do.
If you had only a couple cases like this, I suggest you to ignore. Just disable checkout for out-of-stock items in store settings and be happy.
Re: Prevent adding same item twice in basket
Posted: Fri Aug 16, 2019 7:50 pm
by fredJ
Thank you I will try this and see what customers say.
It won't prevent users from adding the same item twice bug, but at least it won't be possible to add items out of stock, I hope.
Re: Prevent adding same item twice in basket
Posted: Fri Aug 16, 2019 7:53 pm
by fredJ
thekrotek wrote: ↑Fri Aug 16, 2019 7:23 pm
fredJ wrote: ↑Fri Aug 16, 2019 6:53 pm
I don't know how it is possible either but it has happened in my shop several times now.
I can't recreate it either. I don't know what the costumers do.
If you had only a couple cases like this, I suggest you to ignore. Just disable checkout for out-of-stock items in store settings and be happy.
It is already disabled. Customers can't put out-of-stock items in the basket and check out, but they can do like this
Item in stock: 1
Add to basket: 1 pc.
Add by mistake another 1pc of same item.
They can't add 2pc. See also the post above.
Adding to basket is done line-by-line, so line 2 doesn't check if line 1 contains the same item. There is also no check done during checkout.
Re: Prevent adding same item twice in basket
Posted: Fri Aug 16, 2019 8:00 pm
by letxobnav
Adding the same item multiple times is not a bug as long as they have different options, then it is a feature.
adding the "very same item" should not happen but it has been reported before.
The logic for merging an existing "guest" cart with an existing "customer" cart when a customer sign's in is at best flaky and is the most likely cause.
There is a solution for that, I posted it not so long ago, search for it.
Re: Prevent adding same item twice in basket
Posted: Fri Aug 16, 2019 8:05 pm
by letxobnav
Re: Prevent adding same item twice in basket
Posted: Fri Aug 16, 2019 8:46 pm
by fredJ
That's great!
Which of the posts exactly should I implement?
Do I need to edit all those files? I would prefer something simple.
Also, is this verified to work if a customer adds 1 item ABC with extra selection ("blue") and 1 item ABC with extra selection "red"? Of course this must be possible. What should not be possible is to add item ABC +"blue" and item ABC +"red".
Re: Prevent adding same item twice in basket
Posted: Fri Aug 16, 2019 9:29 pm
by letxobnav
customers can and should be able to always add item ABC +"blue" and item ABC +"red" as those are separate line items.
The first post gives a warning and prevents checkout of these if you do not have 2 or more of item ABC.
The second post prevents the possible addition of multiple ABC or multiple ABC + "red" or multiple ABC + "blue" cart line items when a recorded customer cart is merged with his/her guest cart and it prevents the execution of that merging on every page request of a signed-in customer.
so you can still have a cart like:
item ABC qty=x
item ABC qty=y and option "red"
item ABC qty=z and option "blue"
but checkout only if x+y+z <= stock of ABC
you cannot have a cart like:
item ABC qty=a
item ABC qty=b
item ABC qty=c and option "red"
item ABC qty=d and option "red"
etc.
the first post would also prevent checkout if a+b+c+d > stock of ABC but it kind of looks silly and should not happen.
Re: Prevent adding same item twice in basket
Posted: Fri Dec 13, 2019 6:12 pm
by fredJ
Nope, doesn't work.
I still have customers placing double orders, it happens at least once a month.
And there is no alternatives.

I mean seriously, what is this shit? This is from the last month alone.
If I had known that OpenCart is such a buggy mess I would have paid a bit more and used something else. This gives a very unprofessional impression to customers.
One customer told me he added some items to the cart and then switched language, and the cart became empty so he added them again.
That's not supposed to happen !!!
Re: Prevent adding same item twice in basket
Posted: Fri Dec 13, 2019 6:22 pm
by thekrotek
You might want to ask a developer to look into this issue personally, if none of the above solutions helped.
Re: Prevent adding same item twice in basket
Posted: Fri Dec 13, 2019 11:09 pm
by letxobnav