<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NemoPS &#187; compare</title>
	<atom:link href="http://nemops.com/tag/compare/feed/" rel="self" type="application/rss+xml" />
	<link>http://nemops.com</link>
	<description>Prestashop Tutorials, Modules and More!</description>
	<lastBuildDate>Wed, 05 Dec 2018 13:25:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.35</generator>
	<item>
		<title>Adding the Compare button to the Product page</title>
		<link>http://nemops.com/compare-button-to-product-page/</link>
		<comments>http://nemops.com/compare-button-to-product-page/#comments</comments>
		<pubDate>Mon, 12 Aug 2013 10:16:53 +0000</pubDate>
		<dc:creator><![CDATA[Nemo]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[compare]]></category>
		<category><![CDATA[prestashop]]></category>
		<category><![CDATA[products]]></category>

		<guid isPermaLink="false">http://nemops.com/?p=1481</guid>
		<description><![CDATA[<p>In this tutorial, we will see how easy it is to add the compare button to the product page, by only adding a couple of lines to the tpl and using a lightweight override Version used: Prestashop 1.5.4.1 Understanding how the &#8220;Compare&#8221; feature works First of all, we need to understand how the compare feature [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://nemops.com/compare-button-to-product-page/">Adding the Compare button to the Product page</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>In this tutorial, we will see how easy it is to add the compare button to the product page, by only adding a couple of lines to the tpl and using a lightweight override</p>
<p><span id="more-1481"></span></p>
<ul>
<li>Version used: <strong>Prestashop 1.5.4.1</strong></li>
</ul>
<h2>Understanding how the &#8220;Compare&#8221; feature works</h2>
<p>First of all, we need to understand how the compare feature works. Whenever you click on a checkbox to add a product to the comparison table, Prestashop triggers a javascript event, declared in the <strong>reloadProductComparison</strong> function of the file <strong>products-comparison.js</strong>. This file, by default, is added to every page where a product list is displayed. Thus, in addition to the simple blocks of html necessary to display the compare button, we need to consider this as well. We will do it by adding that Javascript file to the header from the <strong>setMedia</strong> method of the <strong>Product Controller</strong>.</p>
<p>Additionally, Every time an item is added or removed from the comparison table, and <strong>ajax</strong> function, which updates the current &#8220;compare list&#8221;, is called, and this very same list is saved into a cookie variable. Prestashop needs to know which items are already in the table, how many they are, and if it is possible to compare products (if the maximum products comparison number is more than 0). Thus, we also need to add a couple of variables to the already mentioned <strong>ProductController.php</strong>, and nothing will better suit the case than an override.</p>
<p>Therefore, our plan of action is:</p>
<ul>
<li>Creating the ProductController.php override</li>
<li>Adding products-comparison.js to the setMedia() method of this new Controller</li>
<li>Extending the initContent method to account for the comparison variables</li>
<li>Embedding the necessary code inside product.tpl</li>
</ul>
<p>&nbsp;</p>
<p>No fear, it&#8217;s easier and quicker than it seems. Let&#8217;s get started!</p>
<div class="separator"></div>
<h2>Overriding ProductController.php</h2>
<p>As always, if you&#8217;re not used to Prestashop Overrides, have a look at the basic <a href="http://doc.prestashop.com/display/PS15/Overriding+default+behaviors" title="Prestashop documentation">Prestashop documentation</a> the Prestateam wrote for us, or my own tutorial on <a href="http://nemops.com/extending-prestashop-objects/" title="How to extend Prestashop Objects">How to extend Prestashop Objects</a>.</p>
<p>Create a new .php file inside <em>override/controllers/front</em> and call it <strong>ProductController</strong>. Write the following snippet inside</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

class ProductController extends ProductControllerCore
{
}
?&gt;
</pre>
<p>Then, let&#8217;s start by embedding <strong>products-comparison.js</strong>. Override the setMedia() method as follows</p>
<pre class="brush: php; title: ; notranslate">
	public function setMedia()
	{
		parent::setMedia();

		if (Configuration::get('PS_COMPARATOR_MAX_ITEM'))
			$this-&gt;addJS(_THEME_JS_DIR_.'products-comparison.js');
	}
</pre>
<p>Remember to call parent::setMedia() so that all needed javascript and css files are included! WHat we are doing here is really straightforward: <strong>if the maximum allowed number of products for the comparison feature is more than zero, then include that Javascript file</strong>.</p>
<p>Then, let&#8217;s extend  <strong>initContent()</strong> as well:</p>
<pre class="brush: php; title: ; notranslate">
	public function initContent()
	{

		$this-&gt;context-&gt;smarty-&gt;assign('comparator_max_item', Configuration::get('PS_COMPARATOR_MAX_ITEM'));
		
		if (isset($this-&gt;context-&gt;cookie-&gt;id_compare))
			$this-&gt;context-&gt;smarty-&gt;assign('compareProducts', CompareProduct::getCompareProducts((int)$this-&gt;context-&gt;cookie-&gt;id_compare));

		parent::initContent();
	}	
</pre>
<p>First, we assign that same comparator item number to the template. Then, if the user added products to the comparison table (so if it has a cookie with product data saved), we assign the list of the added products IDS the template. Of course, we also call the original initContent() <strong>at the end of the overridden method</strong>.</p>
<div class="separator"></div>
<h2>Adding the Compare button and checkbox to the Product page</h2>
<p>So far, so good. Just to be sure our last changes didn&#8217;t break anything, go to the <em>cache/</em> folder and delete <strong>class_index.php</strong>. Refresh the product page and see if any error pops out. If not, read on!</p>
<p>Open up <strong>product.tpl</strong> and locate a suitable place for the compare button. I chose to add it right above the <strong>buy block</strong> at about line 264 of the default template. We can re-use the code from category.tpl and product-list.tpl, with some slight modification. Add the following snippet to the template:</p>
<pre class="brush: php; html-script: true; title: ; notranslate">
		
		{if isset($comparator_max_item) &amp;&amp; $comparator_max_item}
			&lt;p class=&quot;compare&quot;&gt;
				&lt;input type=&quot;checkbox&quot; class=&quot;comparator&quot; id=&quot;comparator_item_{$product-&gt;id}&quot; value=&quot;comparator_item_{$product-&gt;id}&quot; {if isset($compareProducts) &amp;&amp; in_array($product-&gt;id, $compareProducts)}checked=&quot;checked&quot;{/if} /&gt; 
				&lt;label for=&quot;comparator_item_{$product-&gt;id}&quot;&gt;{l s='Select to compare'}&lt;/label&gt;
			&lt;/p&gt;
		{/if}
		{include file=&quot;./product-compare.tpl&quot;}
</pre>
<p>Notice that we are using <strong>$product->id</strong> instead of $product.id_product to refer to the product id.</p>
<p>Save &#038; refresh. Done!!</p>
<p><img src="http://nemops.com/wp-content/uploads/2013/08/iPod-shuffle-Presta154.png" alt="The Prestashop Compare button in a single product page" width="638" height="573" class="alignnone size-full wp-image-1482" />&nbsp;</p>
<div class="separator"></div>
<p>The post <a rel="nofollow" href="http://nemops.com/compare-button-to-product-page/">Adding the Compare button to the Product page</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nemops.com/compare-button-to-product-page/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
