I created a custom page, where users can input SKU of product and after they submit the button, its load product details, like name, image and a configurable option for configurable product.

Here is the issue: I get the SKU from the input field, but product details are not loading on my view page, or details not successfully sent from the controller to view(Actually from block to view, as some page suggested).

I am following the answer: Magento 2 - Send form data to Controller using AJAX and return Result to PHTML

File: viewproduct.phtml



<form data-mage-init='{"validation": {}}' id="load_product">
   <div class="control">
       <input type="text" name="get_product" id="get_product" class="input-text" placeholder="<?= __('Product SKU') ?>" data-validate="{required:true}" />
   <button type="submit" class="action-primary"><?= _('Submit') ?></button>

echo $test = $block->getProductBySku();
//var_dump($test); exit();

    jQuery(document).ready(function() {

            var getProduct = jQuery("input[name='get_product']").val();

            var url = "<?php echo $block->getBaseUrl().'AddBulkProduct/loadproduct/Index/' ?>";
                url: url,
                type: "POST",
                data: {get_product:getProduct},
                showLoader: true,
                cache: false
                success: function(response){
            return false;

File: viewproduct_loadproduct_index.xml



    <referenceContainer name="content">
        <block class="SimpleMagento\AddBulkProduct\Block\Index"  template="SimpleMagento_AddBulkProduct::viewproduct.phtml">


File: Index.php (Controller)



public function execute()
    $getProduct = $this->getRequest()->getParam('get_product');
    $result = $this->jsonFactory->create();
    $resultPage = $this->pageFactory->create();
    $block = $resultPage->getLayout()
    $result->setData(['output' => $block]);
    return $resultPage;

File: Index.php (Block)



class Index extends Template
   * @var ProductRepositoryInterface
    protected $productRepository;

    public function __construct(Template\Context $context, ProductRepositoryInterface $productRepository)
        $this->productRepository = $productRepository;

    public function getProductBySku($sku)
       return $this->productRepository->get($sku);

Current Situation: As a new in Magento, Please tell me if I am using a correct flow or not?

As per my understanding, I am using the block to pass data from controller to my phtml file, but after using block, Magento's default layout is not showing just basic html input text and button, How can I restore my layout?

Currently after press submitting button, its just reloading with '?get_product=24-MB01' url.

Any help or suggestion would be appreciated.


1 Answer 1


It's not showing you anything because, you didn't pass sku in getProductBySku() in this function in phtml file.

As I understand your requirement, you don't need to create function in block and pass to phtml file.

You just pass data from controller when ajax request send. Like this below way :

$block = $resultPage->getLayout()

and get data in phtml using this below code :


Hope, It will helpful for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.