I have a method, that returns me a lowest price product and it works fine:
/** @var Mage_Catalog_Model_Resource_Product_Collection $productCollection */
$product = Mage::getModel('catalog/product');
$productCollection = $product->getCollection();
$productCollection->addAttributeToSelect('*')
->setOrder('price', 'ASC')->load();
$productCollection->getSelect()->limit(1);
$product = $productCollection->getFirstItem();
if ($product->getId() != $productId) {
return $product;
} else {
return false;
}
But if I add filtering by category, it`s return NULL
/** @var Mage_Catalog_Model_Category $category */
$category = Mage::getModel('catalog/category');
$category->load($categoryId);
/** @var Mage_Catalog_Model_Resource_Product_Collection $productCollection */
$product = Mage::getModel('catalog/product');
$productCollection = $product->getCollection();
$productCollection->addCategoryFilter($category);
$productCollection->addAttributeToSelect('*')
->setOrder('price', 'ASC')->load();
$productCollection->getSelect()->limit(1);
$product = $productCollection->getFirstItem();
if ($product->getId() != $productId) {
return $product;
} else {
return false;
}