Page 2 of 2

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Tue Aug 04, 2020 6:44 pm
by imcdonald83
Now is see the error of my ways. I have fixed that and the website is good with the updates applied. I will monitor to see if the issue resolves itself.

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Mon Aug 17, 2020 12:58 am
by TheJaspMan
This is a MAJOR problem - as in business traumatizing when you have hundreds of order per day coming and in it happens 1/3 of the time. Enough that our credit card processor reached out in concern. Is there a cleaner fix thread that is clear, direct and locked of other messages?

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Thu Nov 05, 2020 9:24 pm
by AlanARG
letxobnav wrote:
Thu Jun 04, 2020 8:25 am
3) call these functions in register and login controllers
2 function calls for login, giving the current session id to possible old customer items and merging the current guest items
1 function call for register as there cannot be any old customer items so just merging the current guest items

in catalog/controller/checkout/login.php and catalog/controller/account/login.php
just after:

Code: Select all

unset($this->session->data['guest']);
add:

Code: Select all

			// change[662] giving old cart items of the customer the current session id
			$this->cart->renew_session_id_old_items ($this->customer->getId());
			// change[662] assigning the current guest cart items to the customer
			$this->cart->transfer_items_to_new_customer ($this->customer->getId());

in catalog/controller/checkout/register.php and catalog/controller/account/register.php
just after:

Code: Select all

unset($this->session->data['guest']);
add:

Code: Select all

			// change[662] assigning the current guest cart items to the customer
			$this->cart->transfer_items_to_new_customer ($this->customer->getId());

That is all.
Those two are causing an error that doesn't let users to register/login. Website goes straight to some sort of "not found" page but after F5 it works and the user is logged in or in the register process.

Error log shows the following /checkout/login.php:

Code: Select all

[05-Nov-2020 08:58:19 America/Argentina/Buenos_Aires] PHP Fatal error:  Uncaught Error: Call to undefined method Cart\Cart::renew_session_id_old_items() in /home/c1811017/public_html/catalog/controller/checkout/login.php:64
Stack trace:
#0 /home/c1811017/public_html/storage/modification/system/engine/action.php(79): ControllerCheckoutLogin->save()
#1 /home/c1811017/public_html/catalog/controller/startup/router.php(25): Action->execute(Object(Registry))
#2 /home/c1811017/public_html/storage/modification/system/engine/action.php(79): ControllerStartupRouter->index()
#3 /home/c1811017/public_html/system/engine/router.php(67): Action->execute(Object(Registry))
#4 /home/c1811017/public_html/system/engine/router.php(56): Router->execute(Object(Action))
#5 /home/c1811017/public_html/system/framework.php(165): Router->dispatch(Object(Action), Object(Action))
#6 /home/c1811017/public_html/system/startup.php(104): require_once('/home/c1811017/...')
#7 /home/c1811017/public_html/index.php(26): start('catalog')
#8 {main}
  thrown in /home/c1811017/public_html/catalog/controller/checkout/login.php on line 64
/account/login.php:

Code: Select all

[04-Nov-2020 18:59:41 America/Argentina/Buenos_Aires] PHP Fatal error:  Uncaught Error: Call to undefined method Cart\Cart::renew_session_id_old_items() in /home/c1811017/public_html/catalog/controller/account/login.php:56
Stack trace:
#0 /home/c1811017/public_html/storage/modification/system/engine/action.php(79): ControllerAccountLogin->index()
#1 /home/c1811017/public_html/catalog/controller/startup/router.php(25): Action->execute(Object(Registry))
#2 /home/c1811017/public_html/storage/modification/system/engine/action.php(79): ControllerStartupRouter->index()
#3 /home/c1811017/public_html/system/engine/router.php(67): Action->execute(Object(Registry))
#4 /home/c1811017/public_html/system/engine/router.php(56): Router->execute(Object(Action))
#5 /home/c1811017/public_html/system/framework.php(165): Router->dispatch(Object(Action), Object(Action))
#6 /home/c1811017/public_html/system/startup.php(104): require_once('/home/c1811017/...')
#7 /home/c1811017/public_html/index.php(26): start('catalog')
#8 {main}
  thrown in /home/c1811017/public_html/catalog/controller/account/login.php on line 56
checkout and account/register.php:

Code: Select all

