Nancy Nancy - 11 days ago 5
PHP Question

Opencart to enable autocomplete for name AND model

I am using OC 2.2.0. On frontend, I need autocomplete both on name and model. Right now, only name autocompletes. In catalog/model/catalog/product.php I changed

$sql .= " OR LCASE(p.model) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";


to

$sql .= " OR LCASE(p.model) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";


But still can't get autocomplete on model. Instead, I always must enter the whole model name, but it must be autocomplete. Any suggestions?

Answer

After many days of researching and looking at the code, I managed to understand and fix my problem. I was looking at all the wrong places. The ONLY file which needs changing is getdata.php which is in the root of your OC. So, default autocomplete is for the product name only. It is defined in this file like this:

$sql = "SELECT pd.name FROM " . DB_PREFIX . "product p," . DB_PREFIX . "product_description pd WHERE p.status = 1 AND p.product_id = pd.product_id AND language_id = '".$id."' AND UPPER(pd.name) like UPPER('%$q%') GROUP BY pd.product_id ORDER BY pd.name ASC";
   $res = mysql_query($sql);
   if(mysql_num_rows($res)>0){
       while($ro = mysql_fetch_assoc($res)){

           $name = str_replace( array( '\'', '"', ',' , ';', '<', '>','&quot','&'), ' ', $ro['name']);
       //$str[]= $name."\n";
       echo $name."\n";
       }
   } 

Now, all you need to do is add the exact query for model or whichever database field in the oc_product table you need autocomplete for. Meaning, for example, copy the name query and replace pd.name with p.model and wherever you find ['name']replace with ['model']. And voila! I really hope this helps someone, as up until today I had trouble realizing this myself.

Comments