in magento how can i Get related product in the product listing page

in magento how can i Get related product in the product listing page

September 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
in magento how can i Get related product in the product listing page

Optimizing Magento Product Listing Pages: Displaying Related Products

As an experienced developer working on Magento projects, I frequently encounter questions like yours regarding displaying related and upsell products in the product listing page. In this post, I’ll guide you through the process of achieving this functionality using Magento’s built-in features.

Understanding Magento’s Cross-Selling Capabilities

Magento offers two primary cross-selling options: related products and upsell products. The key difference lies in when they are typically displayed:

  1. Related Products: These are usually shown on the product view page, providing customers with similar items that complement the currently selected product.
  2. Upsell Products: Upsells are often used to suggest higher-value or premium alternatives to the customer’s current selection.

Accessing Cross-Selling Product Collections

To start displaying these products in your listing page (product/list.phtml), you’ll first need to access their collections using Magento’s object manager.

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/* @var $crossSells \Magento\Catalog\Model\Product\Link */
$crossSells = $objectManager->create('Magento\Catalog\Model\Product\Link');
/* @var $upsellCollection \Magento\Catalog\Model\ResourceModel\Product\Link\Collection */
$upsellCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Link\Collection');

$relatedProducts = $crossSells->getProductLinks($productId, 'related');
$upsellProducts = $upsellCollection->addFieldToFilter('link_product_id', $productId)->getColumnValues('linked_product_id');
?>

In this code snippet:

  • We’re using Magento’s object manager to instantiate the required models and collections.
  • $crossSells is used to fetch related products linked to a specific product ID.
  • For upsell products, we create a collection that filters products based on their association with the current product.

Displaying Cross-Selling Products in the Listing Page

Now that you have access to these product arrays, you can iterate through them and display the necessary details. Here’s an example of how you might implement this:

<?php if (count($relatedProducts) > 0): ?>
    <div class="related-products">
        <h2>Related Products</h2>
        <?php foreach ($relatedProducts as $productId => $relatedProduct): ?>
            <div class="related-product">
                <a href="<?= $this->helper('catalog(url')->getUrl('catalog/product/view', array('id' => $relatedProduct['product_id'])) ?>">
                    <?= $relatedProduct['name'] ?>
                </a>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

<?php if (count($upsellProducts) > 0): ?>
    <div class="upsell-products">
        <h2>Upsell Products</h2>
        <?php foreach ($upsellProducts as $productId): ?>
            <div class="upsell-product">
                <a href="<?= $this->helper('catalog(url')->getUrl('catalog/product/view', array('id' => $productId)) ?>">
                    <?= $product->getName() ?>
                </a>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

In this example, we’re using a basic conditional check to ensure that the product arrays are not empty before displaying them. The code then iterates through each product ID in the collections and constructs links to their respective detail pages.

Best Practices for Displaying Cross-Selling Products

When implementing cross-selling functionality:

  1. Limit the Number of Displayed Products: To maintain a clean layout, consider limiting the number of displayed related/upsell products (e.g., show only 4-6 items).
  2. Use Product Thumbnails: Enhance your listing by including product thumbnails alongside each item name.
  3. A/B Test Different Configurations: Magento allows you to test different cross-selling configurations for individual products.

Conclusion

Displaying related and upsell products in the Magento product listing page is a straightforward process once you understand how to access these collections using Magento’s built-in models. By following the steps outlined above, you can enhance your store’s user experience by providing customers with relevant product suggestions.

Remember that Magento offers extensive documentation on this topic, which you can explore further at https://laravelcompany.com/magento-product-related-upsell-display.

Happy coding!

Catalog Performance Themes Customers

Our network