We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    create phtml file at

    app/code/Magemonkeys/Custom/view/frontend/templates/index.phtml

    <div id="ajax-response-id"></div>
    <?php
    $ajaxurl = $block->getAjaxUrl();
    ?>
    <script type="text/x-magento-init">
            {
                "*": {
                    "Magemonkeys_Custom/js/custom": {
                        "AjaxUrl": "<?php echo $ajaxurl; ?>",
                        "CurrentProduct": "<?php echo $currentProductId; ?>",
                    }
                }
            }
    </script>

    create js file at

    app/code/Magemonkeys/Custom/view/frontend/web/js/custom.js

    define([
    "jquery",
    "jquery/ui"
    ], function($){
    "use strict";
    
    function main(config, element) {
    var $elementvar = $(element);
    var AjaxUrlcall = config.AjaxUrl;
    var CurrentProduct = config.CurrentProduct;
    
    $(document).ready(function(){
    setTimeout(function(){
    $.ajax({
    context: '#ajax-response-id',
    url: AjaxUrlcall,
    type: "POST",
    data: {currentproduct:CurrentProduct},
    }).done(function (data) {
    $('#ajax-response-id').html(data.output);
    return true;
    });
    },1000);
    });
    };
    return main;
    });

    Create a controller file at

    app/code/Magemonkeys/Custom/Controller/Index/View.php

    <?php
    namespace MagemonkeysCustomControllerIndex; 
     
    use MagentoFrameworkAppActionAction;
    use MagentoFrameworkAppActionContext;
    use MagentoFrameworkControllerResultJsonFactory;
    use MagentoFrameworkViewResultPageFactory; 
     
    class View extends Action
    { 
        protected $_resultPageFactory;
        protected $_resultJsonFactory;
     
        public function __construct(Context $context, PageFactory $resultPageFactory, JsonFactory $resultJsonFactory)
        { 
            $this->_resultPageFactory = $resultPageFactory;
            $this->_resultJsonFactory = $resultJsonFactory; 
            parent::__construct($context);
        }
     
        public function execute()
        {
            $result = $this->_resultJsonFactory->create();
            $resultPage = $this->_resultPageFactory->create();
            $currentProductId = $this->getRequest()->getParam('currentproduct');
             
            $data = array('currentproductid'=>$currentProductId);
     
            $block = $resultPage->getLayout()
                    ->createBlock('MagemonkeysCustomBlockIndexView')
                    ->setTemplate('Magemonkeys_Custom::view.phtml')
                    ->setData('data',$data)
                    ->toHtml();
     
            $result->setData(['output' => $block]);
            return $result;
        }
    }

    create block file at app/code/Magemonkeys/Custom/Block/Index/View.php

    <?php
    namespace MagemonkeysCustomBlockIndex;
     
    use MagentoFrameworkViewElementTemplate;
     
    class View extends Template
    {
      public function __construct(TemplateContext $context, array $data = [])
        {
            parent::__construct($context, $data);
        }
     
      protected function _prepareLayout()
        {
            return parent::_prepareLayout(); 
        }
     
      public function getProducts($productId)
      {
         return;
      }
    }

    create phtml file  at app/code/Magemonkeys/Custom/view/frontend/templates/view.phtml

    <?php
    $productData = $block->getData();
    $productId = $productData['data']['currentproductid'];
    $products = $block->getProducts($productId);
    ?>
    <div>
      <div>HTML codes</div>
    </div>

    that’s it.

     

    field_5bfb909c5ccae

      Get a Free Quote