I already try this :

public function execute(Observer $observer)
     $layout = '<referenceContainer name="main">
        <block class="C\M\Block\ModalOverlay"

1 Answer 1


You should use the built-in modal. It provides wide customization that most probably covers your needs.

Documentation about modal in Magento 2

Moreover, generating XML in PHP using an observer is not really nice pattern for Magento 2 as it happens a bit too late to have all the functionalities natively implemented for the layout. I never met a case when I needed to use observer instead of XML to update the layout.

Just for terminology :

  • Observers in Magento 2 are server-sided observer and do not observe the users interactions
  • Events are triggered in PHP not in JS

Documentation about events and observers

That means that if you want to open modal depending on some PHP thing, it's the good place but if you intend to do it on user event, you should have look at knockout observables and/or jQuery events.

If you provide more information on what you want to achieve, I can answer more specifically.


Providing that the question is specific to a payment gateway, the idea is to extend the good component. The checkout process in Magento 2 is divided into multiple steps, such as shipping address, shipping method, payment method, and order review. Each step has its own JavaScript component and template responsible for rendering that specific step's content. You will need to choose the appropriate one regarding your specificity.

Check the information about Component extension or wrapping as it is the best way to have a working JS on different versions of Magento 2. Once things are wrapped or extended, you can create the logic to open or close the modal. In your specific case, this should be done after a positive response from API. To protect your sensitive data such as secret keys or stuff like that, you should do calls on server side and manage the result on front afterward. You can create a genuine controller or create API endpoint depending on your specificity. Keep in mind that HttpGetActionInterface will work only with GET and HttpPostActionInterface with POST. It looks obvious but it worth to point it out.

Keep also in mind that KnockOut and jQuery are available along with underScore. This should permit you to achieve what you need.

As stated in comment, this is a tough task for a beginner and I hope you will get some help from colleagues as several aspects of Magento are involved. If not, ChatGPT or similar can give hints but don't relay completely on it as some of its information is outdated at the moment of writing.

  • In fact I am in the process of integrating a payment gateway with external API and this is done in three steps in the second step a one time password is sent to the user. And I have to use this password to build the last query. I therefore thought of setting up an Observer after the second request to the API given that it is done with php and display a popup with the execute() method of Observer class to recover the password and send the last request
    – CHAOS
    Sep 7, 2023 at 8:36
  • I think you can have your three steps on the same page. The first request to API is made using ajax and you provide the front with disclosable data. You would open modal when the call to API is done so the user can enter the one-time password. If the modal is provided by third-party, it's a little more tricky but I would not use PHP events for that as it is a sequence of requests-responses. Sep 7, 2023 at 9:26
  • ok Thanks ! But how to open modal when api call is done. I am new to magento.I thought about the event because it was the only method I saw
    – CHAOS
    Sep 7, 2023 at 9:39
  • Checkout is a really tough place to begin with Magento. You will need to extend the JScomponent that is responsible of display throughout the checkout process. You need to follow the idea that I described in my previous comment. If you can make direct api calls, you can do it from JS but most of the time, sensitive data is sent to payment API so you will need to create controllers. Then check how to create and manage display of modal using official doc as stated in the answer. To be honest, it's a complicated task that asks for wide knowledge of Magento. Sep 7, 2023 at 13:28

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.