1

Going a bit nuts trying to get my head around this. Debug mode doesn't help me.

I'm trying to add a block just before the </header> on only Product Pages. I've created a phtml file with my content (banner). And I've added the below code to:

<theme>/<child>/Magento_Catalog/templates/layout/catalog_product_view.xml

<referenceContainer name="root">
        <block class="Smartwave\Porto\Block\Template" name="banner" template="Magento_Catalog::product/view/banner.phtml"/>
</referenceContainer>
<move element="banner" destination="header" after="-"/>

In my head, the above:

  • Loads Block in the root of the page with <referenceContainer name="root">
  • Then moves the Block to the header container: <move element="banner" destination="header"...
  • And puts it at the very bottom: ...after="-"/>

Clearly though, it does not. I've used a ton of trial-and-error with varying results. It either loads in places I don't want or doesn't appear at all.

Edit

It appears that </header> is part of header.phtml and so I cannot accomplish what I set out to to do.

However, I'd also be happy with the block loading BETWEEN </Header> and <div class="breadcrumbs">. This seems more achievable having looked closer at the code.

0

To be 100% sure, you want to add new block at the the end of </header>? If yes, maybe should just add this new block at the end of header.container container?

<referenceContainer name="header.container">
    <block class="Smartwave\Porto\Block\Template" name="banner" template="Magento_Catalog::product/view/banner.phtml" before="-"/>
</referenceContainer>
  • I've just tried your solution using both before and after. Unfortunately, the block doesn't load at all. – Craig Oct 20 '18 at 19:44
  • hmm, maybe you should add another container? I mean check how it is done in the core vendor/magento/module-theme/view/frontend/layout/default.xml there you have referene to the header.container and before adding a block they add some containers - header.panel.wrapper and header-wrapper which contain blocks with templates – Sebastian Oct 20 '18 at 19:51
  • Just looking into this further now. Brain drained after such a long day, so probably gonna pick this back up in the morning. Note: I edited question based on recent findings. – Craig Oct 20 '18 at 19:55
0

I'm not sure whether I was implementing core answers incorrectly or whether the solution was purely specific to the Porto Theme. However, having realised that I couldn't add a block between the Navigational Menu and </header> (because they were hardcoded into one file) - I instead aimed to load the block directly after </header>.

I then implemented this (without the <move... line) which seems to work as expected.

<referenceContainer name="page.wrapper">
    <block class="Smartwave\Porto\Block\Template" name="banner" template="Magento_Catalog::product/view/banner.phtml" after="porto_header"/>
</referenceContainer>

I know what Widgets could serve as a potential solution, but having looked into this, the Widget block loads quite a few divs further down the page. Therefore wasn't close enough. If this wouldn't bother you, then I recommend checking out this article on stackexchange.

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.