title: how to sort products based on price in prestashop
body: <p>I need to sort based on products price low and high.</p>
  
  <p>Display other products in the same category in below location</p>
  
  <p>\modules\productscategory\productscategory.tpl</p>
  
  <pre><code>{if count($categoryProducts) &gt; 0 &amp;&amp; $categoryProducts !== false}
  &lt;div class="clearfix blockproductscategory"&gt;
  &lt;h2 class="productscategory_h2"&gt;{$categoryProducts|@count} {l s='other products in the same category:' mod='productscategory'}&lt;/h2&gt;
  &lt;div id="{if count($categoryProducts) &gt; 5}productscategory{else}productscategory_noscroll{/if}"&gt;
  {if count($categoryProducts) &gt; 5}&lt;a id="productscategory_scroll_left" title="{l s='Previous' mod='productscategory'}" href="javascript:{ldelim}{rdelim}"&gt;{l s='Previous' mod='productscategory'}&lt;/a&gt;{/if}
  &lt;div id="productscategory_list"&gt;
  &lt;ul {if count($categoryProducts) &gt; 5}style="width: {math equation="width * nbImages" width=107 nbImages=$categoryProducts|@count}px"{/if}&gt;
  {foreach from=$categoryProducts item='categoryProduct' name=categoryProduct}
  &lt;li {if count($categoryProducts) &lt; 6}style="width:60px"{/if}&gt;
  &lt;a href="{$link-&gt;getProductLink($categoryProduct.id_product, $categoryProduct.link_rewrite, $categoryProduct.category, $categoryProduct.ean13)}" class="lnk_img" title="{$categoryProduct.name|htmlspecialchars}"&gt;&lt;img src="{$link-&gt;getImageLink($categoryProduct.link_rewrite, $categoryProduct.id_image, 'medium_default')}" alt="{$categoryProduct.name|htmlspecialchars}" /&gt;&lt;/a&gt;
  &lt;p class="product_name"&gt;
  &lt;a href="{$link-&gt;getProductLink($categoryProduct.id_product, $categoryProduct.link_rewrite, $categoryProduct.category, $categoryProduct.ean13)}" title="{$categoryProduct.name|htmlspecialchars}"&gt;{$categoryProduct.name|truncate:14:'...'|escape:'htmlall':'UTF-8'}&lt;/a&gt;
  &lt;/p&gt;
  {if $ProdDisplayPrice AND $categoryProduct.show_price == 1 AND !isset($restricted_country_mode) AND !$PS_CATALOG_MODE}
  &lt;p class="price_display"&gt;
  &lt;span class="price"&gt;{convertPrice price=$categoryProduct.displayed_price}&lt;/span&gt;
  &lt;/p&gt;
  {else}
  &lt;br /&gt;
  {/if}
  &lt;/li&gt;
  {/foreach}
  &lt;/ul&gt;
  &lt;/div&gt;
  {if count($categoryProducts) &gt; 5}&lt;a id="productscategory_scroll_right" title="{l s='Next' mod='productscategory'}" href="javascript:{ldelim}{rdelim}"&gt;{l s='Next' mod='productscategory'}&lt;/a&gt;{/if}
  &lt;/div&gt;
  &lt;script type="text/javascript"&gt;
  $('#productscategory_list').trigger('goto', [{$middlePosition}-3]);
  &lt;/script&gt;
  &lt;/div&gt;
  {/if}
  </code></pre>
  
  <p>function in \modules\productscategory\productscategory.php</p>
  
  <pre><code>public function hookProductFooter($params)
  {
  
  
  
  $idProduct = (int)(Tools::getValue('id_product'));
  $product = new Product((int)($idProduct));
  
  /* If the visitor has came to this product by a category, use this one */
  if (isset($params['category']-&gt;id_category))
  $category = $params['category'];
  /* Else, use the default product category */
  else
  {
  if (isset($product-&gt;id_category_default) AND $product-&gt;id_category_default &gt; 1)
  $category = New Category((int)($product-&gt;id_category_default));
  }
  
  if (!Validate::isLoadedObject($category) OR !$category-&gt;active)
  return;
  
  // Get infos
  $categoryProducts = $category-&gt;getProducts($this-&gt;context-&gt;language-&gt;id, 1, 100); /* 100 products max. */
  $sizeOfCategoryProducts = (int)sizeof($categoryProducts);
  $middlePosition = 0;
  
  // Remove current product from the list
  if (is_array($categoryProducts) AND sizeof($categoryProducts))
  {
  foreach ($categoryProducts AS $key =&gt; $categoryProduct)
  if ($categoryProduct['id_product'] == $idProduct)
  {
  unset($categoryProducts[$key]);
  break;
  }
  
  $taxes = Product::getTaxCalculationMethod();
  if (Configuration::get('PRODUCTSCATEGORY_DISPLAY_PRICE'))
  foreach ($categoryProducts AS $key =&gt; $categoryProduct)
  if ($categoryProduct['id_product'] != $idProduct)
  {
  if ($taxes == 0 OR $taxes == 2)
  $categoryProducts[$key]['displayed_price'] = Product::getPriceStatic((int)$categoryProduct['id_product'], true, NULL, 2);
  elseif ($taxes == 1)
  $categoryProducts[$key]['displayed_price'] = Product::getPriceStatic((int)$categoryProduct['id_product'], false, NULL, 2);
  }
  
  // Get positions
  $middlePosition = round($sizeOfCategoryProducts / 2, 0);
  $productPosition = $this-&gt;getCurrentProduct($categoryProducts, (int)$idProduct);
  
  // Flip middle product with current product
  if ($productPosition)
  {
  $tmp = $categoryProducts[$middlePosition-1];
  $categoryProducts[$middlePosition-1] = $categoryProducts[$productPosition];
  $categoryProducts[$productPosition] = $tmp;
  }
  
  // If products tab higher than 30, slice it
  if ($sizeOfCategoryProducts &gt; 30)
  {
  $categoryProducts = array_slice($categoryProducts, $middlePosition - 15, 30, true);
  $middlePosition = 15;
  }
  }
  
  
  
  $best_sellers = $this-&gt;getBestSellers($params);
  if ($best_sellers === false)
  return;
  
  $this-&gt;smarty-&gt;assign(array(
  'best_sellers' =&gt; $best_sellers,
  'mediumSize' =&gt; Image::getSize(ImageType::getFormatedName('medium')),
  'smallSize' =&gt; Image::getSize(ImageType::getFormatedName('small'))
  ));
  
  
  
            $bundle_products = $this-&gt;bundleproduct($params);
  
  
  $this-&gt;smarty-&gt;assign(array(
  'bundle_products' =&gt; $bundle_products,
  'mediumSize' =&gt; Image::getSize(ImageType::getFormatedName('medium')),
  'smallSize' =&gt; Image::getSize(ImageType::getFormatedName('small'))
  ));
  
  
  
             $id_product = (int)Tools::getValue('id_product');
  $productsViewed = (isset($params['cookie']-&gt;viewed) &amp;&amp; !empty($params['cookie']-&gt;viewed)) ? array_slice(explode(',', $params['cookie']-&gt;viewed), 0, Configuration::get('PRODUCTS_VIEWED_NBR')) : array();
  
  if (count($productsViewed))
  {
  $defaultCover = Language::getIsoById($params['cookie']-&gt;id_lang).'-default';
  
  $productIds = implode(',', $productsViewed);
  $productsImages = Db::getInstance(_PS_USE_SQL_SLAVE_)-&gt;executeS('
  SELECT image_shop.id_image, p.id_product, il.legend, product_shop.active, pl.name, pl.description_short, pl.link_rewrite, cl.link_rewrite AS category_rewrite
  FROM '._DB_PREFIX_.'product p
  '.Shop::addSqlAssociation('product', 'p').'
  LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = p.id_product'.Shop::addSqlRestrictionOnLang('pl').')
  LEFT JOIN '._DB_PREFIX_.'image i ON (i.id_product = p.id_product)'.
  Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
  LEFT JOIN '._DB_PREFIX_.'image_lang il ON (il.id_image = i.id_image)
  LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = product_shop.id_category_default'.Shop::addSqlRestrictionOnLang('cl').')
  WHERE p.id_product IN ('.$productIds.')
  AND pl.id_lang = '.(int)($params['cookie']-&gt;id_lang).'
  AND cl.id_lang = '.(int)($params['cookie']-&gt;id_lang).'
  GROUP BY product_shop.id_product'
  );
  
  $productsImagesArray = array();
  foreach ($productsImages as $pi)
  $productsImagesArray[$pi['id_product']] = $pi;
  
  $productsViewedObj = array();
  foreach ($productsViewed as $productViewed)
  {
  $obj = (object)'Product';
  if (!isset($productsImagesArray[$productViewed]) || (!$obj-&gt;active = $productsImagesArray[$productViewed]['active']))
  continue;
  else
  {
  $obj-&gt;id = (int)($productsImagesArray[$productViewed]['id_product']);
  $obj-&gt;id_image = (int)$productsImagesArray[$productViewed]['id_image'];
  $obj-&gt;cover = (int)($productsImagesArray[$productViewed]['id_product']).'-'.(int)($productsImagesArray[$productViewed]['id_image']);
  $obj-&gt;legend = $productsImagesArray[$productViewed]['legend'];
  $obj-&gt;name = $productsImagesArray[$productViewed]['name'];
  $obj-&gt;description_short = $productsImagesArray[$productViewed]['description_short'];
  $obj-&gt;link_rewrite = $productsImagesArray[$productViewed]['link_rewrite'];
  $obj-&gt;category_rewrite = $productsImagesArray[$productViewed]['category_rewrite'];
  // $obj is not a real product so it cannot be used as argument for getProductLink()
  $obj-&gt;product_link = $this-&gt;context-&gt;link-&gt;getProductLink($obj-&gt;id, $obj-&gt;link_rewrite, $obj-&gt;category_rewrite);
  
  if (!isset($obj-&gt;cover) || !$productsImagesArray[$productViewed]['id_image'])
  {
  $obj-&gt;cover = $defaultCover;
  $obj-&gt;legend = '';
  }
  $productsViewedObj[] = $obj;
  }
  }
  
  if ($id_product &amp;&amp; !in_array($id_product, $productsViewed))
  {
  // Check if the user to the right of access to this product
  $product = new Product((int)$id_product);
  if ($product-&gt;checkAccess((int)$this-&gt;context-&gt;customer-&gt;id))
  array_unshift($productsViewed, $id_product);
  }
  $viewed = '';
  foreach ($productsViewed as $id_product_viewed)
  $viewed .= (int)($id_product_viewed).',';
  $params['cookie']-&gt;viewed = rtrim($viewed, ',');
  
  if (!count($productsViewedObj))
  return;
  
  $this-&gt;smarty-&gt;assign(array(
  'productsViewedObj' =&gt; $productsViewedObj,
  'mediumSize' =&gt; Image::getSize('medium')));
                   }
  
  $this-&gt;smarty-&gt;assign(array(
  'categoryProducts' =&gt; $categoryProducts,
  'middlePosition' =&gt; (int)$middlePosition,
  'ProdDisplayPrice' =&gt; Configuration::get('PRODUCTSCATEGORY_DISPLAY_PRICE')));
  
  return $this-&gt;display(__FILE__, 'productscategory.tpl');
  }
  </code></pre>
  
  <p>how to sort products based on price ? </p>