Magento – product->isSalable() false positive

collection;productstatus

I have a custom product collection.

$collection = Mage::getModel('catalog/product')->getCollection()
                    ->addFieldToFilter('entity_id', array('in' => $ids));

The products have only some attributes set like: entity_id, entity_type_id, attribute_set_id, type_id, sku, created_at, updated_at, is_salable, stock_item (is an object).
Also for more attributes I can add the method

->addAttributeToSelect(array('name'))

I noticed, that if I don't add the status field to the collection using the method above, then if I want to see if the products from the collection can be bought using isSaleable it will return true because in Mage_Catalog_Model_Product

public function getStatus()
{
    if (is_null($this->_getData('status'))) {
        $this->setData('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
    } ...

and in Mage_Catalog_Model_Product_Type_Abstract

public function isSalable($product = null)
{
    $salable = $this->getProduct($product)->getStatus() == Mage_Catalog_Model_Product_Status::STATUS_ENABLED; ...

Is this a bug? Do I always need to add the status field to the collection? Does anyone know of any other fields you have to add to the collections because of Magento's logic?
Why didn't they force the status to be added in a core observer on before collection load?

Best Answer

it depends on what you want to do with the collection.
If you display it on frontend, you should add the status field to the collection.
You should also add a filter to the collection on the status.

->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)

because you don't want users to see the disabled product. They will not be able to add them to the cart anyway.
If you have the flat catalog enabled, it should not be a problem, because only enabled products are added to the flat tables.
But add the filter anyway, just to be paranoid.

Related Topic