Zen Cart Logo
Forums / General Questions / Advanced search result display (custom field)

Advanced search result display (custom field)

Views: 13,957

Results 1 to 13 of 13
4 Jun 2016, 10:42 PM
#1
chadlly2003 avatar

chadlly2003

Totally Zenned

Join Date:
Jan 2015
Posts:
787
Plugin Contributions:
0

Advanced search result display (custom field)

When someone does a search it displays the name and model number of each product.

Example:

Product Name: product example 1
Product Model Number: 123

I have a custom field in the products table called UPC.

Is there a way when someone does a search I could add the custom field to display the results below:

Product Name: product example 1
Product Model Number: 123
Product UPC: 0123456789123

I have done some research and know the files I have to edit consist of
/include/modules/pages/advanced_search_results/header_php.php
/include/modules/pages/advanced_search/header_php.php

If someone could assist me or lead me in the right direction on solving this I would greatly appreciate it.

4 Jun 2016, 10:53 PM
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,102
Plugin Contributions:
56

Re: Advanced search result display (custom field)

Is the field in the database's ***products ***table names specifically UPC?

4 Jun 2016, 11:22 PM
#3
chadlly2003 avatar

chadlly2003

Totally Zenned

Join Date:
Jan 2015
Posts:
787
Plugin Contributions:
0

Re: Advanced search result display (custom field)

Yes in the database under products it is called UPC

5 Jun 2016, 12:40 AM
#4
chadlly2003 avatar

chadlly2003

Totally Zenned

Join Date:
Jan 2015
Posts:
787
Plugin Contributions:
0

Re: Advanced search result display (custom field)

Just to be more specific when you do a search the product listing page appears like this

Product Name: product example 1
Product Model Number: 123

What I need displayed is (see below)
I have a custom field under products_upc in my database)

Product Name: product example 1
Product Model Number: 123
Product UPC: 0123456789123

5 Jun 2016, 12:48 PM
#5
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: Advanced search result display (custom field)

chadlly2003:

/include/modules/pages/advanced_search_results/header_php.php
That's the file for including the field when searching the db. (EDIT: Or use the observer class idea as lat9 posted below)

To display the field's content, you'll need to edit /includes/modules/name_of_your_template/product_listing.php
and perhaps /includes/index_filters/default_filter.php
and maybe /includes/modules/pages/index/main_template_vars.php
... depending on what you're trying to accomplish.

.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

5 Jun 2016, 1:01 PM
#6
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,102
Plugin Contributions:
56

Re: Advanced search result display (custom field)

Since you're running Zen Cart 1.5.4, you have the capability to use an "auto-loaded observer".

Create the file /includes/classes/observers/auto.search_in_upc.php, containing:

<?php
/**
 * @package plugins
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: Designed for v1.5.4  $
 */

class zcObserverSearchInUpc extends base 
{
    public function __construct() 
    {
      $this->attach ($this, array ('NOTIFY_SEARCH_WHERE_STRING'));
    }

    public function update (&$class, $eventID) 
    {
        switch ($eventID) {
            case 'NOTIFY_SEARCH_WHERE_STRING': 
                global $where_str, $keywords;
                if (isset ($keywords) && zen_not_null ($keywords)) {
                    if (zen_parse_search_string (stripslashes ($keywords), $search_keywords)) {
                        $keyword_str = " OR (p.UPC != '' AND (";
                        for ($i=0, $n = sizeof ($search_keywords); $i<$n; $i++ ) {
                            switch ($search_keywords[$i]) {
                                case '(':
                                case ')':
                                case 'and':
                                case 'or':
                                    $keyword_str .= " " . $search_keywords[$i] . " ";
                                    break;
                                default:
                                    $keyword_str .= " p.UPC LIKE '%:keywords:%' ";
                                    $keyword_str = $db->bindVars ($keyword_str, ':keywords:', $search_keywords[$i], 'noquotestring');
                                    break;
                            }
                        }
                        $keyword_str .= " ) )";
                        $where_str = str_replace ('))', $keyword_str . ' ))', $where_str);
                    }
                }
                break;
            default:
                break;
        }
    }
}

