I just want to check if products already added to the wishlist (for logged in customers only)
and put the result on a twig file that if the product already on the wishlist
button color=red
else
button color=gray
and also even get the above results, when the user clicks the 'add to wishlist button' a live result will be given, I mean button color gray
will be changed to red
using JavaScript.
I'm using the latest version 3.0.3.2 open cart version
Try using this code. I have modified this as below. This is working as now for me
Add a Tag on which you want to perform action
<li><a href="javascript:void(0);" class="btn_wishlist_alt" data-product-id=69><i class="fa fa-heart"></i> <span class="">{{ text_wishlist }}</span></a></li>
Now add some js code to your file
<script type="text/javascript">
$(document).ready(function(){
$('.btn_wishlist_alt').on('click', function(){
//alert();
var self = this;
var pID = $(self).attr('data-product-id');
$.ajax({
url: 'index.php?route=product/product/wishlistcheck&product_id=' + encodeURIComponent(pID),
dataType: 'json',
type: 'post',
cache: false,
contentType: false,
processData: false,
success: function(json) {
if(json['in_wishlist']){
//set color red
}else{
//set color blue
}
}
});
});
})
</script>
Now add this function to your controller file
public function wishlistcheck(){
$json = array();
if ($this->request->server['REQUEST_METHOD'] == 'POST') {
$product_id = $this->request->get['product_id'];
if ($this->customer->getId()) {
$this->load->model('account/wishlist');
$wishlist = $this->model_account_wishlist->getWishlist();
if(in_array($product_id, array_column($wishlist, 'product_id'))) {
$json['in_wishlist'] = true;
}else{
$json['in_wishlist'] = false;
}
}
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
Here it is. Thats all.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.