{"id":896,"date":"2020-03-22T10:12:11","date_gmt":"2020-03-22T10:12:11","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/03\/22\/get-related-products-collection-in-magento-2\/"},"modified":"2025-05-22T14:41:38","modified_gmt":"2025-05-22T09:11:38","slug":"get-related-products-collection-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-related-products-collection-in-magento-2\/","title":{"rendered":"How To Get Related Products Collection In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">E-commerce product page best practices include displaying&nbsp;<a href=\"https:\/\/experienceleague.adobe.com\/en\/docs\/commerce-admin\/catalog\/products\/settings\/related-products-up-sells-cross-sells#related-products\" target=\"_blank\" rel=\"noreferrer noopener\">related products<\/a>&nbsp;on the product page. It encourages shoppers to purchase those related products and hence increase in the average order value!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Such conversion boosters positively affect the business and hence Magento 2 store owners can implement it using the below code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>Get related products collection in Magento 2<\/strong><\/em>&nbsp;store and allow the shoppers to add the items to the cart easily!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to get related products collection in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<em><strong>Extension.php<\/strong><\/em>&nbsp;file at&nbsp;<strong>app\/code\/Vendor\/Extension\/Block<\/strong>&nbsp;folder<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Extension\\Block;\n\nuse Magento\\Framework\\View\\Element\\Template;\nuse Magento\\Backend\\Block\\Template\\Context;\nuse Magento\\Framework\\Registry;\n\nclass Extension extends Template\n{\n    protected $registry;\n    \n    public function __construct(\n        Context $context,        \n        Registry $registry,\n        array $data = []\n    )\n    {        \n        $this->registry = $registry;\n        parent::__construct($context, $data);\n    }\n    \n    public function _prepareLayout()\n    {\n        return parent::_prepareLayout();\n    }\n    \n    public function getCurrentProduct()\n    {        \n        return $this->registry->registry('current_product');\n    }    \n    \n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Call the below function in your&nbsp;<strong>pHTML<\/strong>&nbsp;file<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$currentProduct = $block->getCurrentProduct();\n \nif ($currentProduct = $block->getCurrentProduct()) {\n    $relatedProducts = $currentProduct->getRelatedProducts();\n    \n    if (!empty($relatedProducts)) {\n        foreach ($relatedProducts as $relatedProduct) {\n            echo $relatedProduct->getName().'&lt;br>';\n        }\n    }    \n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please share the solution with the Magento community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>E-commerce product page best practices include displaying&nbsp;related products&nbsp;on the product page. It encourages shoppers to purchase those related products and hence increase in the average&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-896","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=896"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/896\/revisions"}],"predecessor-version":[{"id":15016,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/896\/revisions\/15016"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}