15

I am able to load product collection by using

[$objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')]

in my phtml file.But in that collection product url is not there how can we get Product url also with that collection.

4
39

$productCollection = $productObj;

Gives from product collection url using loop the collection,

foreach($productCollection as $_product){ 
   echo $_product->getProductUrl();
} 
11
  • Hi @Rakesh Actually I am loading collection in product detail page using this [$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection') ->addAttributeToFilter('type_id','bundle') ->addAttributeToFilter('product_identifier',$productIdentifier);] If I print [$productCollection->getData()] it is not giving products urls. Dec 20 '16 at 11:38
  • Here my need is I want to give other product links in this current product detail page. Dec 20 '16 at 11:40
  • have you try with, $_product->getProductUrl()? Dec 20 '16 at 11:42
  • yes I tried using [$productCollection->getProductUrl()] but it is not giving any thing. Dec 20 '16 at 11:44
  • plz share your full code Dec 20 '16 at 11:47
-3

If you're using an objectmanager then try this code.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$productCollection->addAttributeToSelect('*');
foreach($productCollection as $product){
$productUrl=$product->getProductUrl();
echo $productUrl.'<br/>';
}

you can also make a product url using the url key.

foreach($productCollection as $product){
    $productUrl=$this->getUrl().$product->getUrlKey().'.html';
    echo $productUrl; 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.