Ajax in magento (load product view block)

Ajax in magento (load product view block)

July 5, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Ajax in magento (load product view block)

Loading Product View Block via Ajax in Magento

As a senior developer specializing in Magento development, I frequently encounter challenges related to loading product view blocks using Ajax. In this post, we’ll dive into the common issue of opening a pop-up with full product information when clicking on a product link or image within specific areas.

The Problem: Loading Default Product View Block via Ajax

The scenario is straightforward - you want users to be able to click on a product and have a pop-up appear with all the contents of the product view page. However, loading the default ‘product.info’ block using Magento’s built-in functions can lead to infinite loops that consume excessive processor resources.

Why Your Current Approach Isn’t Working

Your current attempts at solving this issue are quite commendable. You’ve successfully created a module, links, templates, and even an Ajax controller to access the product information. However, your approach faces two main challenges:

  1. Setting the Product ID: The default blocks in Magento don’t automatically know which product to load when accessed via an Ajax call. They rely on the context of the current page or category.

  2. Loading the Block Correctly: Loading a block directly using $layout->createBlock('product.info') isn’t enough because it doesn’t account for the necessary dependencies and layout generation required by Magento’s architecture.

The Solution: A Step-by-Step Guide

To achieve your desired outcome, we need to follow these steps:

  1. Override the Catalog Controller

    First, you should override the catalog_product_view controller in your module. This will allow us to intercept the request and handle it appropriately.

     namespace YourNamespace\YourModule\Controller\Catalog;
    
     class Product extends \Magento\Framework\App\Action\Action
     {
         public function execute()
         {
             // ... (code remains mostly unchanged, but we'll add our custom logic)
         }
     }
    
  2. Set the Product ID in the Layout

    To ensure the correct product is loaded, you need to set the catalog_product_view_id parameter in your layout XML.

     <referenceBlock name="product.info">
        <action method="setProductId">
            <argument name="id" xsi:type="string">2</argument> <!-- Replace 2 with the desired product ID -->
        </action>
     </referenceBlock>
    
  3. Generate and Return the Layout Output

    Now, let’s handle the Ajax request in our overridden controller:

     namespace YourNamespace\YourModule\Controller\Catalog;
    
     class Product extends \Magento\Framework\App\Action\Action
     {
         public function execute()
         {
             $productId = (int)$this->getRequest()->getParam('id');
             if ($productId) {
                 // Set the product ID in the layout
                 $layout = $this->pageConfig->getLayout();
                 $update = $layout->getUpdate();
                 $update->load('catalog_product_view');
                 $update->addHandle('catalog_product_view_' . $productId);
                 $update->setArea('frontend');
    
                 // Generate and return the layout output
                 $layout->generateXml();
                 $layout->generateBlocks();
                 $output = $layout->getOutput();
    
                 $this->getResponse()->setBody($output);
             }
         }
     }
    

Final Thoughts

Loading the default ‘product.info’ block via Ajax in Magento isn’t straightforward due to the framework’s architectural constraints. However, by overriding the controller and properly setting up the layout generation, you can achieve your desired outcome of opening a pop-up with full product information.

Remember that this solution is specific to your use case. Depending on your requirements, you might need to adjust the code or explore other solutions offered by Magento extensions like Lofamage’s Ajax Cart.

Don’t hesitate to reach out if you have any questions or need further assistance with your Magento development projects. At Laravel Company, we’re always ready to help!

Catalog Extensions Themes Configuration

Our network