Add Custom Product Fields To Search Words (Without Copying Data)

our product data needed new custom fields, so I extended the schema, and added a hook to the product page to add the fields.

now I need to be able to search using those fields too.

I think I can modify fn.catalog.php, but there is no generic hook there to add the code to add my fields to the sql search clause, and I hate to modify product code, which could change on next release.

is there any other way? possible or recommended?

What about the additional_fields_in_search hook in the fn_get_products function (app/functions/fn.catalog.php) ?

What about the additional_fields_in_search hook in the fn_get_products function (app/functions/fn.catalog.php) ?

thanks.. I have implemented the hook, and its gets called... BUT, the search UI is changing the data (offering lookahead typing).. how can I force a specific search string? single or double quotes don't work.

I typed in 'pma-1234', or "pma-1234" , and the hook got "50-361234" ( as that product data ends in 1234 it appears )

the doc doesn't describe search

my other field data may contain special characters, dash, underscore, #, spaces, ... (I am not sure of the full list).

and if I force the content of the to be searched for string, my function adds the correct sql OR statemets and finds the one product..

Any reason you're not using product features for your new data rather than extending the schema?

Use the get_products_pre hook to process received search query ($params['q']). It is required to debug why quotes do not work.

turning off the searchanise addon solved the search term problem

Any reason you're not using product features for your new data rather than extending the schema?

features are not appropriate.. the original manufacturers part number is not a feature.. the customer didn't like the experience..

ok, my testing stinks.. it doesn't work.. because the fields are in a different table the sql is wrong..

I don't know how the product features part works, whihc is what I modeled off of..

if (!empty($params['feature_variants'])) {
$tmp .= db_quote(" OR (?:product_features_values.value LIKE ?l)", '%' . $piece . '%');
you cannot just drop another table in the middle of the OR sql syntax.. I don't see any function to fix/clean this up this.. the table name needs to be in the $join clause..
my working sql on my table is
select * from cscart_advanced_addon_product_data where nsn_part_number LIKE '%pma-1234%' or pma_part_number LIKE '%pma-1234%';
the created 'condition' is
(descr1.search_words LIKE '%nsn-1234%') OR (descr1.product LIKE '%nsn-1234%') OR (inventory.product_code LIKE '%nsn-1234%') OR (products.product_code LIKE '%nsn-1234%') OR (?:advanced_addon_product_data.oem_part_number LIKE '%nsn-1234%') OR (?:advanced_addon_product_data.nsn_part_number LIKE '%nsn-1234%') OR (?:advanced_addon_product_data.pma_part_number LIKE '%nsn-1234%')
but that will never work.
my table is linked on the product_id field.
also.. my hook is called, but it needs to RETURN variables to the main code.. debugging shows neither the variable list nor the join variable are returned to the caller. are hook calls by value or by reference?? seems by value..

Suggest you use the additional_fields_in_search hook and add your fields and the join needed to resolve the fields if the params indicate that you should.

Suggest you use the additional_fields_in_search hook and add your fields and the join needed to resolve the fields if the params indicate that you should.

I am using the additional_fields_in_search hook..

but the data I need to add is not returned to the caller...

none of the doc on hooks describes how to return multiple pieces of data..

I added my sql updates directly in the fn.catalog.php file around where the hook is called.. and it all works as hoped..

just don't know how to make the hook work

the existing code is

fn_set_hook('additional_fields_in_search', $params, $fields, $sortings, $condition, $join, $sorting, $group_by, $tmp, $piece, $having);
$search_conditions[] = '(' . $tmp . ')';
but this last statement (the very 1st after the hook returns) cannot include the fields added by the hook.. as that field ($tmp) is read only to the hook..
also $join would be read only..

i really need some help here..

You pass the values by reference I.e.

function fn_your_addon_name_additional_fields_in_search(&$params, &$fields, &$sortings, &$condition, &$join, &$sorting, &$group_by, &$tmp, &$piece, &$having) {
// your code here
}
so that your changes are reflected in the caller.

You pass the values by reference I.e.

function fn_your_addon_name_additional_fields_in_search(&$params, &$fields, &$sortings, &$condition, &$join, &$sorting, &$group_by, &$tmp, &$piece, &$having) {
// your code here
}
so that your changes are reflected in the caller.

thanks... when I do that

fn_my_changes_additional_fields_in_search(&$params, &$fields, &$sortings, &$condition, &$join, &$sorting, &$group_by, &$tmp, &$piece, &$having)

function

I get this error

And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error.

see http://php.net/manual/en/language.references.pass.php

I am running php 5.5.9

PHP 5.5.9-1ubuntu4.21

I actually had to use extra variables in the function to be able to use the reference... not like any sample anywhere

// this works..

function fn_my_changes_additional_fields_in_search(&$params, &$fields, &$sortings, &$condition, &$join, &$sorting, &$group_by, &$tmp, &$piece, &$having)
{
$t = &$tmp;
$t .= db_quote(" OR (addon_product_data.oem_part_number LIKE ?l)", "%{$piece}%");
$t .= db_quote(" OR (addon_product_data.nsn_part_number LIKE ?l)", "%{$piece}%");
$t .= db_quote(" OR (addon_product_data.pma_part_number LIKE ?l)", "%{$piece}%");
$j = &$join;
$j .= " LEFT JOIN ?:advanced_addon_product_data as addon_product_data ON addon_product_data.product_id = products.product_id";
}

shouldn't have had to, they would equate the same.

shouldn't have had to, they would equate the same.

i agree... but this produces the ' call-time pass-by-reference ' error.

$tmp .= db_quote(" OR (addon_product_data.oem_part_number LIKE ?l)", "%{$piece}%");
$tmp .= db_quote(" OR (addon_product_data.nsn_part_number LIKE ?l)", "%{$piece}%");
$tmp .= db_quote(" OR (addon_product_data.pma_part_number LIKE ?l)", "%{$piece}%");
$join .= " LEFT JOIN ?:advanced_addon_product_data as addon_product_data ON addon_product_data.product_id = products.product_id";

Then you're passing a variable as '&$variable_name' as the CALLER which you should be passing as '$variable_name'. It is only the function declaration that uses the '&$varriable_name' to indicate it is call by reference rather than call by value.

Then you're passing a variable as '&$variable_name' as the CALLER which you should be passing as '$variable_name'. It is only the function declaration that uses the '&$varriable_name' to indicate it is call by reference rather than call by value.

i didn't write the caller code. and no variable has leading &