Opencart - 更改产品的默认地址
Opencart - changing the default address for products
我一整天都在努力寻找这个问题的答案,但到目前为止还没有成功。基本上我需要的是像这样更改产品的默认路由:
站点。com/product1 -> 站点。com/goods/product1
站点。com/product2 -> 站点。com/goods/product2
我原以为这是一个经常被问到的问题,但到目前为止我还没有找到解决方案。
好的,这是我能够以某种方式编写的解决方法。它进入 seo_pro.php 文件的 public 函数 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 文件中添加了从默认产品 link 到此产品的重定向:
$newUrl = '/goods/' . $this->request->request['_route_'];
header("Location:" . $newUrl,TRUE,301);
现在剩下的就是找到 Opencart 存储默认 link 的位置,以便默认在 url 中生成 /goods/。
更新二:
好的,找到了 Opencart 生成 links 的地方。它位于 /catalog/controllers/ 文件夹中。只需找到负责 product/product 的代码块并在开始使用它之前重写 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
我认为我的努力到此结束,将其标记为已回答。
我一整天都在努力寻找这个问题的答案,但到目前为止还没有成功。基本上我需要的是像这样更改产品的默认路由:
站点。com/product1 -> 站点。com/goods/product1
站点。com/product2 -> 站点。com/goods/product2
我原以为这是一个经常被问到的问题,但到目前为止我还没有找到解决方案。
好的,这是我能够以某种方式编写的解决方法。它进入 seo_pro.php 文件的 public 函数 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 文件中添加了从默认产品 link 到此产品的重定向:
$newUrl = '/goods/' . $this->request->request['_route_'];
header("Location:" . $newUrl,TRUE,301);
现在剩下的就是找到 Opencart 存储默认 link 的位置,以便默认在 url 中生成 /goods/。
更新二:
好的,找到了 Opencart 生成 links 的地方。它位于 /catalog/controllers/ 文件夹中。只需找到负责 product/product 的代码块并在开始使用它之前重写 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
我认为我的努力到此结束,将其标记为已回答。