Get Product Canonical URL in Magento using product id to see your current store product page Canonical URL.
Fetch Product Object by the id and use the method getUrlInStore() to retrieve URL.
Check given code to retrieve canonical URL,
<?php namespace Jesadiya\Canonical\Model; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Framework\Exception\NoSuchEntityException; use Psr\Log\LoggerInterface; class Data { /** * @var ProductRepositoryInterface */ private $productRepository; /** * @var LoggerInterface */ private $logger; public function __construct( ProductRepositoryInterface $productRepository, LoggerInterface $logger ) { $this->productRepository = $productRepository; $this->logger = $logger; } /** * get canonical url * * @param int $productId * @return string */ public function getCanonicalUrl(int $productId) : string { $canonicalUrl = null; try { $product = $this->productRepository->getById($productId); $canonicalUrl = $product->getUrlInStore(); } catch(NoSuchEntityException $exception) { $this->logger->error("Product Id not found", [$exception]) } return $canonicalUrl; } }
The Output will display the Product Canonical Url in the current store.
Using This way, You can achieve Page Canonical URL for the current store.