Zen Cart Logo
Forums / Templates, Stylesheets, Page Layout / Search Redirect if only one result found

Search Redirect if only one result found

Locked

Views: 5,205

Results 1 to 19 of 19
This thread is locked. New replies are disabled.
19 Jan 2008, 4:40 PM
#1
pastelblack avatar

pastelblack

New Zenner

Join Date:
Nov 2007
Posts:
22
Plugin Contributions:
0

Search Redirect if only one result found

If someone uses the search function and only one matching product is found, can the search result redirect to that product's page automatically?

I've searched around a bit, but can't find how to do this.

Thanks in advance!!!!

19 Jan 2008, 4:45 PM
#2
kuroi avatar

kuroi

Totally Zenned

Join Date:
Apr 2006
Location:
London, UK
Posts:
10,475
Plugin Contributions:
11

Re: Search Redirect if only one result found

At the very bottom of includes/modules/pages/advanced_search_result/header_php.php is a piece of code that redirects visitors abck to the advanced search page if there are no products found. You would want to add a similar redirect that says "Aha, only one? then go to the product info page instead!" (but in PHP)

Kuroi Web Design and Development | Twitter

(Questions answered in the forum only - so that any forum member can benefit - not by personal message)

19 Jan 2008, 5:07 PM
#3
pastelblack avatar

pastelblack

New Zenner

Join Date:
Nov 2007
Posts:
22
Plugin Contributions:
0

Re: Search Redirect if only one result found

Yeah, except I don't know very much php. I see the file you're talking about, but I don't know how to modify it to do what you said. :(

19 Jan 2008, 5:39 PM
#4
drbyte avatar

drbyte

Sensei

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

Re: Search Redirect if only one result found

At the bottom of that file, you have a section of code similar to the following.
Simply add the additional lines highlighted here:```
$result = new splitPageResults($listing_sql, MAX_DISPLAY_PRODUCTS_LISTING, 'p.products_id', 'page');
if ($result->number_of_rows == 0) {
$messageStack->add_session('search', TEXT_NO_PRODUCTS, 'caution');
zen_redirect(zen_href_link(FILENAME_ADVANCED_SEARCH, zen_get_all_get_params('action')));
}
[B]// if only one product found in search results, go directly to the product page, instead of displaying a link to just one item:
if ($result->number_of_rows == 1 && SKIP_SINGLE_PRODUCT_CATEGORIES == 'True') {
$result = $db->Execute($listing_sql);
zen_redirect(zen_href_link(zen_get_info_page($result->fields['products_id']), 'cPath=' . zen_get_product_path($result->fields['products_id']) . '&products_id=' . $result->fields['products_id']));
}

[/B]
// This should be last line of the script:
$zco_notifier->notify('NOTIFY_HEADER_END_ADVANCED_SEARCH_RESULTS', $keywords);
//EOF

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

19 Jan 2008, 6:16 PM
#5
pastelblack avatar

pastelblack

New Zenner

Join Date:
Nov 2007
Posts:
22
Plugin Contributions:
0

Re: Search Redirect if only one result found

That didn't quite work. I have ZenCart 1.3.7.1. The heading of the file (/includes/modules/pages/advanced_search_result/header_php.php) says:

 @version $Id: header_php.php 4857 2006-10-28 17:40:08Z ajeh $

and the end of the file's code is as follows:

$listing_sql = $select_str . $from_str . $where_str . $order_str;
// Notifier Point
$zco_notifier->notify('NOTIFY_SEARCH_ORDERBY_STRING');
$breadcrumb->add(NAVBAR_TITLE_1, zen_href_link(FILENAME_ADVANCED_SEARCH));
$breadcrumb->add(NAVBAR_TITLE_2);

// This should be last line of the script:
$zco_notifier->notify('NOTIFY_HEADER_END_ADVANCED_SEARCH_RESULTS');
?>

Am I looking at the wrong file?

Thanks again for your help!!!

19 Jan 2008, 7:02 PM
#6
drbyte avatar

drbyte

Sensei

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

Re: Search Redirect if only one result found

Sorry, that was based on 1.3.8

Just put it above the last notifier section:```
// This should be last line of the script:
$zco_notifier->notify('NOTIFY_HEADER_END_ADVANCED_SEARCH_RESULTS', $keywords);

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

19 Jan 2008, 9:19 PM
#7
pastelblack avatar

pastelblack

New Zenner

Join Date:
Nov 2007
Posts:
22
Plugin Contributions:
0

Re: Search Redirect if only one result found

