1

I'm listening to the catalog_product_collection_load_after event to add some code when the category products grid / list is loaded, and from the observer code I want to get the following data:

  1. is it list view or grid view?
  2. if grid view, how many products per row?

looks like I need to access the products toolbar block, but I'm not sure how to do it. or maybe there's a better way?

Edit: forgot to mention, when screen is resized products per row change (for example reduced on smaller screens). can I know that ahead inside the php?

thanks!

2

You are correct with the assumption that you need the toolbar block to access that information. But the event you are currently listening, catalog_product_collection_load_after will only contain the information obtained regarding the product data, not it's representational logic.

An alternative approach might be to listen to the event core_block_abstract_prepare_layout_after and applying the filter for only toolbar block(make it first filter as that event is triggered on generating every single block in Magento). This way you can obtain all the information from toolbar block right after it is initialized.

Also, if you are seeing the reduced number of products per column, then it is responsive design in action and you can't know that in advance via PHP. It is very possible that on different screen sizes, your column-count remains same, it's just CSS is adjusting it to display properly.

  • good answer, thank you. but you really don't know any (reasonable) way to access the toolbar data? maybe via some global helpers or something like that? thanks! – Ronen Ness Dec 14 '15 at 11:43
  • Global helpers or something like that won't work because this is based upon the category for which the collection is being loaded. There is no certainty that same toolbar will be working for all the categories. You can use the singleton approach for a way to improve some performance though. – Prateek Dec 14 '15 at 12: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.