That code "intercepts" the notifier raised by /includes/modules/pages/advanced_search_result/header_php.php and adds the UPC field in the (already included) products table (aliased as p) to the list of fields to be searched.

5 Jun 2016, 4:23 PM
#7
chadlly2003 avatar

chadlly2003

Totally Zenned

Join Date:
Jan 2015
Posts:
787
Plugin Contributions:
0

Re: Advanced search result display (custom field)

lat9

Thank you for the quick response.
I tried your snippet of code but it does not seem to work. When your code is implemented I get a blank page when i do a search.

5 Jun 2016, 5:14 PM
#8
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,102
Plugin Contributions:
56

Re: Advanced search result display (custom field)

OK, check your /logs folder. There should be a myDEBUG*.log file that identifies the source of the error. If you'll post that (using the "code" tags # in the posting header), it'll give me something to help.

5 Jun 2016, 5:45 PM
#9
chadlly2003 avatar

chadlly2003

Totally Zenned

Join Date:
Jan 2015
Posts:
787
Plugin Contributions:
0

Re: Advanced search result display (custom field)

here is the error report

[05-Jun-2016 13:43:50 America/New_York] PHP Fatal error: Call to a member function bindVars() on a non-object in C:\xampp\htdocs\xxxxxx.com\includes\classes\observers\auto.search_in_upc.php on line 33

5 Jun 2016, 5:53 PM
#10
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,102
Plugin Contributions:
56

Re: Advanced search result display (custom field)

Here's the updated version:

<?php
/**
 * @package plugins
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: Designed for v1.5.4  $
 */

class zcObserverSearchInUpc extends base 
{
    public function __construct() 
    {
      $this->attach ($this, array ('NOTIFY_SEARCH_WHERE_STRING'));
    }

    public function update (&$class, $eventID) 
    {
        switch ($eventID) {
            case 'NOTIFY_SEARCH_WHERE_STRING': 
                global $where_str, $keywords[B], $db[/B];
                if (isset ($keywords) && zen_not_null ($keywords)) {
                    if (zen_parse_search_string (stripslashes ($keywords), $search_keywords)) {
                        $keyword_str = " OR (p.UPC != '' AND (";
                        for ($i=0, $n = sizeof ($search_keywords); $i<$n; $i++ ) {
                            switch ($search_keywords[$i]) {
                                case '(':
                                case ')':
                                case 'and':
                                case 'or':
                                    $keyword_str .= " " . $search_keywords[$i] . " ";
                                    break;
                                default:
                                    $keyword_str .= " p.UPC LIKE '%:keywords:%' ";
                                    $keyword_str = $db->bindVars ($keyword_str, ':keywords:', $search_keywords[$i], 'noquotestring');
                                    break;
                            }
                        }
                        $keyword_str .= " ) )";
                        $where_str = str_replace ('))', $keyword_str . ' ))', $where_str);
                    }
                }
                break;
            default:
                break;
        }
    }
}
5 Jun 2016, 6:10 PM
#11
chadlly2003 avatar

chadlly2003

Totally Zenned

Join Date:
Jan 2015
Posts:
787
Plugin Contributions:
0

Re: Advanced search result display (custom field)

I put the new script in as you gave me above. But for some reason the upc is still not displaying. Am I missing something here?

5 Jun 2016, 6:19 PM
#12
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,102
Plugin Contributions:
56

Re: Advanced search result display (custom field)

That script is not going to make the UPC "display", but it will include product(s) that match that UPC entry as part of the search results.

5 Jun 2016, 8:50 PM
#13
chadlly2003 avatar

chadlly2003

Totally Zenned

Join Date:
Jan 2015
Posts:
787
Plugin Contributions:
0

Re: Advanced search result display (custom field)

Lat9

Just wanted to thank you for your efforts. Yes you are correct the script completes the search aspect of it.

But I am still stuck on my original issue of when you do a search the upc is not added to the product list page. Any ideas would be greatly appreciated.