1

On Magento 1.9.x.x. I want to get the product with the lowest value in a given category. I use the following code:

$product = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('page_number')
    ->addAttributeToSelect('sku')
    ->addFieldToFilter('category_id',$category->getId())
    ->addFieldToFilter('page_number',array('notnull' => true))
    ->addAttributeToFilter('status', array('eq' => 1))
    ->addAttributeToSort('page_number', 'ASC')
    ->getFirstItem();

The page_number is an multi-select with integers as key and value. For example: Page 1 can have ID 512 and page 2 can have 412 as ID. So sorting on the ID's won’t work, I need to sort on the actual values.

Is it possible to do this directly? I don’t want to get all products and loop trough them because of performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.