if I add only the lines that are in red, I get this:

Home :: Advanced Search :: Search Results :: Advanced Search :: Search Results
Advanced Search
1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order by p.products_sort_order, pd.products_name limit 0, 10' at
in:
[SELECT DISTINCT p.products_image, p.products_quantity , m.manufacturers_id, p.products_id, pd.products_name, p.products_price, p.products_tax_class_id, p.products_price_sorter, p.products_qty_box_status FROM (products p LEFT JOIN manufacturers m USING(manufacturers_id), products_description pd, categories c, products_to_categories p2c ) LEFT JOIN meta_tags_products_description mtpd ON mtpd.products_id= p2c.products_id AND mtpd.language_id = 1 WHERE (p.products_status = 1 AND p.products_id = pd.products_id AND pd.language_id = 1 AND p.products_id = p2c.products_id AND p2c.categories_id = c.categories_id AND ((pd.products_name LIKE '%hp%' OR p.products_model LIKE '%hp%' OR m.manufacturers_name LIKE '%hp%' OR (mtpd.metatags_keywords LIKE '%hp%' AND mtpd.metatags_keywords !='') OR (mtpd.metatags_description LIKE '%hp%' AND mtpd.metatags_description !='') OR pd.products_description LIKE '%hp%') )) order by p.products_sort_order, pd.products_name order by p.products_sort_order, pd.products_name limit 0, 10]

