Back to Top

Opencart Wishlist Management

Updated 27 January 2017

Today we will learn about how Opencart manage the wishlist for customer before and after his login. In the Opencart customer can add any number of product in his wishlist.

When customer press the wishlist button for any particular product it sends an ajax request to the controller account/wishlist and process it and add it to the customer wishlist. Lets take a deep look in all these process.

In Opencart at the button of the wishlist it fired the event onclick which call the add method of common.js file which is already loaded in the document during the header load.

//Calls the add method of the wishlist object by passing the product id of common.js file whcih is already loaded with the header
<button type="button" data-toggle="tooltip" class="btn btn-default" title="<?php echo $button_wishlist; ?>" onclick="wishlist.add('<?php echo $product_id; ?>');"><i class="fa fa-heart"></i></button>

In the add method of the common.js file it sends an ajax request to the controller account/wishlist with the product id   and calls the add method of the controller.

var wishlist = {
	'add': function(product_id) {
		$.ajax({
			url: 'index.php?route=account/wishlist/add',
			type: 'post',
			data: 'product_id=' + product_id,
			dataType: 'json',

After getting the product id it at the conroller it first check for product id valid and after that it checks that customer is logged in or not.

Searching for an experienced
Opencart Company ?
Find out More

If customer is login : Opencart directly insert the wishlist data to the database for that customer by deleting that product if  already in wishlist.

if ($this->customer->isLogged()) {
        $this->load->model('account/wishlist');

	// It calls the addwishlist to insert prodcut in the databse for that customer 
	$this->model_account_wishlist->addWishlist($this->request->post['product_id']); //In addWishlist method it first delete if there product already added for that customer in his wishlist then insert it

	$json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . (int)$this->request->post['product_id']), $product_info['name'], $this->url->link('account/wishlist'));

	$json['total'] = sprintf($this->language->get('text_wishlist'), $this->model_account_wishlist->getTotalWishlist());
}

If customer is not login : Opencart preserve it into the session in the wishlist index and send the response with the message to login or create account to save wishlist data.

if (!isset($this->session->data['wishlist'])) { // Check that alredy wishlist index or not and create an array of wishlist to preserve all the wishlist data for this session

	$this->session->data['wishlist'] = array();

}
$this->session->data['wishlist'][] = $this->request->post['product_id'];

$this->session->data['wishlist'] = array_unique($this->session->data['wishlist']); //Remove the duplicate values if any

//Success message to login or create an account to save the wish list data
$json['success'] = sprintf($this->language->get('text_login'), $this->url->link('account/login', '', true), $this->url->link('account/register', '', true), $this->url->link('product/product', 'product_id=' . (int)$this->request->post['product_id']), $product_info['name'], $this->url->link('account/wishlist'));

$json['total'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));

After login to the customer or creating an account it takes all the wishlist index data of the session and add it to the wish list.

// At the account/login and checkout/login controller after login to the customer 
      if (isset($this->session->data['wishlist']) && is_array($this->session->data['wishlist'])) {
	 $this->load->model('account/wishlist');
	  //Add each product to wishlist and unset them from the session
	  foreach ($this->session->data['wishlist'] as $key => $product_id) {
	   $this->model_account_wishlist->addWishlist($product_id);

	    unset($this->session->data['wishlist'][$key]);
	}
}

. . .

Leave a Comment

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


Be the first to comment.

Back to Top

Message Sent!

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

Back to Home