1

I am looping through each sub-category and spitting out its name,url,image etc.

But I also want to show a "From £XX" for each sub category I show.

What is the best/cleanest method for doing this? My current code is below:

<?php
$currentCat = Mage::getModel('catalog/category')->load('4');
$subCategories = $currentCat->getChildrenCategories();

foreach ( $subCategories as $subCategory )
{
    if($subCategory->getIsActive())
    {
        echo'<h3><a href="'.$subCategory->getUrl().'">'.$subCategory->getName().'</a></h3>';
          echo'<a href="'.$subCategory->getUrl().'">';
          echo'<img src="'.Mage::getBaseUrl('media').'catalog/category/'.$subCategory->load($subCategory->getId())->getImage().'" alt="Budget Stove" />';
          echo'<div class="pl_prod_price">From <span>&pound;00.00</span></div>';
        echo'</a>';


    }
}
?>

3 Answers 3

2

Try using the below code to get lowest price in subcategory

$product = Mage::getModel('catalog/category')->load($subCategory->getId())
 ->getProductCollection()
 ->setOrder('price', 'ASC') //sets the order by price 
 ->getFirstItem(); 

echo $product->getFinalPrice();
1

This is an old post but I also want to answer to flag some issues.

The first issue with @Narayan's answer is that he has sorted by price and not by finalprice which is wrong.

The issue with @AmitBera's answer is that he has taken the product collection instead of the category collection, also $collection->getFirst(); is wrong this should be $collection->getFirstItem();.

So here is your final answer

$collection = Mage::getModel('catalog/category')->load($yourcategory_id)
 ->getProductCollection();
$collection
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents(); 
$collection->getSelect()->order("price_index.final_price ASC") // Sort Collection by ASC price
->limit(1) ;
$LowestPricedproduct= $collection->getFirstItem(); 
echo Mage::helper('core')->currency( $LowestPricedproduct->getFinalPrice() , true, false); 
0

KiwisTasteGood,

First thing need to understand that Price is an attribute of product and not an attribute of catgory.

If want to get lowest of price,then you

  • should call product collection of that category
  • and get lowest price from that collection
  • Sort that Collection by final_price.

$collection = Mage::getResourceModel('catalog/product_collection'); $collection->addMinimalPrice()->addFinalPrice() ->addTaxPercents(); $collection->getSelect() ->order("price_index.final_price ASC") // Sort COllection by ASC
->limit(1) $LowestPricedproduct= $collection->getFirst(); $LowestPricedproduct->getFinalPrice();

3
  • "an attribute of product and not an attribute of product" so what is it then? Commented Jun 7, 2016 at 13:50
  • @AndreFerraz,thank for notice .. it should be an attribute of product and not an attribute of category". Typo mistake
    – Amit Bera
    Commented Jun 7, 2016 at 14:15
  • Hi @AmitBera your answer help me, but there are few errors if you allow me then I would like to update your answer . Commented Apr 17, 2017 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.