Magento-删除网址密钥/产品网址中的数字 [英] Magento - Removing numbers in url key/product url

查看:83
本文介绍了Magento-删除网址密钥/产品网址中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可能知道,如果您的产品共享一个url密钥,则该url密钥将附加一个数字:

As you may know, if you have products that share a url key, the url key will have a digit appended to it:

http://www.example.com/main-category/sub-category/product-name-**6260**.html

我如何找到6260的(这是附加到我网址中的#之一)?我尝试了产品编号sku,但找不到它的来源.我问的原因是因为如果可以找到它,我可以创建一个字符串替换函数以将其从url中清除掉,然后再在某些产品列表页面上回显它们.

How do I find the source of that 6260 (which is one of the #'s appended to my urls)? I tried product id, sku, I cannot find the source of it. The reason I ask is because if I can find it, I can create a string replace function to flush it out of url's before I echo them on certain product listing pages.

谢谢.

推荐答案

在我们到达发生这种情况的代码位置之前,建议您进入一个痛苦的世界.

Before we get to the location in code where this happens, be advised you're entering a world of pain.

  1. 对于这些数字的生成方式没有简单规则.在某些情况下,它是商店ID,在某些情况下,它是简单产品ID.在某些情况下两者都不是

  1. There's no simple rule as to how those numbers are generated. There's cases where it's the store ID, there's cases where it's the simple product ID. There's cases where it's neither

即使不存在,Magento站点也经常包含更改此内容的自定义功能

Even if there was, it's common for not-from-scratch Magento sites to contain custom functionality that changes this

最终,由于Magento的人类可读/SEO友好URL位于core_url_rewrite表中,因此人们可以插入任意文本

Ultimately, since Magento's human readable/SEO-friendly URLs are located in the core_url_rewrite table, it's possible for people to insert arbitrary text

除了厄运的警告,您正在寻找的模型是Mage::getSingleton('catalog/url').这包含生成Magento目录和产品重写的大多数逻辑.所有这些方法都以通过getUnusedPath方法传递请求路径结尾.

Warnings of doom aside, the Model you're looking for is Mage::getSingleton('catalog/url'). This contains most of the logic for generating Magento Catalog and product rewrites. All of these methods end by passing the request path through the getUnusedPath method.

#File: app/code/core/Mage/Catalog/Model/Url.php
public function getUnusedPath($storeId, $requestPath, $idPath)
{
    //...
}

此方法包含用于在URL末尾创建唯一编号的逻辑.完全跟踪此内容超出了Stack Overflow帖子的范围,但是特别是这条线是有启发/令人沮丧的.

This method contains the logic for for creating a unique number on the end of the URL. Tracing this in its entirely is beyond the scope of a Stack Overflow post, but this line in particular is enlightening/disheartening.

$lastRequestPath = $this->getResource()
    ->getLastUsedRewriteRequestIncrement($match[1], $match[4], $storeId);
if ($lastRequestPath) {
    $match[3] = $lastRequestPath;
}
return $match[1]
    . (isset($match[3]) ? ($match[3]+1) : '1')
    . $match[4];

尤其是这两行

$match[3] = $lastRequestPath;
//...
. (isset($match[3]) ? ($match[3]+1) : '1')
//...

如果不太明显,在某些情况下,Magento会自动将1附加到URL,然后继续增加它.这使得这些URL的生成取决于生成它们时的系统状态-没有简单的规则.

In case it's not obvious, there are cases where Magento will automatically append a 1 to a URL, and then continue to increment it. This makes the generation of those URLs dependent on system state when they were generated — there's no simple rule.

此文件中其他感兴趣的行是

Other lines of interest in this file are

if (strpos($idPath, 'product') !== false) {
    $suffix = $this->getProductUrlSuffix($storeId);
} else {
    $suffix = $this->getCategoryUrlSuffix($storeId);
}    

$suffix也将在URL的末尾使用,因此值得研究这些方法.

This $suffix will be used on the end of the URL as well, so those methods are worth investigating.

如果您要做的只是从URL中删除数字,则最好使用正则表达式或一些explode/implode字符串跳动.

If all you're trying to do is remove numbers from the URL, you might be better off with a regular expression or some explode/implode string jiggering.

这篇关于Magento-删除网址密钥/产品网址中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