Php Opencart搜索页面自定义产品链接结构
默认情况下,在Opencart中,当我搜索产品时,在搜索页面上,产品显示为以下链接结构:Php Opencart搜索页面自定义产品链接结构,php,opencart,Php,Opencart,默认情况下,在Opencart中,当我搜索产品时,在搜索页面上,产品显示为以下链接结构:sitename/product name/?search=,但我想将其更改为sitename/category/subcategory/product name $this->data['products'][] = array( 'product_id' => $result['product_id'], 'thumb'
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'])
);
不久前我为此编写了一个函数,您可以自由使用它。当一个产品属于多个类别时,唯一的问题就出现了,在这种情况下,函数选择最深级别的类别来生成完整路径。如果在同一深度级别上存在多个匹配项,则将选择第一个匹配项 将此函数添加到
/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\u CONCAT
基于所选类别构建类别层次结构的路径字符串
然后,在
search.php
控制器中调用此函数,如果有非空结果,则将搜索url OpenCart替换为该函数返回的类别路径字符串。您可以这样做。这是指向产品回音链接的href链接
$product['href'] <h4><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></h4>
$product['href']
您想只在搜索结果中更改产品链接还是在所有页面(类别页面、特色模块、畅销书模块…)中更改产品链接?嗯,只有在类别页面的搜索页面中才可以/类别/子类别/产品名称。