
Related Products are other products that may be needed for the base product or will extend its use.
For example, Take laptops as a base product, related products may be a mouse, a keyboard, or a monitor.
If you are a Magento store owner, you must show your customers' related products to serve a better customer experience and increase your sales.
Manually doing this task can take forever. Thanks to Magento 2 Automatic Related Products. Using this module, you can display related product items on the product page.
In this blog, we will learn how you can get, add and remove related products in Magento 2.
How to get related products programmatically in Magento 2?
To get related products in Magento 2, we use Magento\Catalog\Model\Product instance.
<?php ............{ public function __construct(\Magento\Framework\App\Action\Context $context,\Magento\Catalog\Model\Product $product) {$this->product=$product;parent::__construct($context);}public function execute(){ $id=44;$product = $this->product->load($id); $relatedProducts = $product->getRelatedProducts(); if (!empty($relatedProducts)) {echo 'Related Products '; foreach ($relatedProducts as $relatedProduct) {$_product = $this->product->load($relatedProduct->getId());echo $relatedProduct->getId().'-->'.$_product->getPrice().'-->'.$_product->getName().'-->'.$relatedProduct->getId();echo "";}} }} |
In the above example, we get related product information of product id=”44”.
Here we use getRelatedProducts() method to get related products' information. now let’s see how you can add related products in Magento 2
Magento 2 add related products programmatically: Stepwise explanation
To add related products programmatically we use object manager and use Magento\Catalog\Api\Data\ProductLinkInterface instance.
Here We use setSku() method for set base product sku,setLinkedProductSku() method for related product sku and setLinkType() method for set related,upsell or cross sell products.
$productLink = $this->_objectManager->create('Magento\Catalog\Api\Data\ProductLinkInterface')->setSku('24-MB01')->setLinkedProductSku('24-MB03')->setLinkType('related'); $linkData[] = $productLink; //save product links$product = $this->_objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface')->get('24-MB01');if($product) {$product->setProductLinks($linkData)->save();} |
In above example, we have added related products(24-MB03) of base product(24-MB01). Moving on, let’s learn how you can remove related products in Magento 2.
Magento 2 remove related products programmatically: Step by Step
For remove related products in Magento 2, follow below steps:
Step 1: Get the relation type ID, in your case it's 'relation' for related products' relation by using the query:
select * from catalog_product_link_type where code='relation'; |

Step 2: Find your product IDs based on the SKUs you know you have and need the deletion of the related products associated to them:
select entity_id from catalog_product_entity where SKU in ('24-WG02'); |

Step 3: Visualize the associated products:
select * from catalog_product_link where product_id in (44) and link_type_id=1; |

Step 4: Delete the related products:
delete from catalog_product_link where product_id in (44) and link_type_id=1; |
Step 5: Check the results, you should have no related products for your product:

Lastly, Here’s how you can show related products in product view in Magento 2.
- Admin ⇾ Product(left sidebar) ⇾ Catalog
- Click on the Edit link under the Action tab.
- Go to Related Products, Up-Sells, and Cross-Sells section of the page
- Here you can select any number of products and these products will be displayed as related products.
Conclusion:
Using above blog you can easily understand How to get related products in magento 2? You can also visit our Automatic Related Product extension.If any query in above code implementation,you can contact us or let us know in comment section.