Correct, 5 files need changes:
1) get rid of the cart merging logic in the construct function in
system/library/cart/cart.php
remove this part:
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 = '" . (int)$this->customer->getId() . "'");
// 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']);
}
}
2) add these functions which do the same but will be called only when needed:
Code: Select all
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);
}
}
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.