1

So I have a configurable product which pulls certain attributes from the simple product using this code:

if($_product->getTypeId() == "configurable"):
$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
foreach($simple_collection as $simple_product){
    echo $simple_product->getSku() . " - " . $simple_product->getName() . " - " . Mage::helper('core')->currency($simple_product->getPrice()) . "<br>";
}

This works perfectly.

What I want it to do now is sort the products by price, with the lowest at the top.

I am a noob, so if anyone can help me with it and modify the code for my needs, you will be a life saver :)

Thanks in advance

1 Answer 1

1

the following should do the trick:

->addFinalPrice()

and

->order('price_index.final_price', 'asc')

The line will look like following:

$simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions()->addFinalPrice()->order('price_index.final_price', 'asc');

I assume "lowest at the top" means "lowest first"? If the order does not fit, use 'desc' instead of 'asc'

Edit:

My bad, the 'order' function does not exists on this class.

Therefore it should be:

->addAttributeToSort('price_index.final_price', 'asc')

Also i saw that you just fetch the price. But when there is a sale, you will not show the different price there. I recommend you use the $_product->getFinalPrice() function.

But try the following instead of the above solution:

$conf->getUsedProductCollection()
                ->addAttributeToSelect('*')
                ->addFilterByRequiredOptions()
                ->addFinalPrice()
                ->addAttributeToSort('price_index.final_price', 'asc');

To display prices i recommend:

Mage::helper('core')->currency($simple_product->getFinalPrice())
4
  • Sorry, yes, lowest price first. Unfortunately this didnt work, just broke the page :( Commented Aug 27, 2015 at 15:41
  • I updated my answer
    – sebastian
    Commented Aug 27, 2015 at 16:22
  • Still didn't work. Didn't break the page though. Commented Aug 27, 2015 at 18:46
  • I kept the price as it was because the price actually gets updated from another website and will never have a sale value. I added the code which you provided but used the simple price attribute ->addAttributeToSort('Price', 'asc') Thanks a lot for your help :) Commented Aug 27, 2015 at 19:47

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.