Development > Development
Custom field search
Sophia_OS:
Hi everyone,
i wanna search custom field's value in website's main search box, not with its own box.
for example if there is a custom field (color) and its value is "red" and "blue" and "green" , if you search "blue" in the website's main search box, it shows this item in searched list too.
thank you
theme bender
fre2mansur:
its possible by doing as plugin.. post on job section.. dev\s can help you
Sophia_OS:
--- Quote from: fre2mansur on January 09, 2018, 01:49:12 pm ---its possible by doing as plugin.. post on job section.. dev\s can help you
--- End quote ---
@fre2mansur
Thank you for your reply,
Nobody helps us here for free to do this?
marius-ciclistu:
EDIT These hooks are deprecated
Try adding the custom field's values to the end of the description before sending it to the db when the item is saved. There are some hooks: pre_item_add and pre_item_edit
marius-ciclistu:
Sorry, pre_item_edit and pre_item_add are deprecated .
use item_add_prepare_data and item_edit_prepare_data instead
In your theme's functions.php file, add to the end
EDITED I reversed the mc_edit_filter_description($aItem) and mc_add_filter_description($aItem) functions
I splitted mc_add_metas in mc_add_metas_item_edit and mc_add_metas_item_add
--- Code: ---<?php
function endsWith($haystack, $needle)
{
$length = strlen($needle);
return $length === 0 ||
(substr($haystack, -$length) === $needle);
}
function mc_add_metas_item_add($desc, $aItem) {
$_meta = Field::newInstance()->findByCategory($aItem['catId']);
$meta = Params::getParam("meta");
foreach($_meta as $_m) {
$meta[$_m['pk_i_id']] = (isset($meta[$_m['pk_i_id']]))?$meta[$_m['pk_i_id']]:'';
}
$d = '\n\n ';
$mField = Field::newInstance();
foreach($meta as $k => $v) {
$field = $mField->findByPrimaryKey($k);
$d = $d . $field['s_name'] .': ' .$v. '\n';
}
if(endsWith($desc, $d))
return $desc;
else return $desc . $d;
}
function mc_add_metas_item_edit($desc, $itemId) {
$metas = Item::newInstance()->metaFields($item_Id);
$d = '\n\n ';
foreach($metas as $m) {
$d = $d . $m["s_name"] .': ' .$m["s_value"] . '\n';
}
if(endsWith($desc, $d))
return $desc;
else return $desc . $d;
}
function mc_edit_filter_description($aItem) {
foreach(@$aItem['description'] as $key => $value) {
$aItem['description'][$key] = mc_add_metas_item_edit($value,$aItem['idItem']);
}
return $aItem;
}
function mc_add_filter_description($aItem) {
foreach(@$aItem['description'] as $key => $value) {
$aItem['description'][$key] = mc_add_metas_item_add($value,$aItem);
}
return $aItem;
}
osc_add_filter('item_add_prepare_data', 'mc_add_filter_description');
osc_add_filter('item_edit_prepare_data', 'mc_edit_filter_description');
?>
--- End code ---
or if you don't want the custom field's name there remove
--- Code: ---$m["s_name"] .': ' .
--- End code ---
and
--- Code: ---$field['s_name'] .': ' .
--- End code ---
After this you must update the descriptions in your db by editing and saving each item.
Step 2
You would want to remove the added text from description when displaying the description to the public and to the owner when it edits hi's item.
You can do that with a function
Put this before the above code in functions.php
--- Code: ---<?php
function mc_hide_metas($desc, $itemId) {
$metas = Item::newInstance()->metaFields($item_Id);
$d = '\n\n ';
foreach($metas as $m) {
$d = $d . $m["s_name"] .': ' .$m["s_value"] . '\n';
}
if(endsWith($desc, $d))
return str_replace($d,'',$desc);
else return $desc;
}
?>
--- End code ---
or if you don't want the custom field's name there remove
--- Code: ---$m["s_name"] .': ' .
--- End code ---
and call this function where the description is shown. Have a look here https://stackoverflow.com/questions/43015613/how-to-modify-ft-min-word-len-4-to-ft-min-word-len-1-so-that-osclass-3-7-1-can-s
And see where I call 'removeunderline(' for description.
If you do this admin and user side the solution will work if the item is edited and it's category is changed and or it's custom fields are changed.
If the admin makes the editing and the above function can't be called from oc-admin/themes/modern... files, the last function from above must be placed in /oc-includes/osclass/helpers/hSearch.php and removed from functions.php file.
Navigation
[0] Message Index
[#] Next page
Go to full version