Page 1 of 2 12 LastLast
Results 1 to 10 of 13
  1. #1
    Join Date
    Jan 2015
    Posts
    721
    Plugin Contributions
    1

    Default 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.

  2. #2
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,896
    Plugin Contributions
    96

    Default Re: Advanced search result display (custom field)

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

  3. #3
    Join Date
    Jan 2015
    Posts
    721
    Plugin Contributions
    1

    Default Re: Advanced search result display (custom field)

    Yes in the database under products it is called UPC

  4. #4
    Join Date
    Jan 2015
    Posts
    721
    Plugin Contributions
    1

    Default 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. #5
    Join Date
    Jan 2004
    Posts
    66,444
    Plugin Contributions
    279

    Default Re: Advanced search result display (custom field)

    Quote Originally Posted by chadlly2003 View Post
    /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.


    .
    Last edited by DrByte; 5 Jun 2016 at 07:59 PM. Reason: add note about lat9 observer idea
    .

    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.

  6. #6
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,896
    Plugin Contributions
    96

    Default 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:
    Code:
    <?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.
    Last edited by lat9; 5 Jun 2016 at 02:01 PM. Reason: Correct misspelling

  7. #7
    Join Date
    Jan 2015
    Posts
    721
    Plugin Contributions
    1

    Default 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.

  8. #8
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,896
    Plugin Contributions
    96

    Default 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.

  9. #9
    Join Date
    Jan 2015
    Posts
    721
    Plugin Contributions
    1

    Default 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

  10. #10
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,896
    Plugin Contributions
    96

    Default Re: Advanced search result display (custom field)

    Here's the updated version:
    Code:
    <?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, $db;
                    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;
            }
        }
    }

 

 
Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 29 Aug 2013, 04:46 AM
  2. Replies: 5
    Last Post: 1 Oct 2011, 08:04 PM
  3. Problematic code in advanced search result ?
    By angeloio in forum Bug Reports
    Replies: 6
    Last Post: 3 Mar 2011, 04:52 PM
  4. css override on advanced search result
    By weirdorecords in forum Templates, Stylesheets, Page Layout
    Replies: 2
    Last Post: 24 Jun 2010, 12:19 AM
  5. Custom Field in Advanced search
    By SteveDuncan in forum Templates, Stylesheets, Page Layout
    Replies: 3
    Last Post: 9 Apr 2010, 02:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg