1

Just started learning how to use the Mage for retrieving info from the database for a Google base feed. I have been calling the tables directly but I am not able to figure out how to get the "friendly" url.

Direct: /catalog/product/view/id/829/s/double-tier-ventilated-lockers/

Friendly: /gym-lockers/double-tier-ventilated-gym-lockers/double-tier-ventilated-lockers.html

I am using getProductUrl() for the direct url. I read something where you have to walk through the categories and relate the products to it (addUrlRewrite) for the Friendly URL, but with my limited knowledge it isn't making sense to me.

Question: How do I pass a ProductModel into a function of some sort and have it return the friendly url?

Thanks for your help

3 Answers 3

2

For those interested, create a function and pass the entity id into it:

function getProductItemUrl($productId)
{
     return Mage::getModel('core/url_rewrite')->getCollection()
            ->addFieldToFilter('product_id',$productId)
            ->addFieldToFilter('category_id',array('neq'=>null))
            ->getFirstItem()
            ->getRequestPath();
}


$producturl = getProductItemUrl($product->getId());
$producturl = Mage::getUrl().$producturl;
0

You need to reindex the product urls. Go to System > Index Management and reindex all the options that require reindexing that should give you friendly urls

0

The url rewrite part is implemented kind of poorly.
There is this method in the product model

public function getCategoryId()
{
    if ($category = Mage::registry('current_category')) {
        return $category->getId();
    }
    return false;
}

This method is somehow involved in determining the product url. This is wrong. The product model should not depend on the context.
But you can create your own method that receives as parameter the product and returns a friendly url like this:

public function getMeANiceUrl($product) 
{
    $categoryIds = $product->getCategoryIds();
    if (count($categoryIds) == 0) {
        return $product->getProductUrl();
    }
    $oldRegisteredCategory = Mage::registry('current_category');
    $obj = new Varien_Object(array('id' => $categoryIds[0]));
    Mage::unregister('current_category');
    Mage::register('current_category', $obj);
    $url = $product->getProductUrl();
    Mage::unregister('current_category');
    if ($oldRegisteredCategory) {
        Mage::register('current_category', $oldRegisteredCategory);
    }
    return $url;
}

you can write this method in your own helper. There are tons of tutorials on the web on how to create your own helper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.