Zen Cart Logo
Forums / General Questions / Isolating Part Number in Search Term

Isolating Part Number in Search Term

Views: 217

Results 1 to 16 of 16
17 Jul 2026, 2:31 AM
#1
bumba000 avatar

bumba000

Totally Zenned

Join Date:
Feb 2007
Location:
Pennsylvania
Posts:
856
Plugin Contributions:
0

Isolating Part Number in Search Term

Hey All,
In ZC 2.2.2. At a complete loss here. So customers often use very long search terms which do include a model number. What I figured would be pretty simple is actually turning out to be quite a challenge. I've tried working in the includes/modules/pages/search_result/header_php.php as well as classes/class.search.php and I could really use some help here.

This was my plan to basically find a model number included in a search term and essentially change the search term to be just that model number.

$kewordTerms = explode(' ', $_GET['keyword']);
foreach($kewordTerms as $thisKeyword){
    $modelMatch = $db->Execute("SELECT products_id as COUNT from products where products_model = '".$thisKeyword."'");
    if($modelMatch){$keywords= $thisKeyword;}
}

For example, we have a customer search for microsoft wireless mouse MS12399 for win11 desktop pc. Now we do carry the MS12399, but the customer is taken to the search page and given the message "Warning There is no product that matches the search criteria." I would rather he were taken to the product page for the MS12399 mouse.

Please help.

Thank you,
John

17 Jul 2026, 11:01 AM
#2
lat9 avatar

lat9

Administrator

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

Re: Isolating Part Number in Search Term

You could try changing the ste's default search operator (see the admin's Configuration / My Store / Default Search Operator) from **and **to or.

17 Jul 2026, 11:24 AM
#3
bumba000 avatar

bumba000

Totally Zenned

Join Date:
Feb 2007
Location:
Pennsylvania
Posts:
856
Plugin Contributions:
0

Re: Isolating Part Number in Search Term

Hey lat9,
I realized after posting that I had absolutely butchered that code example. lol

I didn't know those settings were in the admin. So made change from 'and' to 'or' and also not include metas. Now there are products returned rather than the no products message, but still the MS12399 mouse is nowhere to be seen, just lost in a sea of WIN11 operating systems, new desktop pcs and everything wireless.

It would be nice to have the only product model included in the search term that matches exactly a product model in our db to be shown at the top of the list of results. Really it'd be nice to have the search react as though that were the only thing in the search term and take the visitor directly to that matched products info page.

I may take another run at this today. Maybe without the bourbon. lol

Any help here would be greatly appreciated.

Thank you,
John

17 Jul 2026, 12:27 PM
#4
brittainmark avatar

brittainmark

Totally Zenned

Join Date:
Apr 2009
Posts:
507
Plugin Contributions:
1

Re: Isolating Part Number in Search Term

The issue with your code is that class.search.php sets the search keywords using $_GET['keyword'] if you modify your code to set this it should work. I would not recommend changing the core code. But if you were to you could change your code to redirect directly to the product page. A better solution if you want to go straight to the product page would be to write an observer.

If you are happy to change the core code plug this in your search_results header.php. This will redirect to the first product with a model number that matches a keyword.

$keywords = trim(stripslashes(isset($_GET['keyword']) ? zen_output_string_protected($_GET['keyword']) : ''));
if ($keywords !== '') {
    $kewordTerms = explode(' ', $keywords);

    foreach($kewordTerms as $thisKeyword){
        $modelMatch = $db->Execute("SELECT products_id  FROM " . TABLE_PRODUCTS . " WHERE products_model = '" . $thisKeyword . "'");
        if(!$modelMatch->EOF){
            $match_id = $modelMatch->fields['products_id'];
            zen_redirect(zen_href_link(zen_get_info_page($match_id), 'products_id=' . $match_id));
            break;
        }
    }
}
17 Jul 2026, 12:36 PM
#5
bumba000 avatar

bumba000

Totally Zenned

Join Date:
Feb 2007
Location:
Pennsylvania
Posts:
856
Plugin Contributions:
0

Re: Isolating Part Number in Search Term

brittainmark:

The issue with your code is that class.search.php sets ...

DUDE! Thank you! Placed above use Zencart\Exceptions\SearchException; in includes/pages/search_result/header_php.php and it works. I'll test it out a while before adding to production site, but I do believe we have a winner here.

Thank you So much!

Best regards,
John

17 Jul 2026, 1:39 PM
#6
pilou2 avatar

pilou2

