简体   繁体   English

Opencart搜索页面自定义产品链接结构

[英]Opencart search page custom product link structure

By default, in Opencart, on the search page when I search for a product, the products appear with the following link structure: sitename/product-name/?search= , but I want to change this to sitename/category/subcategory/product-name 默认情况下,在Opencart中,当我搜索产品时,在搜索页面上,产品会显示以下链接结构: sitename/product-name/?search= ,但我想将其更改为sitename/category/subcategory/product-name

$this->data['products'][] = array(
                'product_id'  => $result['product_id'],
                'thumb'       => $image,
                'name'        => $result['name'],
                'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
                'price'       => $price,
                'special'     => $special,
                'tax'         => $tax,
                'rating'      => $result['rating'],
                'reviews'     => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
                'href'        => $this->url->link('product/product', 'product_id=' . $result['product_id'])
            );

the 'href' is the line that contains the link structure . “ href”是包含链接结构的行。

'href'        => $this->url->link('product/product', 'product_id=' . $result['product_id'])
            );

I wrote a function for this a while back and you are free to use it. 不久前,我为此编写了一个函数,您可以自由使用它。 The only problem arises when a product belongs to more than one category, in which case the function chooses the deepest level category to generate full path. 当产品属于多个类别时,就会出现唯一的问题,在这种情况下,函数会选择最深层次的类别以生成完整路径。 If more than one exists at the same depth level it will choose the first match. 如果在同一深度级别存在多个,它将​​选择第一个匹配项。

Add this function to the end of /catalog/model/catalog/product.php : 将此功能添加到/catalog/model/catalog/product.php

public function getProductCategoryPath ($product_id) {
    $query = $this->db->query("
        SELECT GROUP_CONCAT(path_id ORDER BY LEVEL ASC SEPARATOR '_') as path 
        FROM " . DB_PREFIX . "product_to_category
            LEFT JOIN " . DB_PREFIX . "category_path USING (category_id)
        WHERE product_id = '" . (int)$product_id . "'
            AND category_id =
            (
                SELECT category_id 
                FROM " . DB_PREFIX . "product_to_category c 
                LEFT JOIN " . DB_PREFIX . "category_path cp USING (category_id) 
                WHERE product_id = '" . (int)$product_id . "' 
                AND cp.level =
                (
                    SELECT max(LEVEL) 
                    FROM " . DB_PREFIX . "product_to_category
                    LEFT JOIN " . DB_PREFIX . "category_path USING (category_id)
                    WHERE product_id = '" . (int)$product_id . "'
                )
                ORDER BY category_id LIMIT 1
            )
        GROUP BY category_id
    ");
    return $query->num_rows ? $query->row['path'] : false;  
}

Then revise catalog/controller/product/search.php immediately before the block you posted to call the function like this: 然后,在您发布的代码块之前立即修改catalog/controller/product/search.php来调用此函数:

$path = $this->model_catalog_product->getProductCategoryPath($result['product_id']);
$url = $path ? '&path=' . $path : '';

$this->data['products'][] = array(

Explanation: 说明:

The query in the function first finds all categories that are linked to the product. 函数中的查询首先查找链接到产品的所有类别。 Next it finds the the deepest level and selects the first category it encounters which both matches that level and is assigned to the product. 接下来,它找到最深的级别,并选择遇到的第一个类别,该类别均与该级别匹配并分配给产品。 Then it uses GROUP_CONCAT to build a path string of the category hierarchy based on the selected category. 然后,它使用GROUP_CONCAT根据所选类别构建类别层次结构的路径字符串。

In the search.php controller we then call this function and if there is a non-empty result, we replace the search url OpenCart with the category path string returned by the function. 然后在search.php控制器中调用此函数,如果有非空结果,则将搜索URL OpenCart替换为该函数返回的类别路径字符串。

you can do that. 你可以做到的。 This is the href link for link to a product echo 这是href链接,用于链接到产品回显

$product['href'] <h4><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></h4>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM