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

时间:2015-02-13 09:16:24

标签: php opencart

默认情况下,在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'])
            );

' href'是包含链接结构的行。

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

2 个答案:

答案 0 :(得分:0)

我曾经为此写了一个函数,你可以自由使用它。当产品属于多个类别时,唯一的问题就出现了,在这种情况下,函数选择最深级别类别来生成完整路径。如果在同一深度级别存在多个,则会选择第一个匹配。

将此功能添加到/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;  
}

然后在您发布的块之前立即修改catalog/controller/product/search.php以调用此函数:

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

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

<强>解释

函数中的查询首先查找链接到产品的所有类别。接下来,它找到最深的级别并选择它遇到的第一个类别,它们都匹配该级别并分配给产品。然后,它使用GROUP_CONCAT根据所选类别构建类别层次结构的路径字符串。

search.php控制器中,我们调用此函数,如果存在非空结果,我们将搜索URL OpenCart替换为函数返回的类别路径字符串。

答案 1 :(得分:0)

你可以做到这一点。这是链接到产品echo

的href链接