Zen Follower

Join Date:
Jun 2008
Location:
Japan
Posts:
366
Plugin Contributions:
6

Re: Isolating Part Number in Search Term

Searching by product model works perfectly in default ZC v2.2.2, modifying core code is unnecessary.
When searching operator is set to 'and' in admin, anything in search field that is not either in product name, product model or manufacturer name would prevent you from getting any result. But if you set the operator to 'or' then, if the correct product model is in the search field you should get your result even if other stuff were entered together.
If it is not the case, then most probably there is something interfering, a plugin or a core code modification to fix another problem...

17 Jul 2026, 1:54 PM
#7
brittainmark avatar

brittainmark

Totally Zenned

Join Date:
Apr 2009
Posts:
507
Plugin Contributions:
1

Re: Isolating Part Number in Search Term

Liked the idea, so I have created an auto observer. If you already have a set of your own auto observers just add the Observer name 'NOTIFY_HEADER_START_ADVANCED_SEARCH_RESULTS' to the attach list and put the function updateNotifyHeaderStartAdvancedSearchResults into your file.
If not create a file called auto.mySearchObserver.php in includes/classes/observers and paste in the code. Below is slightly different as if there are more than one model number entered in the search it returns the list of model numbers (space separated) to the search. One side effect of this is that the original query entered is lost and replaced with just the model numbers.

<?php
class zcObserverMySearchObserver extends base {

    public function __construct() {
        $this->attach(
            $this,
            [
                 'NOTIFY_HEADER_START_ADVANCED_SEARCH_RESULTS',
            ]
        );
    }
    /*
     * Observer to redirect to the product page if a single model number is entered in a set of search key words.
     * If multiple model numbers are entered then set the search keywords to the model numbers entered.
     * IMPORTANT for this to work for multiple model numbers you must have set
     *    Configuration->My Store->Default Search Operator to "or".
     */
    protected function updateNotifyHeaderStartAdvancedSearchResults(&$class, $eventID) {
        global $db;
        $keywords = trim(stripslashes(isset($_GET['keyword']) ? zen_output_string_protected($_GET['keyword']) : ''));
        if ($keywords !== '') {
            $match_id = [];
            $match_keyword = [];
            $kewordTerms = explode(' ', $keywords);
            foreach($kewordTerms as $thisKeyword){
                $modelMatch = $db->Execute("SELECT products_id  FROM " . TABLE_PRODUCTS . " WHERE products_model = '" . $thisKeyword . "'");
                if(!$modelMatch->EOF){
                    $match_id [] = $modelMatch->fields['products_id'];
                    $match_keyword[] = $thisKeyword;
                }
            }
            switch (count($match_id)) {
                case 0:
                    // No model nos so do nothing
                    return;
                    break;
                case 1:
                    // Just one so redirect to pade
                    zen_redirect(zen_href_link(zen_get_info_page($match_id[0]), 'products_id=' . $match_id[0]));
                    break;
                default:
                    // Multiple so preplace search with Model no.s
                    $_GET['keyword'] = implode(' ', $match_keyword);
            }
        }
    }
}
17 Jul 2026, 2:03 PM
#8
bumba000 avatar

bumba000

Totally Zenned

Join Date:
Feb 2007
Location:
Pennsylvania
Posts:
856
Plugin Contributions:
0

Re: Isolating Part Number in Search Term

pilou2:

Searching by product model works perfectly in default ZC v2.2.2, modifying core ...

Yes, searching the model number works perfectly, but when a customer searches for MS12399 using a term like "microsoft wireless mouse MS12399 for win11 desktop pc.", it doesn't work perfectly.

17 Jul 2026, 2:04 PM
#9
brittainmark avatar

brittainmark

Totally Zenned

Join Date:
Apr 2009
Posts:
507
Plugin Contributions:
1

Re: Isolating Part Number in Search Term

pilou2:

Searching by product model works perfectly in default ZC v2.2.2, modifying core code is unnecessary.
When searching operator is set to 'and' in admin, anything in search field that is not either in product name, product model or manufacturer name would prevent you from getting any result. But if you set the operator to 'or' then, if the correct product model is in the search field you should get your result even if other stuff were entered together.
If it is not the case, then most probably there is something interfering, a plugin or a core code modification to fix another problem...

Thats fine if the customer does not enter a lot of other words that may also match products. In that case the item can get lost or not show up depending on how many prodducts you return in the search. The observers above will just return the items with that model number.