[04-Nov-2020 11:51:39 America/Argentina/Buenos_Aires] PHP Fatal error:  Uncaught Error: Call to undefined method Cart\Cart::transfer_items_to_new_customer() in /home/c1811017/public_html/catalog/controller/account/register.php:31
Stack trace:
#0 /home/c1811017/public_html/storage/modification/system/engine/action.php(79): ControllerAccountRegister->index()
#1 /home/c1811017/public_html/catalog/controller/startup/router.php(25): Action->execute(Object(Registry))
#2 /home/c1811017/public_html/storage/modification/system/engine/action.php(79): ControllerStartupRouter->index()
#3 /home/c1811017/public_html/system/engine/router.php(67): Action->execute(Object(Registry))
#4 /home/c1811017/public_html/system/engine/router.php(56): Router->execute(Object(Action))
#5 /home/c1811017/public_html/system/framework.php(165): Router->dispatch(Object(Action), Object(Action))
#6 /home/c1811017/public_html/system/startup.php(104): require_once('/home/c1811017/...')
#7 /home/c1811017/public_html/index.php(26): start('catalog')
#8 {main}
  thrown in /home/c1811017/public_html/catalog/controller/account/register.php on line 31
[04-Nov-2020 13:40:59 America/Argentina/Buenos_Aires] PHP Fatal error:  Uncaught Error: Call to undefined method Cart\Cart::transfer_items_to_new_customer() in /home/c1811017/public_html/catalog/controller/checkout/register.php:240
Stack trace:
#0 /home/c1811017/public_html/storage/modification/system/engine/action.php(79): ControllerCheckoutRegister->save()
#1 /home/c1811017/public_html/catalog/controller/startup/router.php(25): Action->execute(Object(Registry))
#2 /home/c1811017/public_html/storage/modification/system/engine/action.php(79): ControllerStartupRouter->index()
#3 /home/c1811017/public_html/system/engine/router.php(67): Action->execute(Object(Registry))
#4 /home/c1811017/public_html/system/engine/router.php(56): Router->execute(Object(Action))
#5 /home/c1811017/public_html/system/framework.php(165): Router->dispatch(Object(Action), Object(Action))
#6 /home/c1811017/public_html/system/startup.php(104): require_once('/home/c1811017/...')
#7 /home/c1811017/public_html/index.php(26): start('catalog')
#8 {main}
  thrown in /home/c1811017/public_html/catalog/controller/checkout/register.php on line 240
This is how it looks checkout/register.php file after the modifications (line 240):

Code: Select all

unset($this->session->data['guest']);
			// change[662] assigning the current guest cart items to the customer
			$this->cart->transfer_items_to_new_customer ($this->customer->getId());
account/register.php (line 31):

Code: Select all

unset($this->session->data['guest']);
			// change[662] assigning the current guest cart items to the customer
			$this->cart->transfer_items_to_new_customer ($this->customer->getId());
account/login.php (line 56):

Code: Select all

unset($this->session->data['guest']);
						// change[662] giving old cart items of the customer the current session id
						$this->cart->renew_session_id_old_items ($this->customer->getId());
						// change[662] assigning the current guest cart items to the customer
						$this->cart->transfer_items_to_new_customer ($this->customer->getId());
checkout/login.php (line 64):

Code: Select all

unset($this->session->data['guest']);
						// change[662] giving old cart items of the customer the current session id
						$this->cart->renew_session_id_old_items ($this->customer->getId());
						// change[662] assigning the current guest cart items to the customer
						$this->cart->transfer_items_to_new_customer ($this->customer->getId());
I honestly don't know what I did wrong since it doesn't seem too complicated, it was just a copy-paste adding a few small lines of code where you told us. I hope you can help me :)

Thanks!

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Sat Nov 07, 2020 9:28 am
by sw!tch
AlanARG wrote:
Thu Nov 05, 2020 9:24 pm
I honestly don't know what I did wrong since it doesn't seem too complicated, it was just a copy-paste adding a few small lines of code where you told us. I hope you can help me :)

Thanks!
You missed step 1 & 2 ..

Note the cart class changes...

viewtopic.php?f=201&t=211273#p789630

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Wed Nov 18, 2020 10:12 am
by AlanARG
sw!tch wrote:
Sat Nov 07, 2020 9:28 am
You missed step 1 & 2 ..

Note the cart class changes...

viewtopic.php?f=201&t=211273#p789630
I'm having the same issue as the guy that posted while ago, a HTTP 500 ERROR.

I've placed it after "private $data" like you said and it's inside the "__construct"

This is the final result with the modifications you said:

Code: Select all

<?php
namespace Cart;
class Cart {
	private $data = array();

