4

This is my code:

$visibility = array(
   Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
   Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG,
   Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE
);

 $_productCollection = Mage::getSingleton('catalog/category')
    ->load($_categoryId)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('visibility', $visibility);

I want to get all products, no matter what visibility they have. The code above skips those that are not visible though. Whats wrong? Is there another way to do what I want?

5

The method getProductCollection() of the category model already applies some filters. If you look at Mage_Catalog_Model_Resource_Product_Collection::_applyProductLimitations() you will see that the visibility filter is part of a join condition that joins the category with catalog/category_product_index, but is only applied if isset($filters['visibility']). You define this filter with setVisibility(), so to remove the filter you can use:

$_productCollection->setVisibility(null);

Or to filter explicitly for the possible values:

$_productCollection->setVisibility(array(
   Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
   Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG,
   Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE
);
3
1

Remove the visibility filter: ->addAttributeToFilter('visibility', $visibility).

Use this:

$_productCollection = Mage::getSingleton('catalog/category')
    ->load($_categoryId)
    ->getProductCollection()
    ->addAttributeToSelect('*');
2
  • That gives me only the visible ones. Oct 20 '14 at 9:03
  • it loads the category resource model which filters out the not visible products
    – huzefam
    Oct 20 '14 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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