Magento 一致的产品 URL

发布于 2024-10-07 16:39:05 字数 422 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野鹿林 2024-10-14 16:39:05

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 the Mage_Catalog_Model_Product_Url::getUrl method depends on the getCategoryId method on the product model to figure out a product's URL. Turns out getCategoryId is explicitly defined in the product model (not a magic method like I assumed) that returns the value of Mage::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!

[浮城] 2024-10-14 16:39:05

为了强制使用该 URL,我非常确定您的产品必须包含类别 ID。

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

当然7只是一个例子。一种产品可以属于多个类别,因此您必须决定如何选择一个类别,也许可以假设第一个类别。

list($categoryId,) = $product->getCategoryIds();
$product->setCategoryId($categoryId)->getProductUrl();

To force that URL I'm pretty sure your product must include a category ID.

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

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.

list($categoryId,) = $product->getCategoryIds();
$product->setCategoryId($categoryId)->getProductUrl();
堇色安年 2024-10-14 16:39:05

我不确定这是否是您所需要的,现在无法检查,但请尝试以下操作:

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

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

$product_url = "http://www.domain.com/".$product->getUrlPath();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文