Magento getProductUrl() is not returning the right url -


i'm stuck in getting product url in product collection. have pieces of code product collection setted featured product.

 $_productcollection = mage::getmodel('catalog/product')                   ->setstoreid(mage::app()->getstore()->getid())                   ->getcollection();   $_productcollection->addattributetofilter('feat_enabled', 1);  $_productcollection->setvisibility(array(2,3,4));  $_productcollection->addurlrewrite();  $_productcollection->groupbyattribute('entity_id');  $_productcollection->load();  foreach($_productcollection $_product){          $_product->load($_product->getid());    if($_product->getissalable()){           $_name = ltrim(rtrim($_product->getname()));           $_url =  $_product->getproducturl();        } }    

what doing wrong here, if ill echo $_url in each loop, url being return lacking. doesn't have category name , subcatname. correct url should be:

index.php/category/subcategory/itemname.html

but returns

  index.php/itemname.html 

it returns corrects data except url. have double check in admin if assigned item category , subcategory , confirmed assigned it.

upon further research, have found code below close need. $_categories = $_product->getcategoryids(); $_category = mage::getmodel('catalog/category')->load($_categories[0]); $_url = mage::geturl($_category->geturlpath()).basename($_product->getproducturl()); 

the problem here, $_url value became

index.php/category/subcategory.html/itemname.html

the subcategory name has .html in it. don't need .html in url item redirected correct page when clicked.

any idea fix this?

the problem products can appear in multiple categories.

therefore, @ level, regardless of way approach it, have involve category somewhere.

so, cleanest way of doing use addurlrewrite method , pass category id. magento check rewrites matching category id , product id - if rewrite found, nice urls looking on getproducturl call. example, using category id of 10:

$collection = mage::getresourcemodel('catalog/product_collection')                 ->addurlrewrite(10); 

if knew each product appear in single category, make little more flexible using following first category id:

therefore complete example using method be:

$categoryid = array_shift($_product->getcategoryids()); $collection = mage::getresourcemodel('catalog/product_collection')                 ->addurlrewrite($categoryid); 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -