2

我将如何解决 Opencart 中两个类别/产品具有相同 URL 的问题?或者如果他们是一个已经这样做的模块?

例如:类别

Ladies -> Trousers (URL: ladies/trousers)
Mens -> Trousers (URL: mens/trousers)

这破坏了 Opencart,因为我们有两个带有 URL = trousers? 的子类别

4

5 回答 5

4

我知道这是一个迟到的答案,但这是 OpenCarts 架构的一个根深蒂固的问题。

就我个人而言,我不是 vQmod 的粉丝,所以这里有一个(有些人称之为 hack)解决方案而不使用它。

catalog/controller/common/seo_url.php我已经看到添加自定义 seo url 的许多更改。此修复程序与此类修改兼容。

我还想补充一点,这绝不是世界上最复杂的解决方案,但它可以保证具有重复 seo url 条目的子类别可以正常工作。

查找catalog/controller/common/seo_url.php

if ($url[0] == 'category_id') {
    if (!isset($this->request->get['path'])) {
        $this->request->get['path'] = $url[1];
    } else {
        $this->request->get['path'] .= '_' . $url[1];
    }
}

替换为以下内容

if ($url[0] == 'category_id') {
    $categories[$i] = $this->model_catalog_category->getCategory($url[1]);

    if (!isset($this->request->get['path'])) {
        $this->request->get['path'] = $categories[$i]['category_id'];
    } else {
        foreach ($query->rows as $row) {
            $url = explode('=', $row['query']);
            $category_id = $url[1];

            $category = $this->model_catalog_category->getCategory($category_id);

            if ($category['parent_id'] == $categories[$i - 1]['category_id']) {
                $this->request->get['path'] .= '_' . $category['category_id'];
            }
        }
    }
}

将以下行添加到方法 index() 的顶部

$this->load->model('catalog/category');

找到线...

foreach ($parts as $part) {
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'");

并替换为以下内容

$categories = array();

for ($i = 0; $i < count($parts); $i++) {
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($parts[$i]) . "'");
于 2014-07-08T14:15:34.227 回答
0

我为那个确切的场景开发了这个 vqmod

于 2012-06-06T16:30:47.720 回答
0

我已经解决了这个问题我已经制作了一个 vqmode xml 文件

请参阅 opencart 为以下网址提供相同的结果

  • http://mycart/mp3-players/iPod-Classic
  • http://mycart/mp3-players/some-child/iPod-Classic
  • http://mycart/some-parent/mp3-players/iPod-Classic
  • http://mycart/iPod-Classic

为那个产品“iPod-Classic”做seo会很困难</p>

所以我用这个脚本写了一个“vqmod”脚本,你可以将用户重定向到一个特定且正确的页面

甚至用户输入了错误的网址

IE

http://mycart/mp3-players/dddfsdgf/sdf/sdf/iPod-Classic

它会自动将其重定向到

http://mycart/mp3-players/iPod-Classic

您只需要从“box”下载“controller_common_seo_url.xml”或

  1. 在您打开的购物车中安装 vqmode
  2. 在 vqmod/xml 中创建一个名为“controller_common_seo_url.xml”的 xml 文件
  3. 打开 controller_common_seo_url.xml 并编写以下代码
    <modification>
            <id>public function index() add more functions</id>
            <version>1.0</version>
            <vqmver>2.1.5</vqmver>
            <author>http://www.bhardwajabhi.wordpress.com</author>
            <file name=”catalog/controller/common/seo_url.php”&gt;
                <operation>
                     <search position=”before”&gt;<![CDATA[ public function index() { ]]></search>
                                <add><![CDATA[

                public function get_seo_title($id, $type)
                {
                $query = $this->db->query("SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = '". $type ."=" . (int)$id  . "'");    

                if($query->row)
                    return     $query->row['keyword'];
                    else
                    return     $id;
                }

            public function get_path_level($id)
                {
                $query = $this->db->query(“select `level` from ” . DB_PREFIX . “category_path where `category_id` = ‘”. $id. “‘ order by level desc LIMIT 1″);    

                    return     $query->row['level'];

                }    


            public function get_product_relative($parts, $product_id)
                {


                if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == ‘on’) || ($this->request->server['HTTPS'] == ’1′))) {

                     $this->data['base'] = $this->config->get(‘config_ssl’);

                } else {

                    $this->data['base'] = $this->config->get(‘config_url’);

                    }    





                $path         =      $this->get_seo_title($product_id, ‘product_id’);        

                $query = $this->db->query(“SELECT category_id FROM ” . DB_PREFIX . “product_to_category WHERE product_id = ‘” . (int)$product_id . “‘”);
                $i =0;

                    foreach($query->rows as $pro_cat):
                    $i++;
                    $sub_query = $this->db->query(“select `path_id` from ” . DB_PREFIX . “category_path where `category_id` = ‘”. $pro_cat['category_id']. “‘ order by level desc”);

                        foreach($sub_query->rows as $pro_sub_cat):
                        $path = $this->get_seo_title($pro_sub_cat['path_id'], ‘category_id’) . “/” . $path;
                        endforeach;

                        $path1 = ‘/’. $path;

                        $array1 = explode(‘/’, $path1);

                        $array2 = explode(‘/’, $this->request->get['_route_']);

                        $result = array_diff($array1, $array2);


                        $new_path[$i]['path'] = $path;
                        $new_path[$i]['count'] = count($result);


                        $path = $this->get_seo_title($product_id, ‘product_id’);      


                    endforeach;

                    $min = PHP_INT_MAX;
                    $max = 0;

                    foreach ($new_path as $i) {                
                        $min = min($min, $i['count']);
                        }


                foreach($new_path as $value):
                    if($value['count']==$min):
                        $final_path =  $value['path'];
                        break;
                    endif;
                endforeach;




                similar_text($this->request->get['_route_'], $final_path, $percent);
                if($percent<>100)
                    header (‘location:’ . $this->data['base'] .”. $final_path);    




            }
            ]]></add>

                </operation>
            </file>
            <file name=”catalog/controller/common/seo_url.php”&gt;
                        <operation info=”After ABC, add 123″&gt;
                                <search position=”after”&gt;<![CDATA[
                               $this->request->get['product_id'] = $url[1];
                                ]]></search>
                                <add><![CDATA[
                                /*Start url redirection*/
                                $this->get_product_relative($parts, $url[1]);
                    /*End Url Redirection*/
                                ]]></add>
                        </operation>

                </file>

        </modification>