17 Jul 2026, 2:05 PM
#10
bumba000 avatar

bumba000

Totally Zenned

Join Date:
Feb 2007
Location:
Pennsylvania
Posts:
856
Plugin Contributions:
0

Re: Isolating Part Number in Search Term

brittainmark:

Liked the idea, so I have created an auto observer....

This should be a core part of zencart. On much older version of another site, I used to use jquery to suggest search terms that matched models as the user typed. Giving them essentially a link to click in the suggestion rather than even running the search.

Thanks again. This will work Great!

Best,
John

17 Jul 2026, 2:52 PM
#11
pilou2 avatar

pilou2

Zen Follower

Join Date:
Jun 2008
Location:
Japan
Posts:
366
Plugin Contributions:
6

Re: Isolating Part Number in Search Term

There is a problem with long search strings. The actual ZC seems to be work fine up to 30 characters...
The problem is NOT with model number. There are lots of filtering in ZC for user input, something needs to be fix there.

17 Jul 2026, 3:40 PM
#12
pilou2 avatar

pilou2

Zen Follower

Join Date:
Jun 2008
Location:
Japan
Posts:
366
Plugin Contributions:
6

Re: Isolating Part Number in Search Term

Actually, the only problem I found is when you want to search using default search field in header, it is limited to 32 characters, at least in Responsive Classic Template.
When using advanced search, everything will work as expected. If you let the default option 'Search in product description' checked, you will get tones of unwanted results, which is expected when using keywords like 'of'.
The problem is that if you use ZC search like an AI search fiding it with as much info as possible, you can't have good result. If you want to use long search strings, the best solution would be to modify code in template so that 'Search In Product Descriptions' is unchecked by default.

17 Jul 2026, 3:50 PM
#13
pilou2 avatar

pilou2

Zen Follower

Join Date:
Jun 2008
Location:
Japan
Posts:
366
Plugin Contributions:
6

Re: Isolating Part Number in Search Term

In these files:

includes/templates/YOUR_TEMPLATE_NAME/sideboxes/tpl_search_header.php
includes/templates/YOUR_TEMPLATE_NAME/sideboxes/tpl_search.php
includes/templates/YOUR_TEMPLATE_NAME/templates/tpl_advanced_search_default.php

There is some code like this:
zen_draw_checkbox_field('search_in_description', '1', true)

Just replace true by false or remove ", true" like this:
zen_draw_checkbox_field('search_in_description', '1')

17 Jul 2026, 4:13 PM
#14
pilou2 avatar

pilou2

Zen Follower

Join Date:
Jun 2008
Location:
Japan
Posts:
366
Plugin Contributions:
6

Re: Isolating Part Number in Search Term

pilou2:

In these files:

includes/templates/YOUR_TEMPLATE_NAME/sideboxes/tpl_search_header.php
includes/templates/YOUR_TEMPLATE_NAME/sideboxes/tpl_search.php
includes/templates/YOUR_TEMPLATE_NAME/templates/tpl_advanced_search_default.php

> There is some code like this:
> zen_draw_checkbox_field('search_in_description', '1', true)
> 
> Just replace true by false or remove ", true" like this:
> zen_draw_checkbox_field('search_in_description', '1')

Update for files modifications:
For standard sidebox and header search files modification are like this:
From
$content .= zen_draw_hidden_field('search_in_description', '1') . zen_hide_session_id();
To
$content .= zen_draw_hidden_field('search_in_description', '0') . zen_hide_session_id();
17 Jul 2026, 4:26 PM
#15
pilou2 avatar

pilou2

Zen Follower

Join Date:
Jun 2008
Location:
Japan
Posts:
366
Plugin Contributions:
6

Re: Isolating Part Number in Search Term

And finally, for advanced search, file to modify is:
includes/modules/pages/search/header_php.php
On line 18, change to:
$sData['search_in_description'] = (isset($_GET['search_in_description']) ? zen_output_string((int)$_GET['search_in_description']) : 0);

17 Jul 2026, 6:14 PM
#16
lat9 avatar

lat9

Administrator

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

Re: Isolating Part Number in Search Term

pilou2:

And finally, for advanced search, file to modify is:
includes/modules/pages/search/header_php.php
On line 18, change to:
$sData['search_in_description'] = (isset($_GET['search_in_description']) ? zen_output_string((int)$_GET['search_in_description']) : 0);
I'll just note that making changes to core Zen Cart files is "frowned upon" as it makes the site more likely to lose its customizations on upgrade.