0

How can I call a .phtml file in an Observer?

I want to show a popup window when event Observer fires after customer login so I can call a .phtml file that contains HTML popup?

So after a customer has logged-in a popup appears on the screen that contains the string text.

3
  • which observer you want to call .
    – Amit Bera
    May 23, 2018 at 7:55
  • i want to call .phtml file from customerLoggedin observer @AmitBera
    – Hiren
    May 23, 2018 at 7:57
  • @Hiren, you got the any solution for that? Because I need solution for same question. Jul 16, 2021 at 5:53

2 Answers 2

1

The use of customer_login event for the block/template is not the proper idea.

The best idea in my view,

  • Create a cookie variable on event customer_login.
  • Then render your block HTML output if that you have that cookie variable after redirecting from loginpost.

Observer class:

<?php
namespace {Vendor}\{Modulename};

use Magento\Framework\Event\ObserverInterface;
class AssigncookieVariable  implements ObserverInterface
{
    /**
     * Cookie key 
     */
    const COOKIE_NAME = 'customer-just-login';
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @var \Magento\Framework\Stdlib\CookieManagerInterface
     */
    protected $cookieManager;

    /**
     * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
     */
    protected $cookieMetadataFactory;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
        \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
    )
    {
        $this->storeManager = $storeManager;
        $this->cookieManager = $cookieManager;
        $this->cookieMetadataFactory = $cookieMetadataFactory;
    }
  public function execute(\Magento\Framework\Event\Observer $observer)
  {

        $metadata = $this->cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setPath(self::COOKIE_PATH)
            ->setHttpOnly(false);
        $this->cookieManager->setPublicCookie(self::COOKIE_NAME, $cookieValue, $metadata);
    }

}    

Stop block from output render when cookie value does not exit

<?php
namespace {Vendor}\{Modulename}\BlocK;


class {YourBlockclass} ....
{
    /**
     * Cookie key 
     */
    const COOKIE_NAME = 'customer-just-login';
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @var \Magento\Framework\Stdlib\CookieManagerInterface
     */
    protected $cookieManager;

    /**
     * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
     */
    protected $cookieMetadataFactory;

    public function __construct(
       ......
        \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
        \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
        ....
    )
    {
        ....
        $this->cookieManager = $cookieManager;
        $this->cookieMetadataFactory = $cookieMetadataFactory;
    }

    protected function _toHtml() {
         if(!{CookieDoesnotExits}){
            return '';
        }
        return parent::_toHtml();
    }

}

Call block via default.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">
    <body>
        <referenceContainer name="{referenceBlockname}">
            <block class="{YourBlockClass}" name="{blocknameIn Layout}" 
            cacheable = "false"
            template="{template}"/>
        </referenceContainer>
   </body>
</page>
0

Add DI in observer's construct function like below :

public function __contruct(\Magento\Framework\View\Element\Context $context)
{
   $this->_layout = $context->getLayout();
}

In execute function, add this code:

if(condition)
{
  $this->_layout->createBlock('Vendor\Module\Block\Ex')->setTemplate('Vendor_Module::path/to/template.phtml')->toHtml();
}

This may work, not sure though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.