How To

Magento 2: How to Open Popup on Button Click in Admin

Hello Magento Friends,

In today’s blog, I will explain How to Open Popup on the Button Click in Magento 2 Admin.

Magento 2, the popular e-commerce platform, provides extensive customization capabilities for both the front end and back end. One such requirement might be the need to open a popup window in the admin panel upon clicking a button, enabling the display of additional information or performing specific actions without navigating to a different page. In this article, we’ll explore how to achieve this functionality in the Magento 2 admin panel.

Steps to Open Popup on Button Click in Magento 2 Admin:

Step 1: First, we need to create a system.xml file inside our extension at the following path

app\code\Vendor\Extension\etc\adminhtml\system.xml

Then add the code as follows

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Backend/etc/system_file.xsd">
    <system>
        <section id="customsection" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Custom Popup</label>
            <resource>Vendor_Extension::custom_config</resource>
            <group id="general" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <field id="sents" translate="label" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>CustomPopup</label>
                    <frontend_model>Vendor\Extension\Block\Adminhtml\System\Config\Custompopup</frontend_model>
                </field>
            </group>
        </section>
    </system>
</config>

Step 2: After that, we need to create a Custompopup.php file inside our extension at the following path

app\code\Vendor\Extension\Block\Adminhtml\System\Config\Custompopup.php

And add the code as given below

<?php

namespace Vendor\Extension\Block\Adminhtml\System\Config;

class Custompopup extends \Magento\Config\Block\System\Config\Form\Field
{
    protected $_template = 'system/config/custompopupbutton.phtml';

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }

    public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
    {
        $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
        return parent::render($element);
    }

    protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
    {
        return $this->_toHtml();
    }

    public function getButtonHtml()
    {
        $button = $this->getLayout()->createBlock(
            'Magento\Backend\Block\Widget\Button'
        )->setData(
            [
                'id' => 'custompopupbutton',
                'label' => __('Custom Popup'),
                'class' => 'primary'
            ]
        );

        return $button->toHtml();
    }
}

Step 3: After that, we need to create a downloadlinkbutton.phtml file inside our extension at the following path.

app\code\Vendor\Extension\view\adminhtml\system\config\downloadlinkbutton.phtml

Then add the below-mentioned code

<div id="messages">
       <div class="message"> MessageContents 
           <button class="primary">ClickMe</button>
       </div>
</div>
<script type="text/javascript">// <![CDATA[
require([
        'jquery',
        'Magento_Ui/js/modal/alert'
    ],
    function($, alert) {
       $('#messages').on('click', 'button.primary', function(event){
            alert({
               content: $(event.target).parent().text()
            })
       })
    }
);
// ]]></script>
</div>

Conclusion:

Integrating a popup window in the Magento 2 admin panel can significantly enhance user interaction and streamline administrative tasks. By following the steps outlined in this article, you can effectively open a popup window on a button click, allowing you to display relevant information or perform specific actions without navigating to a different page. Leveraging this functionality can contribute to a more efficient and user-friendly Magento 2 admin panel experience.

If you find it difficult to perform these steps, Magento 2 experienced developers can help you with that.

Happy Coding!

Click to rate this post!
[Total: 1 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

2 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

3 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

4 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

6 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago