Jump to content

[SOLVED] Using a checkbox for Products comparison in Prestashop 1.6


Recommended Posts

Hi, Prestashop community. I'm trying to get the products comparison to work with a checkbox to select the products. The code I have is from prestashop 1.5:

<input type="checkbox" class="comparator" id="comparator_item_{$product.id_product}" value="comparator_item_{$product.id_product}" {if isset($compareProducts) && in_array($product.id_product, $compareProducts)}checked="checked"{/if} />

 

If I use the "add to compare" button from the standard theme, it works, but I really would like it to work with the checkbox style.

Is there anyway to achieve it?

 

Thanks in advance.

Edited by moy2010 (see edit history)
Link to comment
Share on other sites

You need to open themes/yourtheme/js/product-comparision.js

 

check here:

$(document).ready(function(){
	$(document).on('click', '.add_to_compare', function(e){
		e.preventDefault();
		if (typeof addToCompare != 'undefined')
			addToCompare(parseInt($(this).data('id-product')));
	});

	reloadProductComparison();
	compareButtonsStatusRefresh();
	totalCompareButtons();
});

as you can see adding to comparision is triggered only .on('click'), you need to change this logistic, more information you can find by searching for "how to trigger checkbox change" :)

  • Like 1
Link to comment
Share on other sites

Finally got it working thanks to Krystian. Here's the final code for products-comparison.js:

/*
* 2007-2013 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <[email protected]>
*  @copyright  2007-2013 PrestaShop SA
*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

$('document').ready(function(){
	reloadProductComparison();
});

reloadProductComparison = function() {
	$('a.cmp_remove').click(function(){

  var idProduct = parseInt($(this).data('id-product'));

 	$.ajax({
  			url: 'index.php?controller=products-comparison&ajax=1&action=remove&id_product=' + idProduct,
 			async: false,
 			cache: false,
  			success: function(){
				return true;
			}
		});
	});

	$('input:checkbox.comparator').click(function(){
	
		var idProduct = $(this).attr('value').replace('comparator_item_', '');
		var checkbox = $(this);
		
		if(checkbox.is(':checked'))
{
			$.ajax({
	  			url: 'index.php?controller=products-comparison&ajax=1&action=add&id_product=' + idProduct,
	 			async: true,
	 			cache: false,
	  			success: function(data){
	  				if (data === '0')
	  				{
	  					checkbox.attr('checked', false);
						alert(max_item);
					}
	  			},
	    		error: function(){
	    			checkbox.attr('checked', false);
	    		}
			});	
		}
		else
		{
			$.ajax({
	  			url: 'index.php?controller=products-comparison&ajax=1&action=remove&id_product=' + idProduct,
	 			async: true,
	 			cache: false,
	  			success: function(data){
	  				if (data === '0')
	  					checkbox.attr('checked', true);
	    		},
	    		error: function(){
	    			checkbox.attr('checked', true);
	    		}
			});	
		}
	});
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...