简体   繁体   中英

Magento What is the Best Way to Resolve Observer Load Order Issue?

I need to insert a javascript object into the head of my listing pages containing a json representation of the products listed on that page, ie not all products in the category but only those that have been paged by the toolbar.

I've found that I can get that data by putting an observer on catalog_block_product_list_collection but this is after the Head Block has been rendered, so therefore prevents me using that data in a child block of the Head.

As far as I can see I've got two options.

  1. Load a javascript block to the footer and use jquery to inject it to the header.
  2. Work out what Magento did before catalog_block_product_list_collection was called and recreate the logic in my model.

I wondered which way experienced Magento developers would jump and the pitfalls of both techniques, Thanks in advance.

So you already have added a custom block to the header, if I got you right, if so, you should be able to just retrieve the product collection from the list block.

$this->getLayout()->getBlock('product_list')->getLoadedProductCollection();

I might have misunderstood your question, but maybe it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM