Page 1 of 1

Product search in admin by part of the name

Posted: Fri Jun 03, 2011 8:58 am
by fabioDexus
To make the product show the Canon EOF product for example when you type EOF change the lines in opencart 1.5 in admin/model/catalog/product.php 334-340 to:

Code: Select all

			if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
				$sql .= " AND LCASE(pd.name) LIKE '%" . $this->db->escape(strtolower($data['filter_name'])) . "%'";
			}

			if (isset($data['filter_model']) && !is_null($data['filter_model'])) {
				$sql .= " AND LCASE(p.model) LIKE '%" . $this->db->escape(strtolower($data['filter_model'])) . "%'";
			}
I will put this as a request in the apropriate area later

Re: Product search in admin by part of the name

Posted: Fri Jun 03, 2011 9:28 am
by fabioDexus
Also i added a way to make quantity searchs for > and < like >900 etc:
Changed line 346 to:

Code: Select all

	if (isset($data['filter_quantity']) && !is_null($data['filter_quantity'])) {
				$operator='=';
				if(strpos ($data['filter_quantity'],'<')!==false || strpos ($data['filter_quantity'],'<')!==false){
					$operator='<';
				}
				else if(strpos ($data['filter_quantity'],'>')!==false || strpos ($data['filter_quantity'],'>')!==false){
					$operator='>';
				}
				$quantity=trim(str_replace(array('<','>','<','>'), '', $data['filter_quantity']));
				$sql .= ' AND p.quantity '.$operator.'\'' .$this->db->escape( $quantity). '\'';
			}

Re: Product search in admin by part of the name

Posted: Wed Jul 27, 2011 10:30 am
by davidkimm
Excellent.... the first part of your solution was what I was searching for... really liked the wildcard feature in 1.4.9.3 and bummed that it was missing in 1.5.0.5 ... your solution worked perfect..

Also, love the additional "less than or greater" search for quantity... that was the icing on the cake... its these small features that really makes a difference... two thumbs up! :drunk: thank you

ps - just curious... change that work for price too? or is price handled differently? Too lazy right now...

Re: Product search in admin by part of the name

Posted: Tue Sep 24, 2013 6:59 pm
by lloydmedley
You didn't put the code to replace and I know that some of my admin files have been modified by my theme so didn't try this method. Used this mod instead (thought I'd share as I found this thread first so others might too)

http://www.opencart.com/index.php?route ... _license=0

Re: Product search in admin by part of the name

Posted: Fri Sep 26, 2014 1:00 am
by xlam
fabioDexus wrote:Also i added a way to make quantity searchs for > and < like >900 etc:
Changed line 346 to:

Code: Select all

	if (isset($data['filter_quantity']) && !is_null($data['filter_quantity'])) {
				$operator='=';
				if(strpos ($data['filter_quantity'],'<')!==false || strpos ($data['filter_quantity'],'<')!==false){
					$operator='<';
				}
				else if(strpos ($data['filter_quantity'],'>')!==false || strpos ($data['filter_quantity'],'>')!==false){
					$operator='>';
				}
				$quantity=trim(str_replace(array('<','>','<','>'), '', $data['filter_quantity']));
				$sql .= ' AND p.quantity '.$operator.'\'' .$this->db->escape( $quantity). '\'';
			}
work for me on 1.5.5.1

Re: Product search in admin by part of the name

Posted: Thu Feb 04, 2016 2:18 am
by siteadvice
To search by a part of the product name in Opencart 2+ you need to change lines 251 - 260 like so:

Code: Select all

		if (!empty($data['filter_name'])) {
			$sql .= " AND pd.name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
		}

		if (!empty($data['filter_model'])) {
			$sql .= " AND p.model LIKE '%" . $this->db->escape($data['filter_model']) . "%'";
		}

		if (isset($data['filter_price']) && !is_null($data['filter_price'])) {
			$sql .= " AND p.price LIKE '%" . $this->db->escape($data['filter_price']) . "%'";
In a nutshell, you're just adding a % sign to the start of the search term as well as at the end of it.

Re: Product search in admin by part of the name

Posted: Thu Feb 04, 2016 3:30 am
by OSWorX
I would also add

Code: Select all

LCASE
and

Code: Select all

strtolower
(as can be seen in the first post).

Otherwise the search item has to match exactly the term.

Re: Product search in admin by part of the name

Posted: Wed Apr 13, 2016 2:36 pm
by smarg
Hello to all,
I use version 1.5.6.4,with a theme installed and this code works for me. Also, after fixing the search problem, I had a problem with the product count and shown at the end of the page: "Showing 0 to 0 of 0 (0 Pages)". If anyone else have the same problem, I show you what I did to fix it.

First I show you what I changed to fix the product search:
The only I did was to add % in the line 345: "AND pd.name LIKE '%" and in line 349 " AND p.model LIKE '%". All the code in lines 344-350 is:
if (!empty($data['filter_name'])) {
$sql .= " AND pd.name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
}

if (!empty($data['filter_model'])) {
$sql .= " AND p.model LIKE '%" . $this->db->escape($data['filter_model']) . "%'";
}

Second, I show you what I did to fix the procuct count at the end of admin page:

I changed the code in function:"public function getProfiles($product_id)" in the lines 600 to 607. Add % in line 602(" AND pd.name LIKE '%") and in line 606(" AND p.model LIKE '%"). The hole code is:

public function getProfiles($product_id) {
return $this->db->query("SELECT * FROM `" . DB_PREFIX . "product_profile` WHERE product_id = " . (int)$product_id)->rows;
}

public function getTotalProducts($data = array()) {
$sql = "SELECT COUNT(DISTINCT p.product_id) AS total FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id)";

if (!empty($data['filter_category_id'])) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id = p2c.product_id)";
}

$sql .= " WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'";

if (!empty($data['filter_name'])) {
$sql .= " AND pd.name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
}

if (!empty($data['filter_model'])) {
$sql .= " AND p.model LIKE '%" . $this->db->escape($data['filter_model']) . "%'";
}

I hope to help anyone has the same problem.

Re: Product search in admin by part of the name

Posted: Sun Sep 30, 2018 8:52 am
by radi8tor
smarg wrote:
Wed Apr 13, 2016 2:36 pm
Also, after fixing the search problem, I had a problem with the product count and shown at the end of the page: "Showing 0 to 0 of 0 (0 Pages)". If anyone else have the same problem, I show you what I did to fix it.
Thanks!