In Magento 2, I trying to get the Product URL using this code $_product->getProductUrl ()
. But it is having only the first product's url to all the products. How to fix it.
Used the Following Code:
$_collection = $this->objectManager->get ( '\Magento\Catalog\Model\Product' )->getCollection ();
$_collection = $collection->getData();
foreach ( $_collection as $_product ) {
$_product = $this->objectManager->get ( 'Magento\Catalog\Model\Product' )->load ( $_product['entity_id'] );
echo $product_url = $_product->getProductUrl ();
}
Best Answer
Use this code :
=> Using object manager :
=> Using Factory Method :
=> Get url by product Id :
Then you load the product based on the product id:
Note : Don't use object manager directly