I am trying to extend the default behaviour of the search mechanism provided by the default module blocksearch
in Prestashop 1.6.0.6.
I added to the default form a select
input returning categories.
Everything is working in both controller and view sides. The whole form is sending the correct parameter to the SearchController
. But, in Model, I still don't know what are the changes to apply on Search.php
entity so that the method find
returns products that belong to the specified category.
I spent a good time trying to understand how the search mechanism in find
works but to no avail. Can someone please point me the concerned part of code? where to add my conditions on category_id
in SQL?
Edit:
Among all the sql queries in Search::find
method, I think the following one is the concerned. In fact, I added AND p.'id_manufacturer' ='.(int)$manufacturer_id.'
and I can see changes. But for categories, it seems more complicated, I think I need a JOIN
somewhere. I am afraid I build things that can apparently working but are created with no respect to best practices. Please to help me I am a newbie in sql.
$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity,
pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`name`,
MAX(image_shop.`id_image`) id_image, il.`legend`, m.`name` manufacturer_name '.$score.', MAX(product_attribute_shop.`id_product_attribute`) id_product_attribute,
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
'.Shop::addSqlAssociation('product', 'p').'
INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (
p.`id_product` = pl.`id_product`
AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
)
LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (p.`id_product` = pa.`id_product`)
'.Shop::addSqlAssociation('product_attribute', 'pa', false, 'product_attribute_shop.`default_on` = 1').'
'.Product::sqlStock('p', 'product_attribute_shop', false, $context->shop).'
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`)'.
Shop::addSqlAssociation('image', 'i', false, 'image_shop.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` '.$product_pool.'
AND p.`id_manufacturer` ='.(int)$manufacturer_id.'
GROUP BY product_shop.id_product
'.($order_by ? 'ORDER BY '.$alias.$order_by : '').($order_way ? ' '.$order_way : '').'
LIMIT '.(int)(($page_number - 1) * $page_size).','.(int)$page_size;
Edit numero 2 : temporary solution
if($category_id!=0) $sql=$sql.' AND p.`id_product` IN (SELECT `id_product` FROM `ps_category_product` p WHERE p.`id_category`='.(int)$category_id.' )';