就是这样,现在当有人点击任何产品时,购物车将显示如下页面

http://www.yourSiteUrl/parent-category/child-category/product

注意:您必须为每个类别、子类别和那里的产品提供 seo 标题,因为脚本搜索 seo 标题,如果 seo 标题不可用,那么它会给它 id 所以假设我们没有为子类别提供 seo-title 和vew它的产品然后网址会像

http://www.yourSiteurl/parent-category/56/product

你可以在 这里得到更多的细节

于 2013-06-27T07:15:03.970 回答
0
if ($url[0] == 'category_id') {
    $categories[$i] = $this->model_catalog_category->getCategory($url[1]);

    if (!isset($this->request->get['path'])) {
        $this->request->get['path'] = $categories[$i]['category_id'];
    } else {
        foreach ($query->rows as $row) {
            $url = explode('=', $row['query']);
            $category_id = $url[1];

            $category = $this->model_catalog_category->getCategory($category_id);

            if ($category['parent_id'] == $categories[$i - 1]['category_id']) {
                $this->request->get['path'] .= '_' . $category['category_id'];
            }
        }
    }
}

如果它是 3 级类别,这将不起作用。我希望有人会有更好的解决方案。

于 2014-09-30T16:55:51.540 回答
0

除了@Ian Brindley,您还应该编辑另外 2 个文件,以便您可以在管理面板中输入关键字:
Admin/Controller/Catalog/category.php

在方法中找到这几行代码validateForm

if (utf8_strlen($this->request->post['keyword']) > 0) {
    $this->load->model('catalog/url_alias');

    $url_alias_info = $this->model_catalog_url_alias->getUrlAlias($this->request->post['keyword']);

    if ($url_alias_info && isset($this->request->get['category_id']) && $url_alias_info['query'] != 'category_id=' . $this->request->get['category_id']) {
        $this->error['keyword'] = sprintf($this->language->get('error_keyword'));
    }

    if ($url_alias_info && !isset($this->request->get['category_id'])) {
        $this->error['keyword'] = sprintf($this->language->get('error_keyword'));
    }

    if ($this->error && !isset($this->error['warning'])) {
        $this->error['warning'] = $this->language->get('error_warning');
    }
}

并将它们更改为:

if (utf8_strlen($this->request->post['keyword']) > 0) {
    $this->load->model('catalog/url_alias');

    $url_alias_info = $this->model_catalog_url_alias->getUrlAlias($this->request->post['keyword']);

    if ($url_alias_info && !isset($this->request->get['category_id'])) {
        $this->error['keyword'] = sprintf($this->language->get('error_keyword'));
    }

    if ($this->error && !isset($this->error['warning'])) {
        $this->error['warning'] = $this->language->get('error_warning');
    }
}

这将消除 seo 关键字是否重复的检查。这样您就可以在管理面板中输入 seo 关键字不是phpmyadmin

于 2015-10-22T08:18:19.520 回答