Opencart 搜索页面自定义产品 link 结构
Opencart search page custom product link structure
默认情况下,在Opencart中,当我搜索产品时,在搜索页面上,产品以以下link结构出现: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' 是包含 link 结构的行。
'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_CONCAT
根据所选类别构建类别层次结构的路径字符串。
然后在 search.php
控制器中调用此函数,如果结果非空,我们将搜索 url OpenCart 替换为函数返回的类别路径字符串。
你可以的。这是 link 到产品 echo
的 href link
$product['href'] <h4><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></h4>
默认情况下,在Opencart中,当我搜索产品时,在搜索页面上,产品以以下link结构出现: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' 是包含 link 结构的行。
'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_CONCAT
根据所选类别构建类别层次结构的路径字符串。
然后在 search.php
控制器中调用此函数,如果结果非空,我们将搜索 url OpenCart 替换为函数返回的类别路径字符串。
你可以的。这是 link 到产品 echo
的 href link$product['href'] <h4><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></h4>