This article shows how we can get related products, upsell products, and cross-sell products in Magento 2.
Check the product relation tables in MySQL database
mysql> SELECT * FROM `catalog_product_link_type`;
+--------------+------------+
| link_type_id | code |
+--------------+------------+
| 1 | relation |
| 3 | super |
| 4 | up_sell |
| 5 | cross_sell |
+--------------+------------+
4 rows in set (0.00 sec)
In the above query result,
1 = related products
4 = upsell products
5 = cross-sell products
mysql> SELECT * FROM `catalog_product_link_attribute`;
+---------------------------+--------------+-----------------------------+-----------+
| product_link_attribute_id | link_type_id | product_link_attribute_code | data_type |
+---------------------------+--------------+-----------------------------+-----------+
| 1 | 1 | position | int |
| 2 | 4 | position | int |
| 3 | 5 | position | int |
| 4 | 3 | position | int |
| 5 | 3 | qty | decimal |
+---------------------------+--------------+-----------------------------+-----------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM `catalog_product_link`;
+---------+------------+-------------------+--------------+
| link_id | product_id | linked_product_id | link_type_id |
+---------+------------+-------------------+--------------+
| 3 | 4 | 3 | 1 |
| 5 | 5 | 3 | 1 |
| 8 | 6 | 3 | 1 |
| 6 | 5 | 4 | 4 |
| 10 | 6 | 4 | 4 |
| 9 | 6 | 5 | 5 |
+---------+------------+-------------------+--------------+
6 rows in set (0.00 sec)
Create a class to get current product
app/code/Chapagain/HelloWorld/Block/HelloWorld.php
<?php
namespace Chapagain\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
protected $registry;
protected $productRepositoryInterface;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryInterface,
array $data = []
)
{
$this->registry = $registry;
$this->productRepositoryInterface = $productRepositoryInterface;
parent::__construct($context, $data);
}
public function _prepareLayout()
{
return parent::_prepareLayout();
}
public function getCurrentProduct()
{
return $this->registry->registry('current_product');
}
public function getProductById($productId)
{
return $this->productRepositoryInterface->getById($productId);
}
}
?>
Load the product
If on product detail page:
$product = $block->getCurrentProduct();
Otherwise:
$product = $block->getProductById($productId);
Get Related Products & Collection
Get related products array
$relatedProducts = $product->getRelatedProducts();
Get related product IDs array
$relatedProductIds = $product->getRelatedProductIds();
Get related product collection
// \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection
$relatedProductCollection = $product->getRelatedProductCollection();
Get the related collection link
// \Magento\Catalog\Model\ResourceModel\Product\Link\Collection
$relatedProductCollectionLink = $product->getRelatedProductCollection();
Get Upsell Products & Collection
Get upsell products array
$upsellProducts = $product->getUpsellProducts();
Get upsell product IDs array
$upsellProductIds = $product->getUpsellProductIds();
Get related product collection
// \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection
$upsellProductCollection = $product->getUpsellProductCollection();
Get the upsell collection link
// \Magento\Catalog\Model\ResourceModel\Product\Link\Collection
$upsellProductCollectionLink = $product->getUpsellProductCollection();
Get Cross-sell Products & Collection
Get cross-sell products array
$crossSellProducts = $product->getCrossSellProducts();
Get cross-sell product IDs array
$crossSellProductIds = $product->getCrossSellProductIds();
Get cross-sell product collection
// \Magento\Catalog\Model\ResourceModel\Product\Link\Product\Collection
$crossSellProductCollection = $product->getCrossSellProductCollection();
Get the cross-sell collection link
// \Magento\Catalog\Model\ResourceModel\Product\Link\Collection
$crossSellProductCollectionLink = $product->getCrossSellProductCollection();
For more, see:
vendor/magento/module-catalog/Model/Product.php
Hope this helps. Thanks.