Categories
Extensions OpenCart Projects

OpenCart Self Host Product Like Button

This is simple product like button module for Opencart 2x. It’s used existing product DB column EAN & JAN for store data. It’s self host like/dislike system.
The module need modified 3 file included Model/Product; Controller/Product and Template/Product.
Opencart Self hots product like button Opencart Product Like Button Module

1. Add these 2 function for Model/Product

+ 1) WHERE product_id = '" . (int)$product_id . "'");
}
public function updateDislike($product_id) {
$this->db->query("UPDATE " . DB_PREFIX . "product SET jan = (jan + 1) WHERE product_id = '" . (int)$product_id . "'");
}

2. Edit Controller/Product
Find line:

if ($product_info) {

Add these before the found line:

} else {
$data['liked'] = false;
}
if (isset($this->session->data['_disliked_'. $product_id])) {
$data['disliked'] = $this->session->data['_disliked_'. $product_id];
} else {
$data['disliked'] = false;
}

Find line:

if ($this->config->get('config_tax')) {

Add these before the found line:

if ($product_info['ean']) {
$data['like'] = $product_info['ean'];
$data['dislike'] = $product_info['jan'];
} else {
$data['like'] = '0';
$data['dislike'] = '0';
}

Find line:

public function getRecurringDescription() {

Add these before the found line:

public function like() {
$json = array();

if ($this->request->server['REQUEST_METHOD'] == 'POST') {
if (empty($this->request->post['product_id'])) {
$json['error'] = 'Cheating?';
}
if (empty($this->request->server['REMOTE_ADDR'])) {
$json['error'] = 'Cheating?';
}
if ($this->request->post['product_id'] !== $this->request->get['product_id']) {
$json['error'] = 'Cheating?';
}

if (!isset($json['error'])) {
if ($this->request->server['REMOTE_ADDR']){
$ip = $this->request->server['REMOTE_ADDR'];
} else {
$ip='true';
}

$like = '_liked_' . $this->request->get['product_id'];
if (!isset($this->session->data[$like])) {
$this->load->model('catalog/product');
$this->model_catalog_product->updateLike($this->request->get['product_id'], $this->request->post);
$this->session->data[$like] = $ip;
}
}
}

$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
public function dislike() {
//$this->load->language('product/product');

$json = array();

if ($this->request->server['REQUEST_METHOD'] == 'POST') {
if (empty($this->request->post['product_id'])) {
$json['error'] = 'Cheating?';
}
if (empty($this->request->server['REMOTE_ADDR'])) {
$json['error'] = 'Cheating?';
}
if ($this->request->post['product_id'] !== $this->request->get['product_id']) {
$json['error'] = 'Cheating?';
}

if (!isset($json['error'])) {
if ($this->request->server['REMOTE_ADDR']){
$ip = $this->request->server['REMOTE_ADDR'];
} else {
$ip='true';
}
$dislike = '_disliked_' . $this->request->get['product_id'];

if (!isset($this->session->data[$dislike])) {
$this->load->model('catalog/product');
$this->model_catalog_product->updateDislike($this->request->get['product_id'], $this->request->post);
$this->session->data[$dislike] = $ip;
}
}
}

$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}

3. for Template/Product
Find these line:

<button type="button" data-toggle="tooltip" class="btn btn-default" title="<?php echo $button_compare; ?>" onclick="compare.add('<?php echo $product_id; ?>');"><i class="fa fa-exchange"></i></button>

Add these button:

<button type="button" id="button-like" data-toggle="tooltip" class="btn btn-default" title="" data-original-title="Like <?php echo $like; ?>"><i class="fa fa-thumbs-up"></i> <span class="like-total"><?php echo $like; ?></span></button>
<button type="button" id="button-dislike" data-toggle="tooltip" class="btn btn-default" title="" data-original-title="Dislike <?php echo $dislike; ?>"><i class="fa fa-thumbs-down"></i> <span class="dislike-total"><?php echo $dislike; ?></span></button>

Finally find line:

<?php echo $footer; ?>


Add these code before found line:

<script type="text/javascript"><!--
$(document).ready(function() {
var liked = localStorage.getItem('_liked_<?php echo $product_id; ?>'), disliked=localStorage.getItem('_disliked_<?php echo $product_id; ?>');
if (liked !== null) {
$('#button-like').html('<i class="fa fa-thumbs-up"></i> <span class="like-total"><?php echo $like; ?></span>').prop('disabled', true);
}
if (disliked !== null) {
$('#button-dislike').html('<i class="fa fa-thumbs-down"></i> <span class="dislike-total"><?php echo $dislike; ?></span>').prop('disabled', true);
}
});

<?php if (!$liked) { ?>
$('#button-like').on('click', function() {
$.ajax({
url: 'index.php?route=product/product/like&product_id=<?php echo $product_id; ?>',
type: 'post',
data: 'product_id=<?php echo $product_id; ?>',
dataType: 'json',
beforeSend: function() {
var id=$('#button-like').attr('aria-describedby');
$('div#'+ id +' > .tooltip-inner').html('Like <?php echo $like + 1; ?>');
$('.like-total').html('<?php echo $like + 1; ?>');
$('#button-like').prop('disabled', true);
},
complete: function() {
$('#button-like').prop('disabled', true);
},
success: function(json) {
$('#button-like').html('<i class="fa fa-thumbs-up"></i> <span class="like-total"><?php echo $like +1; ?></span>');
localStorage.setItem('_liked_<?php echo $product_id; ?>', '<?php echo $_SERVER['REMOTE_ADDR'];?>');
},
error: function(xhr, ajaxOptions, thrownError) {
//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
});
<?php }
if (!$disliked) { ?>
$('#button-dislike').on('click', function() {
$.ajax({
url: 'index.php?route=product/product/dislike&product_id=<?php echo $product_id; ?>',
type: 'post',
data: 'product_id=<?php echo $product_id; ?>',
dataType: 'json',
beforeSend: function() {
var id=$('#button-dislike').attr('aria-describedby');
$('div#'+ id +' > .tooltip-inner').html('Dislike <?php echo $dislike + 1; ?>');
$('.dislike-total').html('<?php echo $dislike + 1; ?>');
$('#button-dislike').prop('disabled', true);
},
complete: function() {
$('#button-dislike').prop('disabled', true);
},
success: function(json) {
$('#button-dislike').html('<i class="fa fa-thumbs-down"></i> <span class="dislike-total"><?php echo $dislike +1; ?></span>');
localStorage.setItem('_disliked_<?php echo $product_id; ?>', '<?php echo $_SERVER['REMOTE_ADDR'];?>');
},
error: function(xhr, ajaxOptions, thrownError) {
//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
});
<?php } ?>
//--></script>

For easy install, can be download OCMode file from here OC 2.x

Please feel free donate a cup of coffee for the module.

Paypal Donate

For live action, please check out Fresh baked cake delivery Malaysia on any product page.