Mam rozwiązanie, które pomógł mi przygotować znajomy programista, nie jest to co prawda pełnowartościowy moduł, ale pozwala na wyszukiwanie wg. jednego artybutu jeśli znamy jego ID. Do moich potrzeb w zupełności wystarcza (wyszukiwanie wg koloru).
Do katalogu /overrides/controllers wrzucamy 1 plik:
SearchController.php
<?php
class SearchController extends SearchControllerCore
{
public function preProcess()
{
parent::preProcess();
$query = urldecode(Tools::getValue('q'));
if ($this->ajaxSearch)
{
self::$link = new Link();
$searchResults = Search::find((int)(Tools::getValue('id_lang')), $query, 1, 10, 'position', 'desc', true);
foreach ($searchResults AS &$product)
$product['product_link'] = self::$link->getProductLink($product['id_product'], $product['prewrite'], $product['crewrite']);
die(Tools::jsonEncode($searchResults));
}
if ($this->instantSearch && !is_array($query))
{
$this->productSort();
$this->n = abs((int)(Tools::getValue('n', Configuration::get('PS_PRODUCTS_PER_PAGE'))));
$this->p = abs((int)(Tools::getValue('p', 1)));
$search = Search::find((int)(self::$cookie->id_lang), $query, $this->p, $this->n, $this->orderBy, $this->orderWay);
Module::hookExec('search', array('expr' => $query, 'total' => $search['total']));
$nbProducts = $search['total'];
$this->pagination($nbProducts);
self::$smarty->assign(array(
'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
'search_products' => $search['result'],
'nbProducts' => $search['total'],
'search_query' => $query,
'instantSearch' => $this->instantSearch,
'homeSize' => Image::getSize('home')));
}
elseif ($query = Tools::getValue('search_query', Tools::getValue('ref')) AND !is_array($query))
{
$this->productSort();
$this->n = abs((int)(Tools::getValue('n', Configuration::get('PS_PRODUCTS_PER_PAGE'))));
$this->p = abs((int)(Tools::getValue('p', 1)));
$search = Search::find((int)(self::$cookie->id_lang), $query, $this->p, $this->n, $this->orderBy, $this->orderWay);
Module::hookExec('search', array('expr' => $query, 'total' => $search['total']));
$nbProducts = $search['total'];
$this->pagination($nbProducts);
self::$smarty->assign(array(
'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
'search_products' => $search['result'],
'nbProducts' => $search['total'],
'search_query' => $query,
'homeSize' => Image::getSize('home')));
}
elseif ($tag = urldecode(Tools::getValue('tag')) AND !is_array($tag))
{
$nbProducts = (int)(Search::searchTag((int)(self::$cookie->id_lang), $tag, true));
$this->pagination($nbProducts);
$result = Search::searchTag((int)(self::$cookie->id_lang), $tag, false, $this->p, $this->n, $this->orderBy, $this->orderWay);
Module::hookExec('search', array('expr' => $tag, 'total' => sizeof($result)));
self::$smarty->assign(array(
'search_tag' => $tag,
'products' => $result, // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
'search_products' => $result,
'nbProducts' => $nbProducts,
'homeSize' => Image::getSize('home')));
}
elseif ($attr = Tools::getValue('search_attr'))
{
$this->productSort();
$this->n = abs((int)(Tools::getValue('n', Configuration::get('PS_PRODUCTS_PER_PAGE'))));
$this->p = abs((int)(Tools::getValue('p', 1)));
$search = Search::searchByAttribute((int)(self::$cookie->id_lang), $attr, $this->p, $this->n, $this->orderBy, $this->orderWay);
Module::hookExec('search', array('expr' => $attr, 'total' => $search['total']));
$name = Attribute::getAttributesName('6',$attr);
$nbProducts = $search['total'];
$this->pagination($nbProducts);
self::$smarty->assign(array(
'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
'search_products' => $search['result'],
'nbProducts' => $search['total'],
'search_query' => $name,
'homeSize' => Image::getSize('home')));
}
else
{
self::$smarty->assign(array(
'products' => array(),
'search_products' => array(),
'pages_nb' => 1,
'nbProducts' => 0));
}
self::$smarty->assign('add_prod_display', Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'));
}
}
Do katalogu /overrides/classes wrzucamy 2 plik:
Search.php
<?php
class Search extends SearchCore
{
public static function searchByAttribute($id_lang, $attr, $pageNumber = 1, $pageSize = 1, $orderBy = 'position', $orderWay = 'desc', $ajax = false, $useCookie = true)
{
global $cookie;
$db = Db::getInstance(_PS_USE_SQL_SLAVE_);
// Only use cookie if id_customer is not present
if ($useCookie)
$id_customer = (int)$cookie->id_customer;
else
$id_customer = 0;
// TODO : smart page management
if ($pageNumber < 1) $pageNumber = 1;
if ($pageSize < 1) $pageSize = 1;
if (!Validate::isOrderBy($orderBy) OR !Validate::isOrderWay($orderWay))
return false;
$intersectArray = array();
$scoreArray = array();
//$words = explode(' ', Search::sanitize($expr, (int)$id_lang));
$result = $db->ExecuteS('
SELECT cp.`id_product`
FROM `'._DB_PREFIX_.'category_group` cg
INNER JOIN `'._DB_PREFIX_.'category_product` cp ON cp.`id_category` = cg.`id_category`
INNER JOIN `'._DB_PREFIX_.'category` c ON cp.`id_category` = c.`id_category`
INNER JOIN `'._DB_PREFIX_.'product` p ON cp.`id_product` = p.`id_product`
INNER JOIN `'._DB_PREFIX_.'product_attribute` ppa ON cp.`id_product` = ppa.`id_product`
INNER JOIN `'._DB_PREFIX_.'product_attribute_combination` ppac ON ppa.`id_product_attribute` = ppac.`id_product_attribute`
WHERE c.`active` = 1 AND p.`active` = 1 AND indexed = 1
AND ppac.id_attribute = '.$attr.'
AND cg.`id_group` '.(!$id_customer ? '= 1' : 'IN (
SELECT id_group FROM '._DB_PREFIX_.'customer_group
WHERE id_customer = '.(int)$id_customer.'
)'), false);
$eligibleProducts = array();
while ($row = $db->nextRow($result))
$eligibleProducts[] = $row['id_product'];
foreach ($intersectArray as $query)
{
$result = $db->ExecuteS($query, false);
$eligibleProducts2 = array();
while ($row = $db->nextRow($result))
$eligibleProducts2[] = $row['id_product'];
$eligibleProducts = array_intersect($eligibleProducts, $eligibleProducts2);
if (!count($eligibleProducts))
return ($ajax ? array() : array('total' => 0, 'result' => array()));
}
array_unique($eligibleProducts);
$productPool = '';
foreach ($eligibleProducts AS $id_product)
if ($id_product)
$productPool .= (int)$id_product.',';
if (empty($productPool))
return ($ajax ? array() : array('total' => 0, 'result' => array()));
$productPool = ((strpos($productPool, ',') === false) ? (' = '.(int)$productPool.' ') : (' IN ('.rtrim($productPool, ',').') '));
if ($ajax)
{
return $db->ExecuteS('
SELECT DISTINCT p.id_product, pl.name pname, cl.name cname,
cl.link_rewrite crewrite, pl.link_rewrite prewrite '.$score.'
FROM '._DB_PREFIX_.'product p
INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.')
INNER JOIN `'._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)$id_lang.')
WHERE p.`id_product` '.$productPool.'
ORDER BY position DESC LIMIT 10');
}
$queryResults = '
SELECT p.*, pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`name`,
tax.`rate`, i.`id_image`, il.`legend`, m.`name` manufacturer_name '.$score.', DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 new
FROM '._DB_PREFIX_.'product p
INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.')
LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group`
AND tr.`id_country` = '.(int)Country::getDefaultCountryId().'
AND tr.`id_state` = 0)
LEFT JOIN `'._DB_PREFIX_.'tax` tax ON (tax.`id_tax` = tr.`id_tax`)
LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer`
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
WHERE p.`id_product` '.$productPool.'
'.($orderBy ? 'ORDER BY '.$orderBy : '').($orderWay ? ' '.$orderWay : '').'
LIMIT '.(int)(($pageNumber - 1) * $pageSize).','.(int)$pageSize;
$result = $db->ExecuteS($queryResults);
$total = $db->getValue('SELECT COUNT(*)
FROM '._DB_PREFIX_.'product p
INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.')
LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group`
AND tr.`id_country` = '.(int)Country::getDefaultCountryId().'
AND tr.`id_state` = 0)
LEFT JOIN `'._DB_PREFIX_.'tax` tax ON (tax.`id_tax` = tr.`id_tax`)
LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer`
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
WHERE p.`id_product` '.$productPool);
if (!$result)
$resultProperties = false;
else
$resultProperties = Product::getProductsProperties((int)$id_lang, $result);
return array('total' => $total,'result' => $resultProperties);
}
}
Attribute.php
<?php
class Attribute extends AttributeCore
{
/**
* Get attr_name
*
*/
public static function getAttributesName($id_lang, $id_attr)
{
$attr = Db::getInstance()->ExecuteS('SELECT * FROM `'._DB_PREFIX_.'attribute_lang` WHERE `id_attribute` = '.$id_attr.' AND `id_lang` = '.$id_lang.'');
return $attr[0]['name'];
}
}
Po wrzuceniu tych modyfikacji możemy szukać wg. atrybutu podając jego ID w zapytaniu do search.php, a więc np. search.php?search_attr=3 zwróci nam listę przediotów o ID atrybutu równym 3 (w moim przypadku to jest po prostu szary kolor).
Oczywiście można to wrzucić bezpośrednio do core, ale to nieeleganckie rozwiązanie i posypie się przy apdtejtach.