0

I added a button in block which hits custom controller (I am not using any template)

This is controller code:

<?php
namespace Vtn\UnexportedOrder\Controller\Adminhtml\Order;

use Magento\Framework\Controller\ResultFactory; 

class Export extends \Magento\Backend\App\Action
{
    const ADMIN_RESOURCE = 'Vtn_UnexportedOrder::export';

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $_pageFactory;

    /**
     * @param \Magento\Backend\App\Action\Context $context
     */
    public function __construct(
       \Magento\Sales\Api\OrderItemRepositoryInterface $orderRepo,
       \Magento\Backend\App\Action\Context $context,
       \Magento\Framework\Json\Helper\Data $jsonHelper,
       \Magento\Framework\View\Result\PageFactory $pageFactory
    )
    {
        $this->jsonHelper = $jsonHelper;
        $this->orderRepo = $orderRepo;
        $this->_pageFactory = $pageFactory;
        return parent::__construct($context);
    }

    /**
     * Index action
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        $itemId = $this->getRequest()->getParam('itemid');
        $item = $this->orderRepo->get($itemId);

        if ($this->getRequest()->getParam('itemid')):
            $itemId = $this->getRequest()->getParam('itemid');
            $item = $this->orderRepo->get($itemId);
            $data = $item->getName()." is already exported";
            $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
            $resultJson->setData($data);
            return $resultJson;
        endif;
    }
}

For now it displays products name on controller url (frontname/controller/export). But I want to display it on same controller where button exists using ajax call and display this product name in popup on this controller url (frontname/controller/view)

2
  • So, do you already have a template, where the button is integrated (basically a phtml file, where the button gets inserted)? – Mario Apr 12 at 20:23
  • Actually I am using any template I just add button dynamically from block – Arun Kumar Apr 13 at 3:24
1

You can add a phtml file inside your static cms-block:

{{block class="<vendor_name>\<module_name>\Block\<module_name>" name="<your_block_name>" template="<vendor_name>_<module_name>::<template_directory>/filename.phtml"}}

Inside of the phtml-file, you can add javascript and the logic to call your script via ajax:

<button data-trigger="openmymodal">Click me!</button>
<div id="your-modal-div"></div>
    <script>
      require(
               [
                   'jquery',
                   'Magento_Ui/js/modal/modal'
               ],
               function (
                   $,
                   modal
               ) {
    
                     var modaloption = {
                       type: 'popup',
                       modalClass: 'modal-popup',
                       responsive: true,
                       innerScroll: true,
                       clickableOverlay: true,
                       title: $.mage.__('Popup-Title'),
                       buttons: [{
                          text: $.mage.__('Close'),
                          class: '',
                          click: function () {
                              this.closeModal();
                          }
                      }],
                      trigger: '[data-trigger=openmymodal]'
                   };
                      var modelcreate = modal(modaloption, $('#your-modal-div'));
                      var sku="123";
    
                   $.ajax({
                      method: "POST",
                      url: "/frontname/controller/export",
                      data: { itemid: sku},
                      dataType: "json",
                      async: true,
                      success : function(response){                    
                        console.log(response);
                        $('#your-modal-div').append(response);

                    
                      }
                    });
               });
    </script>

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.