Jump to content

How to add "compare" to the product page (not only category)


Recommended Posts

Hello!

 

When I use the category view I can use checkboxes to compare a number of products.

 

I would like to be able to add a product to this comparison from any product page (like a search or product details for example ...). Is there a way to activate this?

 

thx, piku

Link to comment
Share on other sites

I'm not sure comparator variables are assigned to the product page, but you can try with something like this

 

{if isset($comparator_max_item) && $comparator_max_item}
 <p class="compare">
  <input type="checkbox" class="comparator" id="comparator_item_{$product->id}" value="comparator_item_{$product->id}" {if isset($compareProducts) && in_array($product->id, $compareProducts)}checked="checked"{/if} />
  <label for="comparator_item_{$product->id}">{l s='Select to compare'}</label>
 </p>
   {/if}

 

Of course, then you also need to include the compare button in the [age:

{include file="./product-compare.tpl"}

Link to comment
Share on other sites

thx for your answer, I am happy to get help so quickly!

 

this seems quite sophisticated - I am still setting stuff up.

that's why I couldn't test your suggestion yet ... but I will try this and let you know ...

 

 

sry for any delay,

 

piku

Link to comment
Share on other sites

  • 1 year later...

Add this code in "product.tpl" where you want to place the "Add  to Compare button", code is working fine with Prestshop 1.6 version too --and consists of javascript code /ajax function to add a product to comapre.

Use this code and Enjoy B) B) :D :D :D

 

 <!--Add to Compare---->

    <script type=\"text/javascript\">

function addToCompare()
        {
           
            var totalValueNow = parseInt($('.bt_compare').next('.compare_product_count').val());
            var  totalVal;
            if(totalValueNow<3){
            $.ajax({
                url: 'index.php?controller=products-comparison&ajax=1&action=add&id_product=' +     {$product->id|intval},
                async: true,
                cache: false,
                type: 'Post',   
                dataType: 'html'
               
           });
            compareButtonsStatusRefresh();    
            totalVal = totalValueNow +1,
            $('.bt_compare').next('.compare_product_count').val(totalVal),
            totalValue(totalVal);
            $('.bt_compare').attr("disabled",false);
            $('.bt_compare').css("background-color","#E8490C");
            $('.bt_compare').css("color","#fff");
           
           }
           else{
            var $div8 = $(".comapare_max_limit");
            if ($div8.is(":visible")) { return; }
            $div8.show();
            setTimeout(function() {
            $div8.hide();
            }, 4000);
    
            var $div9 = $(".product-added_overlay");
            if ($div9.is(":visible")) { return; }
            $div9.show();
            setTimeout(function() {
            $div9.hide();
            }, 4000);
           }
          
    }      
   
function totalValue(value)
{
    $('.bt_compare').find('.total-compare-val').html(value);
}

 

function compareButtonsStatusRefresh()
 {
     $('.add_to_compare').each(function() {
        if ($.inArray(parseInt($(this).data('id-product')), comparedProductsIds) !== -1)
            $(this).addClass('checked');
        else
            $(this).removeClass('checked');
       });
}
    </script>         
 

 

 

    {if isset($comparator_max_item) && $comparator_max_item}
        
        <a onclick="addToCompare()" href="#" data-id-product="{$product->id|intval}"><span class="product_btn_comparison">Add to Compare</span></a>
       <div class="comapare_max_limit">You cannot add more than 3 product(s) to the product comparison</div>
      
    {/if}
    {include file="./product-compare.tpl"}
    
    <!------>

Link to comment
Share on other sites

  • 5 weeks later...

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...