我对 opencart 很陌生,我想知道如何修改搜索功能以按选项或价格搜索产品。我的大学有一个项目,我需要创建这样的功能。
你能告诉我怎么做吗?我需要一个例子。
谢谢!
我在这个例子中使用 Opencart 1.5.3.1
您需要修改 getProducts() 函数。搜索查询在构建时如下所示:
SELECT p.product_id, (SELECT AVG(rating) AS total FROM review r1 WHERE r1.product_id =
p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating FROM product p LEFT
JOIN product_description pd ON (p.product_id = pd.product_id) LEFT JOIN product_to_store
p2s ON (p.product_id = p2s.product_id) LEFT JOIN product_tag pt ON (p.product_id =
pt.product_id) WHERE pd.language_id = '1' AND p.status = '1' AND p.date_available <= NOW
() AND p2s.store_id = '0' AND ( LCASE(pd.name) LIKE '%large%' OR LCASE(pt.tag) LIKE
'%large%' AND pt.language_id = '1') GROUP BY p.product_id ORDER BY p.sort_order ASC, LCASE
(pd.name) ASC LIMIT 0,15
您需要向查询添加选项值描述以及向 WHERE 子句添加参数:
SELECT p.product_id, (SELECT AVG(rating) AS total FROM review r1 WHERE r1.product_id =
p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating FROM product p LEFT
JOIN product_description pd ON (p.product_id = pd.product_id) LEFT JOIN
product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN product_option_value
pov ON (p.product_id = pov.product_id) LEFT JOIN option_value_description ovd ON
(pov.option_value_id = ovd.option_value_id) LEFT JOIN product_tag pt ON (p.product_id
= pt.product_id) WHERE pd.language_id = '1' AND p.status = '1' AND p.date_available <=
NOW() AND p2s.store_id = '0' AND ( LCASE(pd.name) LIKE '%large%' OR LCASE(ovd.name)
LIKE '%large%' OR LCASE(pt.tag) LIKE '%large%' AND pt.language_id = '1') GROUP BY
p.product_id ORDER BY p.sort_order ASC, LCASE(pd.name) ASC LIMIT 0,15
为此,请编辑 catalog/model/catalog/product.php。查找第 37,38 行:
if (!$product_data) {
$sql = "SELECT p.product_id, (SELECT AVG(rating) AS total FROM
" . DB_PREFIX . "review r1 WHERE r1.product_id = p.product_id AND r1.status = '1'
GROUP BY r1.product_id) AS rating FROM " . DB_PREFIX . "product p LEFT JOIN " .
DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " .
DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id)";
之后添加:
//add option values to search query
$sql.= " LEFT JOIN " . DB_PREFIX . "product_option_value pov ON (p.product_id = pov.product_id)";
$sql.= " LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (pov.option_value_id = ovd.option_value_id)";
// - - - - - - - - - - - - -
查找第 70 行:
$implode[] = "LCASE(pd.name) LIKE '%" . $this->db->escape(utf8_strtolower($word)) . "%'";
将其更改为:
$implode[] = "LCASE(pd.name) LIKE '%" . $this->db->escape(utf8_strtolower($word)) . "%'
OR LCASE(ovd.name) LIKE '%" . $this->db->escape(utf8_strtolower($word)) . "%'";
现在搜索将“看到”产品选项值名称,例如“大”、“红色”等。
要搜索价格要容易得多,只需在 WHERE 子句中包含参数以进行精确匹配:
"pd.price = '" . $this->db->escape(utf8_strtolower($word)) . "'
例如,对于达到某个值的价格值,请使用:
"pd.price <= '" . $this->db->escape(utf8_strtolower($word)) . "'
希望,你明白了。
现在,这只是一个快速而肮脏的示例,旨在让您继续前进。
我不建议将此作为最终解决方案。
您需要考虑添加新的 GET 变量来控制是否在搜索中包含选项和/或价格。此外,搜索价格范围而不是价格。添加变量以控制是否使用 AND 或 OR 运算符 - 即查找“red”和“XL”或两者中的任何一个。
祝你好运。