1

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?

1 Answer 1

2

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.

2
  • "you don't want users to see the disabled product" that's an assumption... Let's say that I show this custom collection on the frontend and I show all the products, just that I check if they are saleable and show an "Add to cart" button, otherwise if they don't have stock I'll show a "Notify me when this product has qty" or if they aren't enabled "Notify me when the next generation gadget can be bought"
    – Emi
    Jul 29, 2015 at 7:34
  • @Emi. It's not an assumption. This is how it should work. A disabled product should not appear on the website. That's what disabled means. If you want to show it but don't allow users to buy it, just set it out of stock, but it should still be enabled.
    – Marius
    Jul 29, 2015 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.