0

How can I show all products in a category, regardless of its status? I have a category that I want to show all products in the category, even it is disabled, invisible, out of stock, or whatever. I've made a new product/list.phtml template for this specific category and am overriding the Mage_Catalog_Block_Product_List to add a new method to accomplish this. In that method is this code

$catId = 432;  //for simplicity
$category = Mage::getModel('catalog/category')->load($catId);

return $category->getProductCollection()
    ->addAttributeToSelect('sku');

It is only retrieving enabled products. However, if I run the same code in a testing script (outside of catalog module) that loads Magento, I get all products that I'm looking for, enabled and disabled. How can I accomplish this?

3
  • would you like to use the standard template (catalog/product/list.phtml) or a custom one ?
    – Giuseppe
    Jul 18, 2014 at 6:45
  • The layered navigation manipulates the collection filters in \Mage_Catalog_Model_Layer::prepareProductCollection.
    – Thorsten
    Jul 18, 2014 at 7:23
  • @Thorsten That's why I rewrote the block so I could use a method that didn't go through the Layer class. Are you saying this is still having an affect? Jul 18, 2014 at 18:30

2 Answers 2

3

Please use this code

$productCollection = Mage::getResourceModel('catalog/product_collection')

                        ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left')
                        ->addAttributeToFilter('category_id', 3);
                       $productCollection->getSelect()->group('product_id')->distinct(true);
echo '<pre>'; print_r($productCollection->getData());

Definitely this work for you.

1
  • This is retrieving a huge list. I only have 5 products in the category, and this code is getting hundreds. Jul 18, 2014 at 18:12
1

I got this working by doing two things:

1) Add ->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID) to the collection. collection::isEnabledFlat() returns false if the store is admin. However, it was still trying to pull from flat tables and was causing Magento to throw an error. The next step resolved this issue..

2) I had to set System > Configuration > Catalog > Frontend > Use Flat Catalog Product = No

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.