You are here:  » Search Autocomplete / Category Name


Search Autocomplete / Category Name

Submitted by homelesscto on Mon, 2016-09-05 11:40 in

Hi David,

is it possible to show category name and products both in Global product search with Autocomplete.

Example : {link saved}

Thanks
Cheers

Submitted by support on Mon, 2016-09-05 12:48

Hi,

For normal search results, first edit search.php and look for the following code at line 485:

      $sql2 = "SELECT id,name,normalised_name,image_url,description,price,rating FROM `".$config_databaseTablePrefix."products` WHERE id IN (".$in.")";

...and REPLACE with:

      $sql2 = "SELECT id,name,normalised_name,image_url,description,price,rating,category FROM `".$config_databaseTablePrefix."products` WHERE id IN (".$in.")";

Then in html/searchresults.php, to display the category name after the truncated description, look for the following code at line 51:

  <?php endif; ?>

...and REPLACE with:

  <?php endif; ?>
  <?php if ($product["category"]): ?>
  <p>Category: <?php print $product["category"]; ?></p>
  <?php endif; ?>

For Autocomplete, edit searchJSON.php and perform a Search and Replace as follows:

Search:

DISTINCT(normalised_name) AS name

Replace:

DISTINCT(normalised_name) AS name,CONCAT(category,'-',normalised_name) AS label

(2 instances)

And then in html/searchform.php, look for the following code at line 8:

response($.map( data.products, function( item ) { return { label:item.name, value:item.name}

...and REPLACE with:

response($.map( data.products, function( item ) { return { label:item.label, value:item.name}

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Mon, 2019-05-20 16:28

Hi David!

I am using custom category hierarchy.

Is it possible to also display with autocomplete categories from hierarchy?

Thanks!

Submitted by support on Tue, 2019-05-21 08:17

Hi,

Sure - make the changes to html/searchform.php described above to show the label as well as the result name, and then use as new searchJSON.php as follows:

<?php
  
require("includes/common.php");
  
header("Content-Type: application/json;");
  if (
$_GET["q"])
  {
    
$sql "SELECT DISTINCT(normalised_name),categoryid AS name FROM `".$config_databaseTablePrefix."products` WHERE name LIKE '".database_safe($_GET["q"])."%' LIMIT 5";
    if (!
database_querySelect($sql,$products))
    {
      
$sql "SELECT DISTINCT(normalised_name),categoryid AS name FROM `".$config_databaseTablePrefix."products` WHERE name LIKE '%".database_safe($_GET["q"])."%' LIMIT 5";
      
database_querySelect($sql,$products);
    }
    if (
count($products))
    {
      foreach(
$products as $k => $product)
      {
        
$sql "SELECT name FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE id='".database_safe($product["categoryid"])."'";
        if (
database_querySelect($sql,$rows))
        {
          
$products[$k]["label"] = $rows[0]["name"]." - ".$product["name"];
        }
      }
    }
    print 
json_encode(array("totalResultsCount"=>count($products),"products"=>$products));
  }
  exit();
?>

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Tue, 2019-05-21 17:57

Wops. Maybe I didn't explain well.

I mean,besides searching for product names, also searching for categories.

So if someone types "smartpho" it suggests smartphones with the link to the category.

Submitted by support on Wed, 2019-05-22 11:44

Hi,

Ah sorry I see now; have a go with the following replacement searchJSON.php:

<?php
  
require("includes/common.php");
  
header("Content-Type: application/json;");
  function 
getCategoryPath($id)
  {
    global 
$config_databaseTablePrefix;
    
$categories = array();
    do {
      
$sql "SELECT name,parent FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE id = '".$id."'";
      
database_querySelect($sql,$rows);
      
array_unshift($categories,$rows[0]["name"]);
    } while(
$id $rows[0]["parent"]);
    return 
implode("/",$categories);
  }
  if (
$_GET["q"])
  {
    
$products = array();
    
$sql "SELECT DISTINCT(name) AS name,id FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE name LIKE '".database_safe($_GET["q"])."%' LIMIT 5";
    if (!
database_querySelect($sql,$rows))
    {
      
$sql "SELECT DISTINCT(name) AS name,id FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE name LIKE '%".database_safe($_GET["q"])."%' LIMIT 5";
      
database_querySelect($sql,$rows);
    }
    if (
count($rows))
    {
      foreach(
$rows as $row)
      {
        
$products[] = array("name"=>"Category - ".$row["name"],"url"=>tapestry_indexHREF("category",getCategoryPath($row["id"])));
      }
    }
    
$sql "SELECT DISTINCT(normalised_name) AS name FROM `".$config_databaseTablePrefix."products` WHERE name LIKE '".database_safe($_GET["q"])."%' LIMIT 5";
    if (!
database_querySelect($sql,$rows))
    {
      
$sql "SELECT DISTINCT(normalised_name) AS name FROM `".$config_databaseTablePrefix."products` WHERE name LIKE '%".database_safe($_GET["q"])."%' LIMIT 5";
      
database_querySelect($sql,$rows);
    }
    if (
count($rows))
    {
      foreach(
$rows as $row)
      {
        
$products[] = array("name"=>$row["name"],"url"=>"");
      }
    }
    print 
json_encode(array("totalResultsCount"=>count($products),"products"=>$products));
  }
  exit();
?>

And then as a complete replacement for the <script>...</script> section of html/searchform.php (so not including jquery / jquery-ui) - make sure however you are currently including these remains:

<script type='text/JavaScript'>
  $(function() {
  $( "#q" ).autocomplete({
    source: function( request, response ) {
      $.ajax({
        url: "<?php print $config_baseHREF?>searchJSON.php",dataType:"json",data:{q:request.term},
        success: function( data ) {
          response($.map( data.products, function( item ) { return { label:item.name, value:item.name, url:item.url }
          }));
        }
      });
      },
      select: function(event, ui) {
        if (ui.item.url)
        {
          window.location = ui.item.url;
          return;
        }
        if(ui.item){
            $(event.target).val(ui.item.value);
        }
        $(event.target.form).submit();
      },
      minLength: 2,
      open: function() { $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" ); },
      close: function() { $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" ); }
    })
    .keydown(function(e){ if (e.keyCode === 13) { $("#search").trigger('submit'); }
    });
  });
</script>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Wed, 2019-05-22 18:42

Excellent, it works like a charm.
The downside is that keydown doesn't works.

Thank you.

Submitted by support on Thu, 2019-05-23 09:22

Hi,

I just tried without the keydown hook and the category link worked and submit on enter still triggers - have a go with:

<script type='text/JavaScript'>
  $(function() {
  $( "#q" ).autocomplete({
    source: function( request, response ) {
      $.ajax({
        url: "<?php print $config_baseHREF?>searchJSON.php",dataType:"json",data:{q:request.term},
        success: function( data ) {
          response($.map( data.products, function( item ) { return { label:item.name, value:item.name, url:item.url }
          }));
        }
      });
      },
      select: function(event, ui) {
        if (ui.item.url)
        {
          window.location = ui.item.url;
          return;
        }
        if(ui.item){
            $(event.target).val(ui.item.value);
        }
        $(event.target.form).submit();
      },
      minLength: 2,
      open: function() { $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" ); },
      close: function() { $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" ); }
    })
  });
</script>

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Fri, 2019-05-24 09:59

I can confirm it works. Pretty neat! Thank you Mr.