$_product->getProductUrl() giving url path without URL key

Try to get the collection like this:

$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('status', 1);
$collection->addFieldToFilter(array(array('attribute'=>'visibility', 'neq'=>"1" )));
//Where the magic happens
//this will add the url rewrite.
//addUrlRewrite can also be left without a parameter to generate url without category.
$collection->addUrlRewrite($category->getId()); 
$collection->getSelect()->limit(12);

In other words, let the model know to give the url key instead of the long ugly url with $collection->addUrlRewrite();.


Getting A Products URL

Potentially confusing due to the 3 methods you could use, all of which are in Mage_Catalog_Model_Product:

public function getUrlPath($category=null)
public function getUrlInStore($params = array())
public function getProductUrl($useSid = null)

The best way to explain is to simply show the results of several calls. Given a product whose URL key is mondrian-large-coffee-table-set-multicolour on the domain of http://made.local the results are:

$product->getUrlPath();
    'mondrian-large-coffee-table-set-multicolour'

$product->getUrlPath($category);
    'tables/mondrian-large-coffee-table-set-multicolour'

// you cannot stop this method adding ___store to the URL, even by setting _store_to_url to false
$product->getUrlInStore();
    'http://made.local/tables/mondrian-large-coffee-table-set-multicolour?___store=default'

// you cannot stop this method adding ___store to the URL, even by setting _store_to_url to false
// note - see the "using _ignore_category" section below for an arguable bug with using this param
$product->getUrlInStore(array('_ignore_category' => true));
    'http://made.local/mondrian-large-coffee-table-set-multicolour?___store=default'

$product->getProductUrl();
    'http://made.local/tables/mondrian-large-coffee-table-set-multicolour'

$product->getProductUrl(true);
    'http://made.local/tables/mondrian-large-coffee-table-set-multicolour'

Tags:

Url

Products