I’ve read through several forum posts here and on other sites and even a few wikis concerning url rewrites and search engine friendly (SEF) URLs that contain full category paths to product pages. I have a feeling that I’m probably not using the correct search terms but at this point I’ve spent over a day trying to determine the best way to solve this issue so please be kind . I’m also fairly new to Magento (but enjoying it for the most part so far).
The original issue started off where getUrl/getProductUrl wasn’t returning the results for products - I wanted /category1/category2/categoryN/product-url-key.html. What I would usually get was /product-url-key.html instead. A simple search or two including “category specific product urls magento” or some variation of that returned several solutions that resembled:
Starting with the Mage_Catalog_Block_Product_View and Mage_Catalog_Model_Product objects
Then grabbing the array from getCategoryIds from Mage_Catalog_Model_Product
Getting the ID of the last (most specific) category in the array, loading it, then getting it’s path with getUrlPath
At that point appending Mage_Catalog_Model_Product‘s basename getUrlPath to the category’s url path to get /category1/category2/categoryN/product-url-key.html
Now, for the most part this works just fine. The issue I’m running into now (which admittedly shouldn’t exist if good data entry practices were in place) is when a product is saved that has a product url key which already exists in another product Magento creates some rewrite rules that are in the form product-url-key-[product id].html but only for the category specific URLs where no product with that same URL key is currently a member. For example, product with id 299 and 301 have the same URL key ‘untitled’ so URL rewrites are automatically generated as follows:
Target Path: /catalog/product/view/id/299
Request Path: /untitled.html
Target Path: /catalog/product/view/id/299/category/3
Request Path: /the-third-category-url-key/untitled.html
Target Path: /catalog/product/view/id/301
Request Path: /untitled-301.html (auto generated with “-product id” because it’s a duplicate url key yet not represented in the product’s admin edit screen)
Target Path: /catalog/product/view/id/301/category/3
Request Path: /the-third-category-url-key/untitled-301.html
Then here I run into trouble
Target Path: /catalog/product/view/id/301/category/8 (category 8 is a sub category of 3)
Request Path: /the-third-category-url-key/the-eighth-category-url-key/untitled.html (notice the lack of “-product id” at the end of the duplicate url key because it’s in a category that doesn’t already contain a product with the same url key)
So the problem ends up being that when I’m grabbing getUrlPath from the product object it’s unaware that I want the URL path for that product within that category/subcategory so it’s giving me the untitled-[product id].html which represents the product’s URL from the root. In these instances, the method at the top of this post that gets the product’s category URL then appends it with the product’s URL key creates a link doesn’t work (404 error). What I’m looking for is a better solution that takes this scenario into account.
Thanks in advance and sorry for the verbose post!