Reputation: 335
I need to sort based on products price low and high.
Display other products in the same category in below location
\modules\productscategory\productscategory.tpl
{if count($categoryProducts) > 0 && $categoryProducts !== false}
<div class="clearfix blockproductscategory">
<h2 class="productscategory_h2">{$categoryProducts|@count} {l s='other products in the same category:' mod='productscategory'}</h2>
<div id="{if count($categoryProducts) > 5}productscategory{else}productscategory_noscroll{/if}">
{if count($categoryProducts) > 5}<a id="productscategory_scroll_left" title="{l s='Previous' mod='productscategory'}" href="javascript:{ldelim}{rdelim}">{l s='Previous' mod='productscategory'}</a>{/if}
<div id="productscategory_list">
<ul {if count($categoryProducts) > 5}style="width: {math equation="width * nbImages" width=107 nbImages=$categoryProducts|@count}px"{/if}>
{foreach from=$categoryProducts item='categoryProduct' name=categoryProduct}
<li {if count($categoryProducts) < 6}style="width:60px"{/if}>
<a href="{$link->getProductLink($categoryProduct.id_product, $categoryProduct.link_rewrite, $categoryProduct.category, $categoryProduct.ean13)}" class="lnk_img" title="{$categoryProduct.name|htmlspecialchars}"><img src="{$link->getImageLink($categoryProduct.link_rewrite, $categoryProduct.id_image, 'medium_default')}" alt="{$categoryProduct.name|htmlspecialchars}" /></a>
<p class="product_name">
<a href="{$link->getProductLink($categoryProduct.id_product, $categoryProduct.link_rewrite, $categoryProduct.category, $categoryProduct.ean13)}" title="{$categoryProduct.name|htmlspecialchars}">{$categoryProduct.name|truncate:14:'...'|escape:'htmlall':'UTF-8'}</a>
</p>
{if $ProdDisplayPrice AND $categoryProduct.show_price == 1 AND !isset($restricted_country_mode) AND !$PS_CATALOG_MODE}
<p class="price_display">
<span class="price">{convertPrice price=$categoryProduct.displayed_price}</span>
</p>
{else}
<br />
{/if}
</li>
{/foreach}
</ul>
</div>
{if count($categoryProducts) > 5}<a id="productscategory_scroll_right" title="{l s='Next' mod='productscategory'}" href="javascript:{ldelim}{rdelim}">{l s='Next' mod='productscategory'}</a>{/if}
</div>
<script type="text/javascript">
$('#productscategory_list').trigger('goto', [{$middlePosition}-3]);
</script>
</div>
{/if}
function in \modules\productscategory\productscategory.php
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']->id_category))
$category = $params['category'];
/* Else, use the default product category */
else
{
if (isset($product->id_category_default) AND $product->id_category_default > 1)
$category = New Category((int)($product->id_category_default));
}
if (!Validate::isLoadedObject($category) OR !$category->active)
return;
// Get infos
$categoryProducts = $category->getProducts($this->context->language->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 => $categoryProduct)
if ($categoryProduct['id_product'] == $idProduct)
{
unset($categoryProducts[$key]);
break;
}
$taxes = Product::getTaxCalculationMethod();
if (Configuration::get('PRODUCTSCATEGORY_DISPLAY_PRICE'))
foreach ($categoryProducts AS $key => $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->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 > 30)
{
$categoryProducts = array_slice($categoryProducts, $middlePosition - 15, 30, true);
$middlePosition = 15;
}
}
$best_sellers = $this->getBestSellers($params);
if ($best_sellers === false)
return;
$this->smarty->assign(array(
'best_sellers' => $best_sellers,
'mediumSize' => Image::getSize(ImageType::getFormatedName('medium')),
'smallSize' => Image::getSize(ImageType::getFormatedName('small'))
));
$bundle_products = $this->bundleproduct($params);
$this->smarty->assign(array(
'bundle_products' => $bundle_products,
'mediumSize' => Image::getSize(ImageType::getFormatedName('medium')),
'smallSize' => Image::getSize(ImageType::getFormatedName('small'))
));
$id_product = (int)Tools::getValue('id_product');
$productsViewed = (isset($params['cookie']->viewed) && !empty($params['cookie']->viewed)) ? array_slice(explode(',', $params['cookie']->viewed), 0, Configuration::get('PRODUCTS_VIEWED_NBR')) : array();
if (count($productsViewed))
{
$defaultCover = Language::getIsoById($params['cookie']->id_lang).'-default';
$productIds = implode(',', $productsViewed);
$productsImages = Db::getInstance(_PS_USE_SQL_SLAVE_)->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']->id_lang).'
AND cl.id_lang = '.(int)($params['cookie']->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->active = $productsImagesArray[$productViewed]['active']))
continue;
else
{
$obj->id = (int)($productsImagesArray[$productViewed]['id_product']);
$obj->id_image = (int)$productsImagesArray[$productViewed]['id_image'];
$obj->cover = (int)($productsImagesArray[$productViewed]['id_product']).'-'.(int)($productsImagesArray[$productViewed]['id_image']);
$obj->legend = $productsImagesArray[$productViewed]['legend'];
$obj->name = $productsImagesArray[$productViewed]['name'];
$obj->description_short = $productsImagesArray[$productViewed]['description_short'];
$obj->link_rewrite = $productsImagesArray[$productViewed]['link_rewrite'];
$obj->category_rewrite = $productsImagesArray[$productViewed]['category_rewrite'];
// $obj is not a real product so it cannot be used as argument for getProductLink()
$obj->product_link = $this->context->link->getProductLink($obj->id, $obj->link_rewrite, $obj->category_rewrite);
if (!isset($obj->cover) || !$productsImagesArray[$productViewed]['id_image'])
{
$obj->cover = $defaultCover;
$obj->legend = '';
}
$productsViewedObj[] = $obj;
}
}
if ($id_product && !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->checkAccess((int)$this->context->customer->id))
array_unshift($productsViewed, $id_product);
}
$viewed = '';
foreach ($productsViewed as $id_product_viewed)
$viewed .= (int)($id_product_viewed).',';
$params['cookie']->viewed = rtrim($viewed, ',');
if (!count($productsViewedObj))
return;
$this->smarty->assign(array(
'productsViewedObj' => $productsViewedObj,
'mediumSize' => Image::getSize('medium')));
}
$this->smarty->assign(array(
'categoryProducts' => $categoryProducts,
'middlePosition' => (int)$middlePosition,
'ProdDisplayPrice' => Configuration::get('PRODUCTSCATEGORY_DISPLAY_PRICE')));
return $this->display(__FILE__, 'productscategory.tpl');
}
how to sort products based on price ?
Upvotes: 1
Views: 3086
Reputation: 5202
You can sort product by passing sorting information to the getProducts member function. Change your below code :
// Get infos
$categoryProducts = $category->getProducts($this->context->language->id, 1, 100); /* 100 products max. */
to the following code
// Get infos
$categoryProducts = $category->getProducts($this->context->language->id, 1, 100, 'price', 'ASC'); /* 100 products max. */
After numbre of products (in your case it is 100), you can pass Order By (in the above code it is price, you can pass other options also like position, name, id_product etc) argument and then Order Way argument (in the above example, it is ASC, you can also use DESC)
Upvotes: 1