0

I want to add a warning message on the product page but it's not working if full page cache is enabled. I'm using the following event

<event name="controller_action_predispatch_catalog_product_view">
        <observer name="product_view_custom_action" instance="My\Module\Observer\Custom"/>
</event>

And in observer execute I have the following

$this->messageManager->addWarningMessage(__('My warning message'));

Is there a method that I could do this to show the warning message every time. Even after the page is cached?

  • We can use Ajax. – Khoa TruongDinh Mar 22 '18 at 16:18
  • Hi @KhoaTruongDinh, could you point me to an example? Thank you. – Magarusu Mar 26 '18 at 12:31
  • @Magarusu Did you able to solve this ? – Vinoth Jun 12 '18 at 7:25
  • @Vinoth Yes, I forgot to update the question. I've added my solution, hope it helps. – Magarusu Jun 13 '18 at 8:27
0

you can use magento event http_response_send_before.

  • 'http_response_send_before' does not work and I can't seem to find this dispatch in the code. I've tried a plugin on 'Magento\Framework\App\Response\Http::beforeSendResponse' and it does not work either. The code is called every time but only when I hit an uncached page will the message be show. And it's shown for every time it got triggered. Meaning, if I refresh a cached page severl times, when I hit an uncached one it will show my message for each previous hit. – Magarusu Mar 23 '18 at 9:26
0

I've managed to achieve this using a template defined under: My/Module/view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceContainer name="page.messages">
        <block class="My\Module\Block\Messages" name="CUSTOM_ID" as="CUSTOM_ID"
               template="My_Module::messages.phtml" after="messages"/>
    </referenceContainer>
</page>

And then in your phtml file:

<div role="alert" class="messages">
    <div class="message-warning warning message">
        <div><?php echo $block->escapeHtml(__('My warning message')) ?></div>
    </div>
</div>

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.