5

In my magento store (ce-1.9.0) I have setup a custom product attribute:

custom_depth_check

And the attribute has the setting of

Used in Product Listing : Yes
Used for Sorting in Product Listing : Yes

I now need to filter any getLoadedProductCollection() with this attribute. The attribute will either be set at:

null
0
1

I need to filter all instances of getLoadedProductCollection() so that it will EXLCUDE any products with

custom_depth_check : 1

I have tried to clear and reload like so:

$_productCollection=$this->getLoadedProductCollection()
->clear()
->addAttributeToFilter('custom_depth_check', array('neq' => 1));

But that results in an empty collection. I also tried adding ->load() at the end but again nothing comes up.

I have to stored this collection into an array for specific field such as name, price, description, images etc.

how can i achieve this.

6
  • The issue here is that it getLoadedProductCollection probably returns a collection which has been loaded. All filters etc have already been applied and it is now immutable. Are you using Mage_Catalog_Block_Product_List:getLoadedProductCollection ? Commented Oct 22, 2015 at 14:23
  • Yep, I also tried making a local version of that List.php file and trying to add it in there but couldn't see how - on line #56 - protected function _getProductCollection()
    – odd_duck
    Commented Oct 22, 2015 at 14:27
  • Reckon I've got it. In your local version of List.php copy down the functionality from Mage_Catalog_Model_Layer::prepareProductCollection You'll want to add your new filter right below the addAttributeToSelect line. I think one of the addMinimalPrice, addUrlRewrite etc are loading the collection ahead of what you want. Commented Oct 22, 2015 at 14:32
  • To confirm - am I making a local copy of Layer.php and adding my filter in there? (line 120?) or copying that function and using it in my local copy of List.php?
    – odd_duck
    Commented Oct 22, 2015 at 14:47
  • 1
    Did this all work out for ya? I'll pop it as answer if so. Commented Oct 25, 2015 at 11:32

1 Answer 1

5

The issue is that Mage_Catalog_Block_Product_List:getLoadedProductCollection returns a loaded product collection. Meaning that it has already had all filters applied to it etc and has actually run the query.

To add new parameters to this query you need to rewrite/override Mage_Catalog_Model_Layer::prepareProductCollection and pop your extra filters in right below the filters already present. Make sure it's above the addMInimumPrice etc as one of these is going to be the thing causing your collection to be loaded.

You can see the instruction here on how to rewrite this block (How to Override Mage_Checkout_Block_Links).

An alternative (and my preferred solution) is to create a new block extending the Mage_Catalog_Block_Product_List and make a layout xml update to tell the code to use your block.

Either way, that's all that needed. Glad to have helped.

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.