Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - Opencart duplicate URL keywords

How would I go about fixing the issue where two categories/products have the same URL in Opencart? Or if their is a module which already does this?

E.g: categories

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

This breaks Opencart because we have two sub-categories with the URL = trousers?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I know this is a late answer, but this is a deep rooted problem with OpenCarts architecture.

Personally I am not a fan of vQmod, so here is a (what some would call a hack) solution to the problem without using one.

I have seen many alterations to catalog/controller/common/seo_url.php that add custom seo urls. This fix is compatible with such modifications.

I would also like to add that is by no means the most sophisticated solution in the world but it will guarantee that sub categories with duplicate seo url entries will work as they should.

Find in 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];
    }
}

Replace with the following

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

Add the following line to the top of the method index()

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

Find the line...

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

And replace with the following

$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]) . "'");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...