Showing results for 
Search instead for 
Did you mean: 

Reusing loaded objects in modules

Reusing loaded objects in modules

I am developing a module for filtering products on the category page based off product attributes which need parsed.  I have never worked with magento before and have spent the last few weeks reading and working with it and made a custom module that works as well as setup a development server and got all working.


One thing I find a bit overwhelming is the amount of OOP, I've worked a lot with drupal for instance and it is far more procedural.  What I wish to do is take the objects loaded on the category page already, and then use them in my module to get a list of their attributes and doing this without reloading them using the object manager, as I don't want to reget the data from the database/cache and create a new object I want to reference the product collection already loaded.  I am having trouble finding a way to do this as all the examples about getting product collections seem to run a fresh query, even if that query is based off the current category.


So that is my question, how do I get the loaded product collection on a category page as a reference in my module?  Here's what I've tried:

* Create a view/frontend/layout/catalog_category_view.xml file and a view/frontend/templates/category/test.phtml template

* have the layout xml refer to a Block/Category/Test.php Block

* In that block I copied parts of Block/Category/View.php of the module-category code, gave it the namespace of my module, and deleted all but the construct method and preparelayout method while keeping all properties and dependency injector style instantiations in the constructor.

* I want to do the coding to get all the product attributes on the page for my attribute, and then parse them with a custom function.  This is the part I need help on.  I could reload the objects using the object manager, but there will be millions of records and that could be costly.


I haven't found a way to do this outside perhaps an observer, but then I'd have to tie the reading of the product collection to an event that could be recalled with an expensive calculation I really only want to do once.  Dependency Injection doesn't seem to have an answer, the registry appears to give me a skeleton with no data unless I load it with attribute queries (so reloads again) as even trying to print the name of the products is empty, I've also looked at repositories but they seem to also load resources by id, etc, so I'm still not seeing a way to access to list of products on the category page.



Re: Reusing loaded objects in modules

I found an answer in part here:


I then had to call it like this in my Block however:

$collection = $this->_catalogLayer->getProductCollection();


looping over the object and printing the objects name now isn't blank.