OpenCart – How the Wish List Works

The wish list is an important part of the Opencart software and is used by just about everyone as a way to offer a nice feature to customers. There is also a wonderful Multiple Wishlist Extension available in the Opencart store if you want to enhance the default. Whatever you case may be, this article will explain how the wish works for those that are interested.

The Wish List Session Array

The wishlist, which is basically a collection of products that the customer has chosen to stow away is comprised of a session array. Every time a customer clicks on ADD TO WISH LIST, the session array is updated. Before the session array can even be updated is must first be created and it is done so when you click the ADD TO WISH LIST BUTTON for the first time.

If you open catalog/controller/account/wishlist.php you can see the following code:

if (!isset($this->session->data['wishlist'])) {
$this->session->data['wishlist'] = array();
}

This can be found in the public function add(). It basically is saying “if no array, then create one”.

Now that the array has been created “obviously it already might be”, then the system will want to add the product_id to it thus created the list or adding TO the list. That code can be found in the same file/’function here:

if ($product_info) {
if (!in_array($this->request->post['product_id'], $this->session->data['wishlist'])) {
$this->session->data['wishlist'][] = $this->request->post['product_id'];
}

Now we have a Session with product_id’s that is in fact our wish list but it has not yet been stored in the database. It’s merely in cyber space at the moment. In order to view your wish list you have to LOGIN. Once you login, some more code fires that compares the session array you started with the current database array “if any” and executes code accordingly.

When you login, the following code will fire, found in: system/library/customer.php

if ($customer_query->row['wishlist'] && is_string($customer_query->row['wishlist'])) {
if (!isset($this->session->data['wishlist'])) {
$this->session->data['wishlist'] = array();
}

$wishlist = unserialize($customer_query->row['wishlist']);

foreach ($wishlist as $product_id) {
if (!in_array($product_id, $this->session->data['wishlist'])) {
$this->session->data['wishlist'][] = $product_id;
}
}
}

In short, this code checks for an existing wishlist array and compares it to the wishlist that is possibly already stored in the CUSTOMER TABLE and if exists……add to it. This allows you to see the actual items in your wish list once you login. When you logout, it updates the customer table again, if need be. Now, if you want to remove an item from the wish list it will just remove it from the array and update the database accordingly.

Looking for quality OpenCart Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , | Posted under OpenCart | RSS 2.0

Author Spotlight

Joe Stenhouse

Joe Stenhouse

I am a web application developer that specializes in PHP, JAVASCRIPT, MYSQL, HTML, and CSS. We manifest exciting potentials within the world wide web through means of innovation.

Leave a Reply

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


8 + 3 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>