tags:

views:

22

answers:

1

Hi Guys,

I have created a custom module in magento and wants to show products with their URL as links.

I am trying in this way :-

 for ($counter=0; $counter < count($products); $counter++)
{  
 $_product =    Mage::getModel('catalog/product')->loadByAttribute('id',$products[$counter]->product_id);
  echo $_product->getProductUrl();
}

but the $_product->getProductUrl() function always return a url which is not related to none of the loaded products.

Can you guys inspect it and let me know what I am missing?

Thanks.

+1  A: 

You are using loadByAttribute('id', ...) but the attribute that identifies a product (and most other entities) is entity_id. A shorter, safer method is just load(...). An even shorter method is this:

echo Mage::helper('catalog/product')->getProductUrl($products[$counter]->product_id);
clockworkgeek
Many Thanks it's worked. I am in another problem now. on this same custom module I want to check the availability of product but It always return true even if I set the out of stock for the product I am checking like this $_product = Mage::getModel('catalog/product')->loadByAttribute('id',$products[$counter]->product_id); $_product->isSaleable(); Please your quick help needed. Thanks
Shakti Singh
Soory, Such a stupid comment. Your answer is worked for me perfectly. I want to give point to your answer but I can't. Thanks alot
Shakti Singh