I am getting a product collection of a certain category on a page in a few different Magento sites. My code for getting the collection is:
$category = new Mage_Catalog_Model_Category();
$category->load($id);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('status', 1);
$collection->addFieldToFilter(array(array('attribute'=>'visibility', 'neq'=>"1" )));
$collection->getSelect()->limit(12);
foreach ($collection as $shopProduct) :
echo $shopProduct->getProductUrl();
endforeach;
My problem is that on one of the Magento sites we're running the ProductUrl()
being fetched is a URL like http://www.my site.com/catalog/product/view/id/2309/s/shopcat/category/373/
and not more like http://www.site.com/shopcat/product-url-key.html
. But on all other sites it's appearing as we want it.
Does anyone know why this might be? Thanks! I tried using getUrlPath()
too but this didn't return anything. I know I can get round this by doing something like <?php echo $this->getBaseUrl().$shopProduct->getUrlKey().".html"; ?>
but that method seems a bit inefficient!
EDIT 21/03/14: I'm still having this problem. I've realised getProductUrl()
retrieves the wanted URL on some template files of the site but not others. For example, I'm loading one collection on the homepage and it's giving me the URLs I want. But getProductUrl()
is not giving me the URLs I want with the same code on a category view.