Jump to content

[SOLVED] display lowest price of subcategory in listing


Recommended Posts

Hey guys,

 

I'm new to Prestashop and I'm trying to create an own theme. There's one thing I'm missing in hte category view (the subcategory in the category.tpl): the lowest price in each subcategory.

 

For example, in subcategory "shirts" the cheapest shirt costs 9,99 € and this should be displayed below the subcategory name.

 

But I don't know how to do so. Is there anybody who could give me a practical tipp?

 

Thanks in advance! :)

 

Hando

Edited by hando (see edit history)

Share this post


Link to post
Share on other sites

Okay guys, I've found a solution. For everyone who's interested in .. here's a step by step tutorial:

 

1st step: open Category Model Category.php (can be found in shoproot/classes/) and insert the following code snippet:

 

/**
 * Return lowest price in a specific (sub)category
 *
 * @param integer $id_category Indicates the category ID of that the lowest price will be looked up
 */
public function getLowestPriceOfCategory( $id_category ) {
  if( !Validate::isInt( $id_category ) ) {
  die( Tools::displayError() );
  }

  $sql = 'SELECT MIN( p.price ) lowest_price
 FROM `'._DB_PREFIX_.'product` p
 INNER JOIN `'._DB_PREFIX_.'category` c
 ON (p.`id_category_default` = c.`id_category` AND c.`id_category` = ' . (int) $id_category . ')
 GROUP BY c.`id_category`;';

  $result = Db::getInstance( _PS_USE_SQL_SLAVE_ )->getRow( $sql );

  return $result['lowest_price'];
}

 

2nd step: find the method called getSubCategories( ... ) and change the foreach loop from:

 

foreach ($result as &$row)
{
  $row['id_image'] = file_exists(_PS_CAT_IMG_DIR_.$row['id_category'].'.jpg') ? (int)$row['id_category'] : Language::getIsoById($id_lang).'-default';
  $row['legend'] = 'no picture';
}

 

to

 

foreach ($result as &$row)
{
  $row['id_image'] = file_exists(_PS_CAT_IMG_DIR_.$row['id_category'].'.jpg') ? (int)$row['id_category'] : Language::getIsoById($id_lang).'-default';
  $row['legend'] = 'no picture';
  $row['lowest_price'] = $this->getLowestPriceOfCategory( $row['id_category'] );
}

 

3rd step: in the category.tpl (located in shoproot/themes/templatename/) you can add the lowest price for a category within the loop {foreach from=$subcategories item=subcategory} using:

 

{$subcategory.lowest_price}

 

And ... that's it. Have fun :-)

 

See on Github

Edited by hando (see edit history)

Share this post


Link to post
Share on other sites

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

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More