Magento 一致的产品 URL
Magento 似乎有许多不同的适用于产品 URL 的格式,包括:
/catalog/product/view/id/123/s/my-product-slug/
/my-product-slug/
/top-category/sub-category/my-product-slug/
etc...
我总是想只使用其中一种格式,即第三种格式,将所有父类别 slug 在产品 slug 之前连接在一起。有没有办法将 Magento 配置为仅使用其中一种格式?
使用像 getProductUrl()
这样的方法似乎会返回不同的结果,具体取决于您所在的页面/上下文,有人能告诉我这是为什么吗?
一般来说,有谁知道任何文档或博客文章等,可以帮助我更好地理解 Magento 中的路由?
谢谢!
Magento seems to have a number of 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 want to just use one of these, always, which is the 3rd one, concatenating all parent category slugs together before the product slug. Is there a way to configure Magento to only use one of these formats?
Using methods like getProductUrl()
seems to return different results depending on what page/context you're on, can anyone tell me why this is?
And in general does anyone know of any documentation or blog posts, etc., that might help me understand routing in Magento a little better?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Clockworkgeek 非常接近并引导我找到了答案...
我尝试使用
$product->setCategoryId()
并发现$product->getCategoryId()
没有返回任何内容,并且Mage_Catalog_Model_Product_Url::getUrl
方法依赖于产品模型上的getCategoryId
方法来确定产品的 URL。结果getCategoryId
是在产品模型中显式定义的(不像我想象的那样是一个神奇的方法),它返回Mage::registry('current_category')
的值。所以基本上我注册和取消注册“current_category”来满足我的需求。但这仍然让我的问题部分没有得到解答。我想出了如何破解并获取我想要的 URL,但从技术上讲,混乱的
/catalog/product/view/id/123/s/my-product-slug/
类型的 URL 仍然有效,并且我宁愿所有的 URL 类型都路由到类别化的 URL 类型。如果能够在 Magento 中定义单一的、规范的 URL 样式,那就太好了。如果有人对此有任何进一步的见解,请告诉我!clockworkgeek was pretty close and led me to my answer...
I tried using
$product->setCategoryId()
and was finding that$product->getCategoryId()
wasn't returning anything and that theMage_Catalog_Model_Product_Url::getUrl
method depends on thegetCategoryId
method on the product model to figure out a product's URL. Turns outgetCategoryId
is explicitly defined in the product model (not a magic method like I assumed) that returns the value ofMage::registry('current_category')
. So basically I registered and unregistered 'current_category' to meet my needs.This still leaves my question partially unanswered though. I figured out how to hack around and get what URL I wanted, but technically the messy
/catalog/product/view/id/123/s/my-product-slug/
-type URLs still work and I'd rather all of the URL varieties route to the category-fied one. It would be great to be able to define a single, canonical URL style in Magento. If anyone has any further insight on this please let me know!为了强制使用该 URL,我非常确定您的产品必须包含类别 ID。
当然
7
只是一个例子。一种产品可以属于多个类别,因此您必须决定如何选择一个类别,也许可以假设第一个类别。To force that URL I'm pretty sure your product must include a category ID.
Of course
7
is just an example. A product can belong to many categories so you'll have to decide how to pick one, perhaps by assuming the first.我不确定这是否是您所需要的,现在无法检查,但请尝试以下操作:
I'm not sure if this is what you need and can't check right now but try this: