-2

I m adding some product to cart but my problem is starting when i log in the site my cart is empty

1 Answers1

0

So the issue is with your session in system/library/cart/cart.php

After you have logged in, the customer class is initiated and the __construct method runs.

on line 22 of the file system/library/cart/cart.php you can see how it works:

// this code queries the current cart of your session id. so before you were logged int, your cart was saved to the database under a session id.
$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()) . "'");

//after it finds your card products, it adds them to your CUSTOMER id.
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']);
}

So the reason for not populating your cart after login is because in that session. It could be that some part of your code deletes the $this->session->getId() so the script can't find any products in cart.

To debug this just print_r the session_id and the results like this

//print out the session id.
print_r($this->session->getId());
$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()) . "'");

//print out the result of the query
print_r($cart_query->rows);
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']);
}

to see that is in there. if it is empty, you will need to dig deeper to see who is deleting it.

if it is populated, visit the phpmyadmin table oc_cart to check to find the records of the session_id=$this->session->getId() and try to find out why it is not returning the cart products.

hope this helps.

Dmitriy Zhuk
  • 757
  • 1
  • 6
  • 9