(I included the breadcrumbs in hopes that it might help in figuring out what's wrong.)

If I add everything in the box (replacing the last line - this line is still slightly different than what you indicate it should be), it works when only one product is returned, but gives the error message when multiple products should be returned.

I'm still stuck...

20 Jan 2008, 6:28 AM
#8
drbyte avatar

drbyte

Sensei

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

Re: Search Redirect if only one result found

For 1.3.7.1, change this:

$listing_sql = $select_str . $from_str . $where_str . $order_str;
// Notifier Point
$zco_notifier->notify('NOTIFY_SEARCH_ORDERBY_STRING');
$breadcrumb->add(NAVBAR_TITLE_1, zen_href_link(FILENAME_ADVANCED_SEARCH));
$breadcrumb->add(NAVBAR_TITLE_2);

// This should be last line of the script:
$zco_notifier->notify('NOTIFY_HEADER_END_ADVANCED_SEARCH_RESULTS');
?>

to this:```
$listing_sql = $select_str . $from_str . $where_str . $order_str;
// Notifier Point
$zco_notifier->notify('NOTIFY_SEARCH_ORDERBY_STRING');
$breadcrumb->add(NAVBAR_TITLE_1, zen_href_link(FILENAME_ADVANCED_SEARCH));
$breadcrumb->add(NAVBAR_TITLE_2);

[B]
// if only one product found in search results, go directly to the product page, instead of displaying a link to just one item:
$result = new splitPageResults($listing_sql, MAX_DISPLAY_PRODUCTS_LISTING, 'p.products_id', 'page');
if ($result->number_of_rows == 1 && SKIP_SINGLE_PRODUCT_CATEGORIES == 'True') {
$result = $db->Execute($listing_sql);
zen_redirect(zen_href_link(zen_get_info_page($result->fields['products_id']), 'cPath=' . zen_get_product_path($result->fields['products_id']) . '&products_id=' . $result->fields['products_id']));
}
[/B]

// This should be last line of the script:
$zco_notifier->notify('NOTIFY_HEADER_END_ADVANCED_SEARCH_RESULTS');
?>

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

20 Jan 2008, 4:39 PM
#9
pastelblack avatar

pastelblack

New Zenner

Join Date:
Nov 2007
Posts:
22
Plugin Contributions:
0

Re: Search Redirect if only one result found

You didn't indicate what to change, so I just added the code. This resulted in the same error.

Can we try one more time?

I really appreciate all of your help!

20 Jan 2008, 4:49 PM
#10
pastelblack avatar

pastelblack

New Zenner

Join Date:
Nov 2007
Posts:
22
Plugin Contributions:
0

Re: Search Redirect if only one result found

If it helps, my site is: VictoryToner.com/catalog

Search for C6656AN to return 1 product (redirect works)

Search for HP toner to return multiple products (error message)

21 Jan 2008, 8:22 AM
#11
drbyte avatar

drbyte

Sensei

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

Re: Search Redirect if only one result found

PastelBlack:

You didn't indicate what to change, so I just added the code. This resulted in the same error.

Can we try one more time?

I really appreciate all of your help!

Oops ... I've updated my post above.

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

21 Jan 2008, 4:24 PM
#12
pastelblack avatar

pastelblack

New Zenner

Join Date:
Nov 2007
Posts:
22
Plugin Contributions:
0

Re: Search Redirect if only one result found

Dr Byte, I really do appreciate your time and your help!

I am still getting the same error with the repeated breadcrumbs.

You can see what I'm talking about here: VictoryToner.com/catalog

Search for C6656AN to return 1 product (redirect works)

Search for HP toner to return multiple products (error message)

I'm using ZC v1.3.7.1 with a few mods (Customer Tax Exempt v1.1.0, Automatic Login v1.2.1, Encrypted Master Password v1.1, Editable Sidebox v1.0, Search Log v2.0, and Group Pricing Per Item v1.2.1a), so I don't know if the problem lies here.

I would really like to get this to work. :down:

21 Jan 2008, 4:28 PM
#13
drbyte avatar

drbyte

Sensei

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

Re: Search Redirect if only one result found

... and if you put the code back to the original from before the edits I suggested, what happens?

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

21 Jan 2008, 5:11 PM
#14
pastelblack avatar

pastelblack

New Zenner

Join Date:
Nov 2007
Posts:
22
Plugin Contributions:
0

Re: Search Redirect if only one result found

Haha... Your post got me thinking...

So I went back to my last backup (a few days ago) and re-uploaded that file before ANY changes were made. It worked the way it originally did.

I then made the change that you suggested and re-uploaded, and it works. :clap:

I must've screwed something up before, but now with the fresh upload it appears to be working.

You are awesome! Many, many thanks!

1 Jul 2008, 2:32 PM
#15
zskiman avatar

zskiman

New Zenner

Join Date:
Mar 2005
Posts:
69
Plugin Contributions:
0

Re: Search Redirect if only one result found

Thanks for this little snippet, works great especially when searches are done on isbn # where there is only a possability of one. again thanks alot.

Never be afraid to try something new, remember that amateurs built the ark, professionals built the Titanic

20 Jul 2010, 12:03 AM
#16
kiddo avatar

kiddo

Totally Zenned

Join Date:
Jul 2006
Location:
SF Bay Area
Posts:
837
Plugin Contributions:
1

Re: Search Redirect if only one result found

DrByte:

At the bottom of that file, you have a section of code similar to the following.
Simply add the additional lines highlighted here:```
$result = new splitPageResults($listing_sql, MAX_DISPLAY_PRODUCTS_LISTING, 'p.products_id', 'page');
if ($result->number_of_rows == 0) {
$messageStack->add_session('search', TEXT_NO_PRODUCTS, 'caution');
zen_redirect(zen_href_link(FILENAME_ADVANCED_SEARCH, zen_get_all_get_params('action')));
}
[B]// if only one product found in search results, go directly to the product page, instead of displaying a link to just one item:
if ($result->number_of_rows == 1 && SKIP_SINGLE_PRODUCT_CATEGORIES == 'True') {
$result = $db->Execute($listing_sql);
zen_redirect(zen_href_link(zen_get_info_page($result->fields['products_id']), 'cPath=' . zen_get_product_path($result->fields['products_id']) . '&products_id=' . $result->fields['products_id']));
}

[/B]
// This should be last line of the script:
$zco_notifier->notify('NOTIFY_HEADER_END_ADVANCED_SEARCH_RESULTS', $keywords);
//EOF


I know its an old post, but was just what I was looking for!

Works in 1.3.9d too :)

Thanks Dr. Byte!
20 Jul 2010, 3:44 AM
#17
kenichi avatar

kenichi

Zen Follower

Join Date:
Oct 2008
Posts:
252
Plugin Contributions:
0

Re: Search Redirect if only one result found

Thanks for bumping the thread, that's a great addition :P

20 Jul 2010, 11:15 AM
#18
jk10 avatar

jk10

New Zenner

Join Date:
Jun 2010
Posts:
7
Plugin Contributions:
0

Re: Search Redirect if only one result found

Exactly what I needed. Just applied this to 1.39d and works great, thanks.

20 Jul 2010, 6:18 PM
#19
llynix avatar

llynix

Zen Follower

Join Date:
Jul 2009
Location:
Texas
Posts:
210
Plugin Contributions:
0

Re: Search Redirect if only one result found

Jk10:

Exactly what I needed. Just applied this to 1.39d and works great, thanks.

I think it should be in 1.3.9e

DentalPlanet Designed/Administrated by Falls Geek