Magento 1 – get related products
In this tutorial I show you how to get related products. Related products are very important, because with it you can increase order totals. There are other techniques like upsell or crosssell. In this article I concentrate on related products, how to use them and how to increase sales.
Magento 1 – get related products
In you Magento 1 backend product detail site you have a tab for related products. There you can assign other products to a main product. The normal use is to assign for example batteries or replacement parts for a technical gadget like a quad-copter. If you display those products on details site and during checkout or in your cart, the chance is high, that a customer also buys this products. Another very effective way to use it is a bundle product with reduced prices, but this is another story…
You get a list of related product ids from a product model and print all skus the following way:
1 2 3 4 5 6 | $relatedProducts = $_product->getRelatedProductIds(); if(count($relatedProducts) > 0) { foreach ($relatedProducts as $product) { echo $product->getSku()."\n"; } } |
It is pretty easy to do so. In your *.phtml files you may use this technique to display a slider with this products.
Set related products manually
You can manually assign related products. For this go to product detail page and use “Related Products” tab. For a new product you will see an empty grid. Click on “Reset Filter” – you will see all products available. You can now filter by name, sku or other attributes and assign products by checking its checkbox.
Set related products programmatically
You can set this data very easy by array. A sample array to assign may look like the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | $relatedProductData=array( $relatedProductId1 => array( 'position'=> 0 ), $relatedProductId2 => array( 'position'=> 1 ), $relatedProductId3 => array( 'position'=> 2 ), ); $product->setRelatedLinkData($relatedProductData); $product->save(); |
Now all product ids from this array are assigned to product. We also set a defined sequence.
Database
Magento stores related products in catalog_product_relation table that looks like this:
Conclusion
I showed you detailed information on how to get related products in Magento 1 working. You can assign them by code or manually. See my Magento 2 articleon how to import related products programmatically.