I am trying to fetch the product links for a product (related, cross sell, up sell), but all return as null or zero. Even though, the product links are shown on the admin UI.
1: Use ProductLinkRepositoryInterface
$prdRepo = $this->objectManager->create('Magento\Catalog\Api\ProductLinkRepositoryInterface');
if ($prdRepo) {
$links = $prdRepo->getList($product);
//This output as 0
echo 'links='; echo count($links); echo PHP_EOL;
}
2: Use ProductInterface, getProductLinks() method
$productLinks = $product->getProductLinks();
These two ways return 0. I am not sure if it is the correct way to fetch the product links.