Post by stoikkas@gmail.com » Thu Sep 26, 2024 8:06 pm

Hi All,

I'd like to change the way the cart works in opencart. Id like a new cart each time a customer logs in. So the same customer could log in from 2 devices at the same time and have a separate cart each. Is this something that is achievable or is the way the cart works to inbedded to be easily changed?
Many thanks

Opencart Version: 3.0.3.9


Posts

Joined
Tue Mar 02, 2021 12:39 am

Post by nonnedelectari » Thu Sep 26, 2024 9:27 pm

stoikkas@gmail.com wrote:
Thu Sep 26, 2024 8:06 pm
Hi All,

I'd like to change the way the cart works in opencart. Id like a new cart each time a customer logs in. So the same customer could log in from 2 devices at the same time and have a separate cart each. Is this something that is achievable or is the way the cart works to inbedded to be easily changed?
Many thanks

Opencart Version: 3.0.3.9
You can even login with the same customer credentials from two or more different browsers on the same device and have seperate carts if you like.

Active Member

Posts

Joined
Thu Mar 04, 2021 6:34 pm

Post by stoikkas@gmail.com » Thu Sep 26, 2024 9:35 pm

nonnedelectari wrote:
Thu Sep 26, 2024 9:27 pm
stoikkas@gmail.com wrote:
Thu Sep 26, 2024 8:06 pm
Hi All,

I'd like to change the way the cart works in opencart. Id like a new cart each time a customer logs in. So the same customer could log in from 2 devices at the same time and have a separate cart each. Is this something that is achievable or is the way the cart works to inbedded to be easily changed?
Many thanks

Opencart Version: 3.0.3.9
You can even login with the same customer credentials from two or more different browsers on the same device and have seperate carts if you like.
That's great! How do I set this up? As at the moment both devices/browsers get the same cart.

Many thanks


Posts

Joined
Tue Mar 02, 2021 12:39 am

Post by nonnedelectari » Thu Sep 26, 2024 10:41 pm

stoikkas@gmail.com wrote:
Thu Sep 26, 2024 9:35 pm
nonnedelectari wrote:
Thu Sep 26, 2024 9:27 pm
stoikkas@gmail.com wrote:
Thu Sep 26, 2024 8:06 pm
Hi All,

I'd like to change the way the cart works in opencart. Id like a new cart each time a customer logs in. So the same customer could log in from 2 devices at the same time and have a separate cart each. Is this something that is achievable or is the way the cart works to inbedded to be easily changed?
Many thanks

Opencart Version: 3.0.3.9
You can even login with the same customer credentials from two or more different browsers on the same device and have seperate carts if you like.
That's great! How do I set this up? As at the moment both devices/browsers get the same cart.

Many thanks
They get the same cart because OC saves customer carts and restores them and merges them upon login (actually it does this on every page load).
It only deletes carts from guests after a certain period as in:
in system/library/cart/cart.php

Code: Select all

		// 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 is fine but merging you have to remove as that is done on customer id and while the session id will be different in different browsers for the same customer, the customer id will be the same..

in system/library/cart/cart.php
comment out:

Code: Select all

	if ($this->customer->getId()) {
			// We want to change the session ID on all the old items in the customers cart
			$this->db->query("UPDATE " . DB_PREFIX . "cart SET session_id = '" . $this->db->escape($this->session->getId()) . "' WHERE api_id = '0' AND customer_id = '" . $this->customer_id . "'");

			// Once the customer is logged in we want to update the customers cart
			$cart_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "cart WHERE api_id = '0' AND customer_id = '0' AND session_id = '" . $this->db->escape($this->session->getId()) . "'");

			foreach ($cart_query->rows as $cart) {
				$this->db->query("DELETE FROM " . DB_PREFIX . "cart WHERE cart_id = '" . (int)$cart['cart_id'] . "'");

				// The advantage of using $this->add is that it will check if the products already exist and increaser the quantity if necessary.
				$this->add($cart['product_id'], $cart['quantity'], json_decode($cart['option']), $cart['recurring_id']);
			}
		}

Active Member

Posts

Joined
Thu Mar 04, 2021 6:34 pm

Post by stoikkas@gmail.com » Thu Sep 26, 2024 11:18 pm

Amazing!! That works a treat! Thank you so much!!


Posts

Joined
Tue Mar 02, 2021 12:39 am
Who is online

Users browsing this forum: No registered users and 6 guests