我对opencart很新,我想知道如何通过选项或价格修改搜索功能来搜索产品。我有一个项目为我的大学,我需要创建这样的功能。
你能告诉我怎么样吗?我需要一个例子。Thanx!
答案 0 :(得分:3)
我在本例中使用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)) . "'
for,比方说,价格值达到一定值,使用:
"pd.price <= '" . $this->db->escape(utf8_strtolower($word)) . "'
希望,你明白了。
现在,这只是一个快速而又肮脏的例子,旨在让您前进。
我不是提出这个作为最终解决方案。
您需要考虑添加新的GET变量来控制是否在搜索中包含选项和/或价格。此外,搜索价格范围而不是价格。添加变量来控制是否使用AND或OR运算符 - 即查找'red'和'XL'或两者中的任何一个。
祝你好运。