jcadima
4/20/2015 - 9:21 PM

Get product URLs with URL rewrite

Get product URLs with URL rewrite

<?php 
//reference
// http://magento.stackexchange.com/questions/14458/product-getproducturl-giving-url-path-without-url-key

// modify this:
  $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;
        
// with this: 
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('status', 1);
$collection->addFieldToFilter(array(array('attribute'=>'visibility', 'neq'=>"1" )));
$collection->addUrlRewrite($category->getId()); //this will add the url rewrite.
$collection->getSelect()->limit(12);