Magento 1 - 删除 url 键/产品 url 中的数字 [英] Magento 1 - Removing numbers in url key/product url

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

问题描述

您可能知道,如果您的产品共享一个 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 的 来源(这是附加到我的网址的 # 之一)?我试过产品id,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 1 - 删除 url 键/产品 url 中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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