Opencart - 更改产品的默认地址

时间:2021-07-28 17:19:41

标签: url opencart

我一整天都在试图找到这个问题的答案,但到目前为止都没有成功。基本上我需要的是更改产品的默认路由,如下所示:

site.com/product1 -> site.com/goods/product1
site.com/product2 -> site.com/goods/product2

我希望这是一个经常被问到的问题,但到目前为止我无法找到解决方案。

1 个答案:

答案 0 :(得分:0)

好的,这是我能够编写的以某种方式起作用的解决方法。它进入 seo_pro.php 文件的公共函数 index()。基本上,我复制了一部分专门用于处理产品的原始路由,并将其重写为在 url 中使用 /goods/。

if (explode("/",$this->request->get['_route_'])[0]=='goods') {      
      
    $route_ = $this->request->get['_route_'];
    unset($this->request->get['_route_']);
    $parts = explode('/', trim(utf8_strtolower($route_), '/'));
    
    list($last_part) = explode('.', array_pop($parts));
    array_push($parts, $last_part);
    $rows = array();
    foreach ($parts as $keyword) {
        if (isset($this->cache_data['keywords'][$keyword])) {
            $rows[] = array('keyword' => $keyword, 'query' => $this->cache_data['keywords'][$keyword]);
        }
    }    
    $queries = array();
    foreach ($rows as $row) {
        $queries[utf8_strtolower($row['keyword'])] = $row['query'];                         
    }
    reset($parts);
    if (count($parts) > 2) {
        return new Action($this->request->get['route'] = 'error/not_found');
    }
    $url = explode('=', $queries[$parts[1]]);
    $this->request->get[$url[0]] = $url[1];
        
    if (isset($this->request->get['product_id'])) {
        $this->request->get['route'] = 'product/product';
        if (!isset($this->request->get['path'])) {
            $path = $this->getPathByProduct($this->request->get['product_id']);
            if ($path) $this->request->get['path'] = $path;
        }
    }
    return new Action($this->request->get['route']);                        
}

目前只是检查 /goods/ 之后的内容,并执行通常的操作,就好像其中没有 /goods/ 一样。应该添加一些额外的检查以使其更安全,但这超出了本问题的范围。

更新 1:
我还在同一个 seo_pro.php 文件中添加了从默认产品链接到此链接的重定向:

$newUrl = '/goods/' . $this->request->request['_route_'];
header("Location:" . $newUrl,TRUE,301);

现在剩下的就是找到 Opencart 存储默认链接的位置,以便 if 会默认在 url 中生成 /goods/。

更新 2:
好的,找到 Opencart 生成链接的地方。它位于 /catalog/controllers/ 文件夹中。只需要找到负责产品/产品的代码块并在开始使用它之前重写 url,有点像这样:

$newUrl = explode('/', rtrim($this->url->link('product/product', 'product_id=' . $result['product_id']), '/'));
array_splice($newUrl, 3, 0, 'goods');
$newUrl = implode('/', $newUrl) .'/';

然后我将它提供给 $data['products'][] 数组:

'href' => $newUrl

我认为我的努力到此结束,将其标记为已回答。