0

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;
    }

1 Answer 1

1

You can find ans in below link. I have same issue which i have fixed from URL.

also you can use

->addAttributeToFilter('category_id', array(
                    array('in' => $currentCategoryId),
            ))
1
  • Thank you! The solution is to adding $category->setIsAnchor(true); after loading category Jul 29, 2016 at 7:00

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.