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.
If you are facing error as per the below, Then...
I've to add custom CSS for particular store on checkout...
If we use the quote collectTotal() method in our custom...
If you want to set a different look for a...
I have recently faced an issue during CSV import as...