2

I'm using magento 1.9.3.1 and I'm wondering how I can conditionally remove items when they are sorted by price on a category page.

I have items that contain specific attributes, that when sorted by price I would like those items removed entirely from the results.

Is this possible?

1
  • Did my answer help?
    – tjons
    Commented Nov 28, 2016 at 21:49

1 Answer 1

1

Sure, that's possible. *winks*. Nearly anything is possible in Magento.

Take a look at the Mage_Catalog_Block_Product_List_Toolbar class. This class handles the ordering of the product collection that is then used to create the product list; specifically, I'd focus on the getCurrentOrder() and setCollection() methods. I suggest creating a block that extends it, and then use that block as a replacement for the original one in layout XML.

You'll need to override the setCollection() method. On lines 232 and 233 of the class, there's a perfect way for you to hook in and modify the functionality. After the order's existence is checked, you can add an additional check to make sure that it's the sort method you want. Then, after the order is set, you can add the code to add an attribute as a filter (addAttributeToFilter()).

Make sure that you don't edit any core code in the process - my suggestions should be implemented in the class that extends the core block.

Please let me know if you have any questions.

Edit: After writing the above, I noticed there's a better way:

In the block that "owns" that collection, after the collection is set, an event is fired that would allow you to do everything you needed to: catalog_block_product_list_collection. I suggest listening to that event, and then implementing the sort check and attribute filtering in the observer.

Once again, if you have questions, let me know.

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.