	public function __construct($registry) {
		$this->config = $registry->get('config');
		$this->customer = $registry->get('customer');
		$this->session = $registry->get('session');
		$this->db = $registry->get('db');
		$this->tax = $registry->get('tax');
		$this->weight = $registry->get('weight');
		
		

		// Remove all the expired carts with no customer ID
		$this->db->query("DELETE FROM " . DB_PREFIX . "cart WHERE (api_id > '0' OR customer_id = '0') AND date_added < DATE_SUB(NOW(), INTERVAL 1 HOUR)");
		
		public function renew_session_id_old_items ($customer_id) {
		// We want to change the session ID on all the old items in the customers cart
		$sql = "UPDATE " . DB_PREFIX . "cart SET session_id = '" . $this->db->escape($this->session->getId()) . "' WHERE api_id = '0' AND customer_id = '" . (int)$customer_id . "'";
		$this->db->query($sql);
	}
	
	public function transfer_items_to_new_customer ($customer_id) {
		// Once the customer is logged in we want to merge the current guest items with the customer cart and empty the guest cart
		$sql = "SELECT * FROM " . DB_PREFIX . "cart WHERE api_id = '0' AND customer_id = '0' AND session_id = '" . $this->db->escape($this->session->getId()) . "'";
		$cart_save_query = $this->db->query($sql);
		foreach ($cart_save_query->rows as $cart) {
			$this->add($cart['product_id'], $cart['quantity'], json_decode($cart['option']), $cart['recurring_id']);
			$sql = "DELETE FROM " . DB_PREFIX . "cart WHERE cart_id = '" . (int)$cart['cart_id'] . "'";
			$this->db->query($sql);
		}
	}
		
	}
Thanks a lot man!

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Wed Nov 18, 2020 10:48 am
by sw!tch
AlanARG wrote:
Wed Nov 18, 2020 10:12 am
I'm having the same issue as the guy that posted while ago, a HTTP 500 ERROR.
500 error can be caused by a lot of things. Whats the full error being reported in your server error log?

Maybe PM the member who wrote it for support, clearly the errors being posted are due to improper implementation.

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Wed Nov 18, 2020 11:26 am
by AlanARG
sw!tch wrote:
Wed Nov 18, 2020 10:48 am
AlanARG wrote:
Wed Nov 18, 2020 10:12 am
I'm having the same issue as the guy that posted while ago, a HTTP 500 ERROR.
500 error can be caused by a lot of things. Whats the full error being reported in your server error log?

Maybe PM the member who wrote it for support, clearly the errors being posted are due to improper implementation.
PHP Parse error: syntax error, unexpected 'public' (T_PUBLIC) in /home/c1811017/public_html/system/library/cart/cart.php on line 17
PHP Parse error: syntax error, unexpected 'public' (T_PUBLIC) in /home/c1811017/public_html/system/library/cart/cart.php on line 19

And there's other errors on login.php and register.php (both catalog/controller/checkout and account).

I've tried to send a PM but I think he's profile is blocked. Maybe I can quote him here? @letxobnav
letxobnav wrote:
Sun Apr 21, 2019 11:20 am
a
Any ideas on what's wrong? :)

Thanks for any help!

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Wed Nov 18, 2020 12:05 pm
by sw!tch
AlanARG wrote:
Wed Nov 18, 2020 11:26 am
Any ideas on what's wrong? :)
Yes, the two functions you added are incorrectly implemented.

Code: Select all

public function transfer_items_to_new_customer ($customer_id) { ..}
public function renew_session_id_old_items ($customer_id) { ..}
Your formatting is messed up, these should go into the class not the __construct method.

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Wed Nov 18, 2020 11:09 pm
by AlanARG
sw!tch wrote:
Wed Nov 18, 2020 12:05 pm
AlanARG wrote:
Wed Nov 18, 2020 11:26 am
Any ideas on what's wrong? :)
Yes, the two functions you added are incorrectly implemented.

Code: Select all

public function transfer_items_to_new_customer ($customer_id) { ..}
public function renew_session_id_old_items ($customer_id) { ..}
Your formatting is messed up, these should go into the class not the __construct method.
Like this?

Code: Select all

<?php
namespace Cart;
class Cart {
	private $data = array();
	
	public function renew_session_id_old_items ($customer_id) {
		// We want to change the session ID on all the old items in the customers cart
		$sql = "UPDATE " . DB_PREFIX . "cart SET session_id = '" . $this->db->escape($this->session->getId()) . "' WHERE api_id = '0' AND customer_id = '" . (int)$customer_id . "'";
		$this->db->query($sql);
	}
	
