<?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; product rating</title>
	<atom:link href="http://nemops.com/tag/product-rating/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>Display Product rating in the products list in Prestashop</title>
		<link>http://nemops.com/prestashop-product-rating-product-list/</link>
		<comments>http://nemops.com/prestashop-product-rating-product-list/#comments</comments>
		<pubDate>Wed, 17 Apr 2013 13:18:13 +0000</pubDate>
		<dc:creator><![CDATA[Nemo]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[prestashop]]></category>
		<category><![CDATA[product rating]]></category>

		<guid isPermaLink="false">http://nemops.com/?p=1214</guid>
		<description><![CDATA[<p>Regardless of what we sell, it&#8217;s always a good idea to showcase how much our products are loved by our customers. Prestashop comes with a review module, but the rating only displays in the product page. Let&#8217;s see how to show the product rating in the products list too! Version used: Prestashop 1.5.4 Product Comments [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://nemops.com/prestashop-product-rating-product-list/">Display Product rating in the products list in Prestashop</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Regardless of what we sell, it&#8217;s always a good idea to showcase how much our products are loved by our customers. Prestashop comes with a review module, but the rating only displays in the product page. Let&#8217;s see how to show the product rating in the products list too!</p>
<p><span id="more-1214"></span></p>
<ul>
<li>Version used: <strong>Prestashop 1.5.4</strong></li>
<li>Product Comments Module: <strong> version 2.3</strong></li>
</ul>
<h2>Introduction</h2>
<p>It&#8217;s vital for every shop to let customers know about the experience of people who previously bought there. It&#8217;s the essence of e-commerce, and people tend to trust more a shop whose customers review those products they bought, If they&#8217;re happy with them. Thus, it&#8217;s becoming more and more important to have some kind of review system.<br />
Prestashop comes with the so-known &#8220;Prestashop Product comments&#8221; module, which adds a basic review system to our store. The problem with this is that it&#8217;s only displaying reviews (and, subsequently, ratings) in the product detail page. In this tutorial, we will modify the existing module to allow it to hook to product listings, thus enabling each page displaying products to also have those nice stars displayed. Here is the result we want to achieve:</p>
<p><a href="http://nemops.com/wp-content/uploads/2013/04/end_result.png"><img src="http://nemops.com/wp-content/uploads/2013/04/end_result.png" alt="Prestashop product rating in Products List - End result" width="550" height="883" class="aligncenter size-full wp-image-1222" /></a></p>
<div class="separator"></div>
<h2>Step 1 &#8211; Adding a new hook to the Product Rating module</h2>
<p>In order to display those cute stars in the product list, we need a new hook. This hook will be placed right below the product name in the list, but to begin with, let&#8217;s open up <strong>modules/productcomments/productcomments.php</strong>, and find the <strong>install()</strong> method.</p>
<pre class="brush: php; title: ; notranslate">
	public function install()
	{
		if (!file_exists(dirname(__FILE__).'/'.self::INSTALL_SQL_FILE))
			return false;
		else if (!$sql = file_get_contents(dirname(__FILE__).'/'.self::INSTALL_SQL_FILE))
			return false;
		$sql = str_replace(array('PREFIX_', 'ENGINE_TYPE'), array(_DB_PREFIX_, _MYSQL_ENGINE_), $sql);
		$sql = preg_split(&quot;/;\s*[\r\n]+/&quot;, trim($sql));

		foreach ($sql as $query)
			if (!Db::getInstance()-&gt;execute(trim($query)))
				return false;
		if (parent::install() == false ||
			!$this-&gt;registerHook('productTab') ||
			!$this-&gt;registerHook('extraProductComparison') ||
			!$this-&gt;registerHook('productTabContent') ||
			!$this-&gt;registerHook('header') ||
			!$this-&gt;registerHook('productOutOfStock') ||
			!Configuration::updateValue('PRODUCT_COMMENTS_MINIMAL_TIME', 30) ||
			!Configuration::updateValue('PRODUCT_COMMENTS_ALLOW_GUESTS', 0) ||
			!Configuration::updateValue('PRODUCT_COMMENTS_MODERATE', 1))
				return false;
		return true;
	}
</pre>
<p>We want to add our new hook there, so let&#8217;s do it right after registering the <strong>productOutOfStock</strong> hook:</p>
<pre class="brush: php; title: ; notranslate">
	public function install()
	{
		if (!file_exists(dirname(__FILE__).'/'.self::INSTALL_SQL_FILE))
			return false;
		else if (!$sql = file_get_contents(dirname(__FILE__).'/'.self::INSTALL_SQL_FILE))
			return false;
		$sql = str_replace(array('PREFIX_', 'ENGINE_TYPE'), array(_DB_PREFIX_, _MYSQL_ENGINE_), $sql);
		$sql = preg_split(&quot;/;\s*[\r\n]+/&quot;, trim($sql));

		foreach ($sql as $query)
			if (!Db::getInstance()-&gt;execute(trim($query)))
				return false;
		if (parent::install() == false ||
			!$this-&gt;registerHook('productTab') ||
			!$this-&gt;registerHook('extraProductComparison') ||
			!$this-&gt;registerHook('productTabContent') ||
			!$this-&gt;registerHook('header') ||
			!$this-&gt;registerHook('productOutOfStock') ||
			!$this-&gt;registerHook('productInList') ||
			!Configuration::updateValue('PRODUCT_COMMENTS_MINIMAL_TIME', 30) ||
			!Configuration::updateValue('PRODUCT_COMMENTS_ALLOW_GUESTS', 0) ||
			!Configuration::updateValue('PRODUCT_COMMENTS_MODERATE', 1))
				return false;
		return true;
	}
</pre>
<p>As you noticed, we adedd <strong>!$this->registerHook(&#8216;productInList&#8217;) ||</strong>. This will be the name of our new hook.<br /><strong>Reset or install the module</strong>, then place a couple of comments and give them some different star rating. be sure they appear in the product page.</p>
<div class="separator"></div>
<h2>Step 2 &#8211; Adding the hook to the product list template</h2>
<p>Open up <strong>product-list.tpl</strong>, which can be found in <em>themes/nameofyourtheme</em> folder. I will be using the default 1.5.4 Template for this example.</p>
<p>Locate the product name, which should be around line 44:</p>
<pre class="brush: php; html-script: true; title: ; notranslate">
	&lt;h3&gt;&lt;a href=&quot;{$product.link|escape:'htmlall':'UTF-8'}&quot; title=&quot;{$product.name|escape:'htmlall':'UTF-8'}&quot;&gt;{$product.name|escape:'htmlall':'UTF-8'|truncate:35:'...'}&lt;/a&gt;&lt;/h3&gt;
</pre>
<p>After it, add the following snippet:</p>
<pre class="brush: php; title: ; notranslate">
	{hook h='productInList' idproduct=$product.id_product}
</pre>
<p><strong>Explanation:</strong> we are taking advantage of a new way to add hooks, which was introduced in Prestashop 1.5 (To learn more: <a href="http://nemops.com/adding-hooks-to-prestashop-1-5/" title="Quick Tip: Adding hooks to Prestashop 1.5 (the new way)" target="_blank">Adding new hooks to prestashop (the new way)</a>). We can pass any number of arguments to the parameters of the hook, and in this case I chose to pass the product id, which is available to us since we are right inside the products iteration function (foreach loop).</p>
<div class="separator"></div>
<h2>Step 3 &#8211; Creating the hooking function</h2>
<p>So, we have our hook. Let&#8217;s test it out. At the end of <strong>productcomments.php</strong> create a new function, as such:</p>
<pre class="brush: php; title: ; notranslate">
	function hookProductInList($params)
	{
		echo 'hey there';
	}
</pre>
<p>Save &amp; refresh, and you should get this:</p>
<p><a href="http://nemops.com/wp-content/uploads/2013/04/the_hey_there.png"><img src="http://nemops.com/wp-content/uploads/2013/04/the_hey_there.png" alt="Prestashop product rating in Products List - New hook in action" width="544" height="504" class="aligncenter size-full wp-image-1223" /></a></p>
<p>If you can&#8217;t see it, go back and check the previous steps, as it means the hook is not being processed. if it is, move forward and change the lastly created function this way:</p>
<pre class="brush: php; title: ; notranslate">
	function hookProductInList($params)
	{
		require_once(dirname(__FILE__).'/ProductComment.php');
		require_once(dirname(__FILE__).'/ProductCommentCriterion.php');

		$id_product = $params['idproduct'];
		$product_average = 0;

		$grades = ProductComment::getAveragesByProduct($id_product, $this-&gt;context-&gt;language-&gt;id);
		$criterions = ProductCommentCriterion::getByProduct($id_product, $this-&gt;context-&gt;language-&gt;id);
		$grade_total = 0;
	}
</pre>
<p><strong>Explanation:</strong> First, we need the <strong>ProductComment</strong> and <strong>ProductCommentCriterion</strong> classes, and we include them at the beginning of the method. Then, we define the product id as a variable, to avoid accessing the huge params array every time. Lastly, we get all the average grades for the product and all the criterions, and set a default value for the total grade, to avoid undeclared variable issues. Let&#8217;s go ahead and calculate the average grade for every product.</p>
<p>After the previous code, add the following:</p>
<pre class="brush: php; title: ; notranslate">
		if (count($grades) &gt; 0)
		{
			foreach ($criterions as $criterion)
			{
				if (isset($grades[$criterion['id_product_comment_criterion']]))
				{
					$grade_total += (float)($grades[$criterion['id_product_comment_criterion']]);
				}
			}

			$product_average = $grade_total / count($criterions);
		}
</pre>
<p><strong>Explanation:</strong> We check that we actually have grades, and then, for each criterion, if the grades array contains something for that criterion, we add it to the total. In this stage, we are building the sum of all grades. After looping through all the criterions, we get the total average dividing the total grade by the number of criterions. This way we are sure the final average is the &#8220;average of all averages&#8221; of all criterions.</p>
<p>As a final step in this function, let&#8217;s assign the number we just got, and return a template:</p>
<pre class="brush: php; title: ; notranslate">

		$this-&gt;context-&gt;smarty-&gt;assign('average_total', (int)$product_average);
		return $this-&gt;display(__FILE__, '/product-list.tpl');

</pre>
<p>We want to get the integer part of the average so be sure to add <strong>(int)</strong> when assigning it. We don&#8217;t have the mentioned <strong>product-list.tpl</strong> template file in the module&#8217;s folder, let&#8217;s create it in the last step!</p>
<div class="separator"></div>
<h2>Step 4 &#8211; Creating a template file to display the rating</h2>
<p>Create a new .tpl file in <em>modules/productcomments</em> and call it <strong>product-list</strong>.tpl. Be sure it has the .tpl extension. if you don&#8217;t know how to create one from scratch, copy and rename one of the files which are in the folder, and delete everything it contains. Open up the new file and add the following:</p>
<pre class="brush: php; html-script: true; title: ; notranslate">
	&lt;div class=&quot;star_content clearfix&quot;&gt;
	{section name=&quot;i&quot; start=0 loop=5 step=1}
		{if $smarty.section.i.index lt $average_total}
                        &lt;div class=&quot;star star_on&quot;&gt;&lt;a href=&quot;&quot;&gt;&lt;/a&gt;&lt;/div&gt;
		{else}
			&lt;div class=&quot;star&quot;&gt;&lt;a href=&quot;&quot;&gt;&lt;/a&gt;&lt;/div&gt;			
		{/if}
	{/section}
	&lt;/div&gt;
</pre>
<p>We are simply taking advantage of the <strong>section</strong> helper that smarty provides, allowing us to iterate a block of code for a defined number of times; in this case, 5. Inside the loop, we check the average total for the product. If it&#8217;s bigger than the current iteration index, it means the current star element must be active, if not, it must be grayed out. Save and refresh, you should get something like this:</p>
<p><a href="http://nemops.com/wp-content/uploads/2013/04/partial-end-result.png"><img src="http://nemops.com/wp-content/uploads/2013/04/partial-end-result.png" alt="Prestashop product rating in Products List - Almost Finished" width="542" height="614" class="aligncenter size-full wp-image-1225" /></a></p>
<p>Messy. Let&#8217;s clean it up a bit. Go to your theme folder and open up <em>css/product_list.css</em>. Locate <strong>#product_list li h3</strong>.</p>
<pre class="brush: css; title: ; notranslate">
	#product_list li h3 {
		padding:0 0 10px 0;
		font-size:13px;
		color:#000
	}
</pre>
<p>Change that padding to 0. All of it. Then, add the following at the end of the file (or where you want actually, it doesn&#8217;t matter):</p>
<pre class="brush: css; title: ; notranslate">
#product_list li .star_content  
{
	display: inline-block;
	margin-bottom: 10px;
}
</pre>
<p>Save and refresh. We are done! this is the end result. As you can see, the current product rating is now displaying correctly below each name:</p>
<p><a href="http://nemops.com/wp-content/uploads/2013/04/end_result.png"><img src="http://nemops.com/wp-content/uploads/2013/04/end_result.png" alt="Prestashop product rating in Products List - End result" width="550" height="883" class="aligncenter size-full wp-image-1222" /></a></p>
<p>The post <a rel="nofollow" href="http://nemops.com/prestashop-product-rating-product-list/">Display Product rating in the products list in Prestashop</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nemops.com/prestashop-product-rating-product-list/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
	</channel>
</rss>
