News:

Support the VirtueMart project and become a member

Main Menu

No "Show search" Option for custom fields searching

Started by bpolinski, March 31, 2017, 10:05:16 AM

Previous topic - Next topic

bpolinski

Hi!

Im using VirtueMart 3.0.18. There is option "searchable" in custom fields, but i cannot enable "show search" in configuration, becouse there is no such option :(
I was using this tutorial: https://docs.virtuemart.net/tutorials/administration-configuration-vm-2/225-searchable-customfield-usecase.html
information in there: Requires VirtueMart 3.0.18 or higher. Please help :) Thank You!

edit: i also turned on "COM_VM_CATEGORY_FIELD_SELECT_SHOWSEARCH" in menu item virtuemart category. Still doesnt see search in categories.

hegbi

j3.7.0 / VM3.2.1 / PHP 7
I am having the same problem but in the meantime, if you want you can try this as a temporary workaround...

first save a copy of your original file administrator\components\com_virtuemart\models\product.php so you can restore it if needed
than in that file try to replace all lines from 287 to 354
where line 287 is if (!empty($this->searchcustoms)) {
and line 354 is } which is just before line 356 if($isSite and !VmConfig::get('use_as_catalog',0)) {

with this code

// hardcoded search customs Begin
if (!empty($this->keyword) and $this->keyword !== '' and $group === FALSE) {

$keyword =  '"%' .str_replace(array(' ','-','#39'),'%',$this->_db->getEscaped( $this->keyword, true )). '%"';
//$keyword = '"%' . $this->_db->getEscaped ($this->keyword, TRUE) . '%"';

foreach ($this->valid_search_fields as $searchField) {
if ($searchField == 'category_name' || $searchField == 'category_description') {
//$joinCategory = TRUE;
$joinCatLang = true;
}
else if ($searchField == 'mf_name') {
//$joinMf = TRUE;
$joinMfLang = true;
}
else if ($searchField == 'product_price') {
$joinPrice = TRUE;
}
else if (!$joinLang and ($searchField == 'product_name' or $searchField == 'product_s_desc' or $searchField == 'product_desc' or $searchField == '`p`.product_sku' or $searchField == '`l`.slug') ){
$joinLang = TRUE;
}

if (strpos ($searchField, '`') !== FALSE){
$keywords_plural = preg_replace('/\s+/', '%" AND '.$searchField.' LIKE "%', $keyword);
$filter_search[] =  $searchField . ' LIKE ' . $keywords_plural;
} else {
$keywords_plural = preg_replace('/\s+/', '%" AND `'.$searchField.'` LIKE "%', $keyword);
$filter_search[] = '`'.$searchField.'` LIKE '.$keywords_plural;
//$filter_search[] = '`' . $searchField . '` LIKE ' . $keyword;
}
}
// if no custom fields where defined to filter for use these hardcoded ones
if(empty($this->searchcustoms)){
  // 5 - id number of some custom field e.g. Supllier
  // 11 - id number of some custom field e.g. Color
  // 10 - id number of some custom field e.g. Size
  // 14 - id number of some custom field e.g. Type etc.
$custom_ids=array(5,11,12,14);
$this->searchcustoms=array_fill_keys($custom_ids, $this->keyword);
}
// add custom fields to filter array to get an OR condition in sql request
if (!empty($this->searchcustoms)) {
$joinCustom = TRUE;
foreach ($this->searchcustoms as $key => $searchcustom) {
$custom_search[] = '(pf.`virtuemart_custom_id`="' . (int)$key . '" and pf.`customfield_value` like "%' . $this->_db->escape ($searchcustom, TRUE) . '%")';
}}
if (!empty($custom_search)) {
$where[] = '(' . implode (' OR ', $custom_search) . ')';
}
else {
$where[] = '`product_name` LIKE ' . $keyword;
$joinLang = TRUE;
//If they have no check boxes selected it will default to product name at least.
}

}
// hardcoded search customs End


than search for line $custom_ids=array(5,11,12,14); and change values per your custom field id number that you want to search...

please note that this is a hack and that this file will be overwritten on any VM update