How to display related products in Ajax pop up in Magento

How to display related products in Ajax pop up in Magento

October 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to display related products in Ajax pop up in Magento

Displaying Related Products in an Ajax Pop Up in Magento

Are you looking to enhance your Magento shopping experience by displaying related products within an Ajax pop up? You’re on the right track! Let’s dive into how you can achieve this.

Understanding the Current Setup

You’ve already set up an impressive core structure for your popup. Here’s what we have so far:

  • A product image and message displayed using Magento’s built-in image helper.
  • Navigation links to continue shopping or proceed to checkout.

The code looks like this:

<?php
$product = Mage::getModel('catalog/product')->load($this->getRequest()->getParam('product'));
$message = $this->__('<b>%s</b> is successfully added to your Shopping Basket.', $product->getName());
?>
<div id="ajax_image">
  <img src='<?php echo Mage::helper('catalog/image')->init($product, 'image')->resize(60,null)?>' />
  <span><?php echo $message ?></span>
</div>
<div class="row2">

<a class="back" id="closeLink" href="javascript:void(0);"><span><?php echo $this->__('Continue Shopping') ?></span></a>
<a class="next" href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK).'checkout/cart/' ?>"><span><?php echo $this->__('Checkout') ?></span></a>
</div>

You’ve attempted to include related products using a foreach loop. You’re retrieving the related products and iterating through them, but you’re not displaying all the necessary information.

Here’s how you can modify your code:

<div>
  <?php $related= $product->getRelatedProducts(); ?>
    <?php foreach($related as $_item): ?>

  <ul class="mini-products-list" id="block-related">
        <li class="item">

    <?php if(!$_item->isComposite() && $_item->isSaleable()): ?>
                <?php if (!$_item->getRequiredOptions()): ?>

                <?php endif; ?>
            <?php endif; ?>

            <div class="product">
                <a href="<?php echo $_item->getProductUrl() ?>"><img src="<?php echo Mage::helper('catalog/image')->init($_item, 'image') ?>" width="80" height="80" /></a>
        <div class="product-details">
                    <p class="product-name">
        <a href="<?php echo $_item->getProductUrl() ?>"><?php echo $this->htmlEscape($_item->getName()) ?></a></p>
                     <p class="sku"><?php echo $_item->getSku()?></p>

                </div>
            </div>
        </li>

    <?php endforeach ?>
  </ul>
</div>

Incorporating Product Images and Names

You mentioned that the product images aren’t being displayed correctly. The issue lies in how you’re trying to get the image URL.

To display the product’s thumbnail or small image, use Magento’s built-in image helper:

<img src="<?php echo Mage::helper('catalog/image')->init($_item, 'image') ?>" width="80" height="80" />

This will fetch and resize the product’s main image to a 80x80 dimension.

Displaying Product Names

For the product name, you’re already using htmlEscape($_item->getName()), which is the correct approach. This ensures that the name is properly escaped for HTML output.

Displaying Prices and Add-to-Cart Button

To display both the normal and special prices, you can use the following code:

<p class="price">
  <?php if($_item->getFinalPrice() != $_item->getPrice()): ?>
    <span class="regular-price"><?php echo $this->formatCurrency($_item->getPrice()) ?></span>
    <br/>
    <span class="special-price"><?php echo $this->formatCurrency($_item->getFinalPrice()) ?></span>
  <?php else: ?>
    <span class="regular-price"><?php echo $this->formatCurrency($_item->getPrice()) ?></span>
  <?php endif; ?>
</p>

This will display the special price if it exists, otherwise it will just show the regular price.

For the Add to Cart button, you can use:

<button type="button" class="btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_item) ?>')"><span><span>Add to Cart</span></span></button>

Conclusion

Displaying related products in a Magento Ajax pop up is a great way to enhance the user experience. By using Magento’s built-in helpers and methods, you can easily fetch and display relevant product information.

Remember to test your code thoroughly on different browsers and devices to ensure compatibility and responsiveness.

If you need more advanced customizations or have any other questions about Magento development, feel free to reach out to us at Laravel Company.

Happy coding!

Checkout Catalog JavaScript

Our network