Immediate Magento Product URLs

Magento seems to have several different formats that work for product URLs, including:

/catalog/product/view/id/123/s/my-product-slug/ /my-product-slug/ /top-category/sub-category/my-product-slug/ etc... 

I just want to use one of them, always the third one, to concatenate all the ingots of the parent categories together before launching the product. Is there a way to configure Magento to use only one of these formats?

Using methods like getProductUrl() seems to produce different results depending on which page / context you are on, can someone tell me why this is?

In general, does anyone know of any docs or blog posts, etc. that can help me understand that routing in Magento is a little better?

Thanks!

0
url php magento
source share
3 answers

clockworkgeek was pretty close and brought me to my answer ...

I tried using $product->setCategoryId() and found that $product->getCategoryId() did not return anything and that the Mage_Catalog_Model_Product_Url::getUrl method depends on the getCategoryId method of the getCategoryId model to determine the product URL. It turns out getCategoryId explicitly defined in the product model (and not the magic method, as I expected), which returns the value Mage::registry('current_category') . So basically I registered and unregistered "current_category" to meet my needs.

This still leaves my question partially unanswered. I figured out how to hack and get the URL that I wanted, but the technically messy URLs /catalog/product/view/id/123/s/my-product-slug/ types still work, and I would prefer that all kinds of URLs were routed to a category. It would be great to define one, canonical style of URL in Magento. If anyone has a deeper understanding of this, let me know!

+3
source share

To force this URL, I'm sure your product should include a category identifier.

 $product->setCategoryId(7)->getProductUrl(); 

Of course, 7 is just an example. A product can belong to many categories, so you have to decide how to choose it, perhaps by taking the first one.

 list($categoryId,) = $product->getCategoryIds(); $product->setCategoryId($categoryId)->getProductUrl(); 
+2
source share

I'm not sure if this is what you need and I can’t check right now, but try the following:

 $product_url = "http://www.domain.com/".$product->getUrlPath(); 
0
source share

All Articles