Let’s initiate a discussion!!
create phtml file at
app/code/Magemonkeys/Custom/view/frontend/templates/index.phtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<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
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 $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
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 37 38 |
<?php namespace Magemonkeys\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 { 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('Magemonkeys\Custom\Block\Index\View') ->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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php namespace Magemonkeys\Custom\Block\Index; use Magento\Framework\View\Element\Template; class View extends Template { public function __construct(Template\Context $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
1 2 3 4 5 6 7 8 |
<?php $productData = $block->getData(); $productId = $productData['data']['currentproductid']; $products = $block->getProducts($productId); ?> <div> <div>HTML codes</div> </div> |
that’s it.
Magento Commerce Cloud is Magento's brilliant move to free you...
With Magento 1’s end-of-life, so many merchants are scrambling for...
As per the recent statistics, 1.8 billion people across the...
We have mentioned some of the best strategies to boost...
As per the recent study, the majority of Magento stores...
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.