Reading list Switch to dark mode

    How Opencart Retain Cart Data Before And After Login

    Updated 16 July 2021

    Today we will learn about how the Opencart retain the cart data before and after login for the customer.

    When the customer visit to the site and before login to the store if he added the products to the cart and after that he login to the site. Opencart retains the added product before login to the site and also update the cart information according to the cart information before logout by customer last time.

    In the Opencart when the customer visit to the site it sets the customer id as null value and started unique session for the customer. From Opencart 2.1.x.x and it’s above version Opencart manages the cart on the basis of cart table. In the previous version Opencart cart had managed through session.

    Opencart Managing to the cart table:

    When customer added the product to the cart it calls the add method of the cart library. Where it check for the row if any row has same value on the basis of option, recurring_id, session_id, customer_id and product_id. If it finds any row in cart table then it just update the quantity other wise it insert as a new value.

    Searching for an experienced
    Opencart Company ?
    Find out More
    public function add($product_id, $quantity = 1, $option = array(), $recurring_id = 0) {
        //Check for the row which have same product_id, option,recurring_id, session value, customer id and product id
         $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "cart WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "' AND product_id = '" . (int)$product_id . "' AND recurring_id = '" . (int)$recurring_id . "' AND `option` = '" . $this->db->escape(json_encode($option)) . "'");
    
         //If not found any row then it insert it as new row otherwise update the quantity of them
         if (!$query->row['total']) {
               $this->db->query("INSERT " . DB_PREFIX . "cart SET api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "', customer_id = '" . (int)$this->customer->getId() . "', session_id = '" . $this->db->escape($this->session->getId()) . "', product_id = '" . (int)$product_id . "', recurring_id = '" . (int)$recurring_id . "', `option` = '" . $this->db->escape(json_encode($option)) . "', quantity = '" . (int)$quantity . "', date_added = NOW()");
         } else {
               $this->db->query("UPDATE " . DB_PREFIX . "cart SET quantity = (quantity + " . (int)$quantity . ") WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "' AND product_id = '" . (int)$product_id . "' AND recurring_id = '" . (int)$recurring_id . "' AND `option` = '" . $this->db->escape(json_encode($option)) . "'");
         }
    }
    

    In the above code if the customer is not login then the customer id column has value 0.

    After the customer login:

    When the customer login to the site the Opencart process as following:

    if ($this->customer->getId()) {
    
          //Update the session column value of the cart table with current session value for the previous addede products
    	$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() . "'");
    
          // Fetch all the rows which are added from current session
    	$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()) . "'");
    
          // Delete all rows with their cart id and update them again with current customer id and update the quantity if same product already added
    	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']);
    	}
    }

    First, Opencart update the session value of the previous session value in the cart table with the current session value for that customer.

    After that It fetches all the product which are added before the login in current session.

    After fetch the rows it delete those rows and update the rows details again, it done it beacuse this time it update the customer id and update the quantity if the same product is already added in the cart.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • Sagar Kunjadiya
    • Webkul Support
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home