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

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

问题描述

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

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

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

谢谢.

解决方案

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

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

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

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

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

  #File:app/code/core/Mage/Catalog/Model/Url.php公共函数getUnusedPath($ storeId,$ requestPath,$ idPath){//...} 

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

  $ lastRequestPath = $ this-> getResource()-> getLastUsedRewriteRequestIncrement($ match [1],$ match [4],$ storeId);如果($ lastRequestPath){$ match [3] = $ lastRequestPath;}返回$ 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的生成取决于生成它们时的系统状态-没有简单的规则.

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

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

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

如果您只想从URL中删除数字,则最好使用正则表达式或一些 explode / implode 字符串跳格./p>

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

i.e

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

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.

Thanks.

解决方案

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

  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

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

  3. 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

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)
{
    //...
}

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];

In particular, these two lines

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

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);
}    

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

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

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