	public function transfer_items_to_new_customer ($customer_id) {
		// Once the customer is logged in we want to merge the current guest items with the customer cart and empty the guest cart
		$sql = "SELECT * FROM " . DB_PREFIX . "cart WHERE api_id = '0' AND customer_id = '0' AND session_id = '" . $this->db->escape($this->session->getId()) . "'";
		$cart_save_query = $this->db->query($sql);
		foreach ($cart_save_query->rows as $cart) {
			$this->add($cart['product_id'], $cart['quantity'], json_decode($cart['option']), $cart['recurring_id']);
			$sql = "DELETE FROM " . DB_PREFIX . "cart WHERE cart_id = '" . (int)$cart['cart_id'] . "'";
			$this->db->query($sql);
		}
	}

	public function __construct($registry) {
		$this->config = $registry->get('config');
		$this->customer = $registry->get('customer');
		$this->session = $registry->get('session');
		$this->db = $registry->get('db');
		$this->tax = $registry->get('tax');
		$this->weight = $registry->get('weight');
		
		

		// Remove all the expired carts with no customer ID
		$this->db->query("DELETE FROM " . DB_PREFIX . "cart WHERE (api_id > '0' OR customer_id = '0') AND date_added < DATE_SUB(NOW(), INTERVAL 1 HOUR)");		
	}
This certainly confused me before that's why I placed it there.
letxobnav wrote:
Tue Aug 04, 2020 5:06 pm
no, you have put both functions inside the __construct function.

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Thu Nov 19, 2020 4:16 am
by sw!tch
AlanARG wrote:
Wed Nov 18, 2020 11:09 pm
Like this?
Yes that should work. If you're going for proper class formatting __construct should be first then the two other functions.

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Thu Nov 19, 2020 6:50 am
by AlanARG
sw!tch wrote:
Thu Nov 19, 2020 4:16 am
AlanARG wrote:
Wed Nov 18, 2020 11:09 pm
Like this?
Yes that should work. If you're going for proper class formatting __construct should be first then the two other functions.
That worked! Thanks a lot man! :)

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Mon Dec 14, 2020 11:50 pm
by raph7
letxobnav wrote:
Wed Jul 24, 2019 8:03 pm
this one is better, previous one had to many deletes ( delete the saved entries all at once and also in the loop).

Code: Select all

public function transfer_items_to_new_customer ($customer_id) {
		// Once the customer is logged in we want to update the customers cart
		$sql = "SELECT * FROM " . DB_PREFIX . "cart WHERE api_id = '0' AND customer_id = '0' AND session_id = '" . $this->db->escape($this->session->getId()) . "'";
		$cart_save_query = $this->db->query($sql);
		foreach ($cart_save_query->rows as $cart) {
			$sql = "DELETE FROM " . DB_PREFIX . "cart WHERE cart_id = '" . (int)$cart['cart_id'] . "'";
			$this->db->query($sql);
			$this->add($cart['product_id'], $cart['quantity'], json_decode($cart['option']), $cart['recurring_id']);
		}
	}
I use the separate delete query in the loop so I can better trace it (is what is deleted also added) with an error_log statement if needed.
I did all the steps mentioned on this topic.. But it is still duplicating for me... Any idea what else I could do? :(

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Tue Dec 15, 2020 12:47 am
by ADD Creative
raph7 wrote:
Mon Dec 14, 2020 11:50 pm
I did all the steps mentioned on this topic.. But it is still duplicating for me... Any idea what else I could do? :(
Have you refreshed your modifications and checked that the changes made are applied any files that exist in storage/modification?

You could also try a slightly different version of the workaround.
https://github.com/ADDCreative/opencart ... 7b357a7ce2

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Tue Dec 15, 2020 4:36 pm
by raph7
ADD Creative wrote:
Tue Dec 15, 2020 12:47 am
raph7 wrote:
Mon Dec 14, 2020 11:50 pm
I did all the steps mentioned on this topic.. But it is still duplicating for me... Any idea what else I could do? :(
Have you refreshed your modifications and checked that the changes made are applied any files that exist in storage/modification?

You could also try a slightly different version of the workaround.
https://github.com/ADDCreative/opencart ... 7b357a7ce2


Do you mean to go into this path /system/storage/modification... and look for the following? If not, ignore what I put there untill --end--

under the paths:
/system/storage/modification/catalog/controller/account/login.php
/system/storage/modification/catalog/controller/account/register.php

- I do have the files login and register but both dont have any of the code that I added in the other files as mentioned in the post before...

under the path:
/system/storage/modification/catalog/controller/checkout
I only have these files: https://prnt.sc/w306aq
That means I dont have login.php and register.php

and under the path:
/system/storage/modification/system
I dont have a folder nor file called cart...

--end--

Another question is...
Should I add this 'workaround' https://github.com/ADDCreative/opencart ... 7b357a7ce2
that you gave me with those changes I already did or should I go back to how it was and then do this work around?

Thank you

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Tue Dec 15, 2020 7:30 pm
by ADD Creative
raph7 wrote:
Tue Dec 15, 2020 4:36 pm
Do you mean to go into this path /system/storage/modification... and look for the following? If not, ignore what I put there untill --end--

under the paths:
/system/storage/modification/catalog/controller/account/login.php
/system/storage/modification/catalog/controller/account/register.php

- I do have the files login and register but both dont have any of the code that I added in the other files as mentioned in the post before...

under the path:
/system/storage/modification/catalog/controller/checkout
I only have these files: https://prnt.sc/w306aq
That means I dont have login.php and register.php

and under the path:
/system/storage/modification/system
I dont have a folder nor file called cart...

--end--

Another question is...
Should I add this 'workaround' https://github.com/ADDCreative/opencart ... 7b357a7ce2
that you gave me with those changes I already did or should I go back to how it was and then do this work around?

Thank you
If you don't have any extension that modify those files then you won't find them in storage/modification. If the two files in storage/modification/catalog/controller/account don't have your changes, try refreshing your modifications. In admin go to Extension -> Modifications, then click the blue refresh button in the top right corner.

If you wanted to try the other workaround then you would need to undo the changes you have already made first.

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Tue Dec 15, 2020 8:21 pm
by raph7
ADD Creative wrote:
Tue Dec 15, 2020 7:30 pm
raph7 wrote:
Tue Dec 15, 2020 4:36 pm
Do you mean to go into this path /system/storage/modification... and look for the following? If not, ignore what I put there untill --end--

under the paths:
/system/storage/modification/catalog/controller/account/login.php
/system/storage/modification/catalog/controller/account/register.php

- I do have the files login and register but both dont have any of the code that I added in the other files as mentioned in the post before...

under the path:
/system/storage/modification/catalog/controller/checkout
I only have these files: https://prnt.sc/w306aq
That means I dont have login.php and register.php

and under the path:
/system/storage/modification/system
I dont have a folder nor file called cart...

--end--

Another question is...
Should I add this 'workaround' https://github.com/ADDCreative/opencart ... 7b357a7ce2
that you gave me with those changes I already did or should I go back to how it was and then do this work around?

Thank you
If you don't have any extension that modify those files then you won't find them in storage/modification. If the two files in storage/modification/catalog/controller/account don't have your changes, try refreshing your modifications. In admin go to Extension -> Modifications, then click the blue refresh button in the top right corner.

If you wanted to try the other workaround then you would need to undo the changes you have already made first.
Yeah I just realised I was looking into a different storage... (the one inside my website files and not the one outside)... I found the code there in the 2 files.. What should I do now?

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Tue Dec 15, 2020 9:00 pm
by ADD Creative
raph7 wrote:
Tue Dec 15, 2020 8:21 pm
Yeah I just realised I was looking into a different storage... (the one inside my website files and not the one outside)... I found the code there in the 2 files.. What should I do now?
If you are sure you have made all the changes and that any files that exist in storage/modification also have the changes, then either there is some sort of mistake with the changes you have made or your issue is caused by something else.

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Mon Sep 04, 2023 7:01 pm
by soydemadrid
Hi, could anyone please help with the full steps on how to fix this problem?

I have tried this fix: https://github.com/ADDCreative/opencart ... 7b357a7ce2

and it doesn't seem to work - items are still duplicated in the cart at checkout.

I'm getting a lot of customer complaints each day that when they add items to the cart, then go to checkout more items are duplicated and added in their cart without them knowing. The customer then pays and ends up having to open Return Requests.

Any help to fix this would be really appreciated. I added a comment to the bug report too.

Re: Cart Items Duplicated When 15+ Items in Cart and Choose Register During Checkout (Multiple Ajax Calls)

Posted: Tue Sep 05, 2023 5:33 am
by ADD Creative
soydemadrid wrote:
Mon Sep 04, 2023 7:01 pm
Hi, could anyone please help with the full steps on how to fix this problem?

I have tried this fix: https://github.com/ADDCreative/opencart ... 7b357a7ce2

and it doesn't seem to work - items are still duplicated in the cart at checkout.

I'm getting a lot of customer complaints each day that when they add items to the cart, then go to checkout more items are duplicated and added in their cart without them knowing. The customer then pays and ends up having to open Return Requests.

Any help to fix this would be really appreciated. I added a comment to the bug report too.
From the comment added to the bug report it sounds like a different issue, as this issue only affect customers who register at checkout. If that is not the case for you I would start a new topic and post more details, such as your OpenCart version, PHP version, theme used, extensions used and the exact steps to recreate the issue.