Let’s initiate a discussion!!
Sometime we need to set html data without load or set it via ajax.
Then create a module name : Magemonkey:Ajaxcall
create index.phtml in Magemonkey/Ajaxcall/view/frontend/templates/
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<div id="ajaxresponsedata"></div> <?php $ajaxurl = $block->getAjaxUrl(); ?> <script type="text/x-magento-init"> { "*": { "Magemonkey_Ajaxcall/js/call": { "AjaxUrl": "<?php echo $ajaxurl; ?>", } } } </script> |
Create js file Magemonkey/Ajaxcall/view/frontend/web/js/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
define([ "jquery", "jquery/ui" ], function($){ "use strict"; function main(config, element) { var $element = $(element); var AjaxUrl = config.AjaxUrl; var CurrentProduct = productid; $(document).ready(function(){ setTimeout(function(){ $.ajax({ context: '#ajaxresponsedata', url: AjaxUrl, type: "POST", data: {currentproduct:CurrentProduct}, }).done(function (data) { $('#ajaxresponse').html(data.output); return true; }); },1000); }); }; return main; }); |
Create Index.php file in Magemonkey/Ajaxcall/Controller/Index/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php namespace Magemonkey\Custom\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\Controller\Result\JsonFactory; use Magento\Framework\View\Result\PageFactory; class View extends Action { 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('Magemonkey\Ajaxcall\Block\Index\Index') ->setTemplate('Magemonkey_Ajaxcall::index.phtml') ->setData('data',$data) ->toHtml(); $result->setData(['output' => $block]); return $result; } } |
That’s it.
[crayon-642623dcea9f8832336119/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.