Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls

Add a true Google search to your X-cart.

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions
 
Thread Tools Search this Thread
  #1  
Old 03-17-2003, 11:45 AM
  minorgod's Avatar 
minorgod minorgod is offline
 

X-Adept
  
Join Date: Sep 2002
Location: Arivaca, AZ
Posts: 402
 

Default Add a true Google search to your X-cart.

Hello all,
I've just finished adding a site-specific google-search to our advanced search page. For anyone interested, here's how to do it for x-cart 3.3.1...please note that I've already made fix for the price search bug in version 3.3.1:

First you need to sign up for a Google API key at:
http://www.google.com/apis/

Then, you need to download the excellent google search class and supporting files from the original author (written in French). You can find them here:

ftp://ftp2.xhtml.net/xhtml/php/google.zip

Then extract the files you downloaded and upload everything except index.php into your /customer/ folder.

So you should have copied the following extracted files from the google.zip file to the /customer/ folder:
SOAP_Google.php
PEAR.php
The entire SOAP directory

Then you should replace your customer/search.php page with the following code:
Code:
<? /*****************************************************************************\ +-----------------------------------------------------------------------------+ | X-Cart | | Copyright (c) 2001-2002 Ruslan R. Fazliev. All rights reserved. | +-----------------------------------------------------------------------------+ | The Ruslan R. Fazliev forbids, under any circumstances, the unauthorized | | reproduction of software or use of illegally obtained software. Making | | illegal copies of software is prohibited. Individuals who violate copyright | | law and software licensing agreements may be subject to criminal or civil | | action by the owner of the copyright. | | | | 1. It is illegal to copy a software, and install that single program for | | simultaneous use on multiple machines. | | | | 2. Unauthorized copies of software may not be used in any way. This applies | | even though you yourself may not have made the illegal copy. | | | | 3. Purchase of the appropriate number of copies of a software is necessary | | for maintaining legal status. | | | | DISCLAIMER | | | | THIS SOFTWARE IS PROVIDED BY Ruslan R. Fazliev ``AS IS'' AND ANY | | EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | | DISCLAIMED. IN NO EVENT SHALL Ruslan R. Fazliev OR ITS | | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | | | | The Initial Developer of the Original Code is Ruslan R. Fazliev. | | Portions created by Ruslan R. Fazliev are Copyright (C) 2001-2002 | | Ruslan R. Fazliev. All Rights Reserved. | +-----------------------------------------------------------------------------+ \*****************************************************************************/ # # $Id: search.php,v 1.35 2002/11/14 07:58:18 zorg Exp $ # require "../smarty.php"; require "../config.php"; @include "./https.php"; require "./auth.php"; require "../include/categories.php"; $tmp=strstr($QUERY_STRING, "PHPSESSID="); if (!empty($tmp)) $QUERY_STRING=""; if(!empty($QUERY_STRING)) { # # Permorfm SQL search query # if ( isset($_GET['q'])){ //BEGIN GOOGLE SEARCH CODE require_once 'SOAP_Google.php'; // Configure the following to make this work on your site $nr = 10; // Number of results per page $smarty->assign("nr",$nr); $clef = 'insert_your_google_key_here'; // your Google API key provided by Google $site_url = 'www.realestate-school.com'; //replace this with your site's url // End of configuration section if (strlen($q) > 0) { $google = new SOAP_Google($clef); $result = $google->search(array( 'query' => $q.' site:'.$site_url, 'start' => $s, 'maxResults' => $nr, 'lr' => 'lang_en', 'filter' => true )); if (false !== $result) { // echo " Found about {$result['estimatedTotalResultsCount']} documents in {$result['searchTime']} seconds.</p>"; // echo " Displaying results {$result['startIndex']} through {$result['endIndex']}.</p>"; $res = $result['resultElements']; if( count($res) < $nr ){ $is_last_page=true; }else{ $is_last_page=false; } $i=$result['startIndex']; if (is_array($res)){ foreach ($res as $site) { $google_links[]= " {$i}. {$site['title']} {$site['snippet']}</p>"; $i++; } }else{ $suggestion=$google->getSpellingSuggestion($q); if(!empty($suggestion)){ $google_links[]="No results found. <font size='2'>Did you mean: <a href='search.php?q=".$suggestion."'>".$suggestion."?</font>"; $gootle_links[]="Click here to search again."; }else{ $google_links[]="No results found matching your search term. Click here to search again."; } } //echo "<pre>"; //print_r($google_links); //echo "</pre>"; $smarty->assign("google_links",$google_links); $smarty->assign("main","search"); $smarty->assign("google_result",$result); //echo "<div align='center'>"; if ($result['startIndex'] >= $nr) { $t = $result['startIndex'] - ($nr+1); $google_nav="<a href='search.php?q="; $google_nav.=urlencode($q); $google_nav.="&amp;s=0"; $google_nav.="'>first</a> | "; } if ($result['startIndex'] >= $nr) { $t = $result['startIndex'] - ($nr+1); $google_nav.="<a href='search.php?q="; $google_nav.=urlencode($q); $google_nav.="&amp;s="; $google_nav.=$t; $google_nav.="'>previous</a> | "; } //generate page links 1 | 2 | 3 | 4 | etc..... if ($result['estimatedTotalResultsCount'] > $nr ){ $number_pages = $result['estimatedTotalResultsCount'] / $nr; if($result['estimatedTotalResultsCount'] % $nr != 0){ $number_pages++; } for($i=0;$i<$number_pages;$i++){ //$t = $result['endIndex'] ;//+ 1; $t=$i*$nr; $google_nav.="<a href='search.php?q="; $google_nav.=urlencode($q); $google_nav.="&amp;s="; $google_nav.=$t; $google_nav.="'>"; //echo "i is $i and s is $s"; if($i * $nr == $s){ $google_nav.=""; }else{ $google_nav.="<u>"; } $google_nav.=$i+1; if($i * $nr == $s){ $google_nav.=""; }else{ $google_nav.="</u>"; } $google_nav.="</a> | "; } } //if (($result['estimatedTotalResultsCount'] > $result['endIndex']) || !$result['estimateIsExact'] || $result['startIndex'] >= $nr ){ // $google_nav.=" Navigation | "; //} if ((($result['estimatedTotalResultsCount'] > $result['endIndex']) || !$result['estimateIsExact']) && $is_last_page!=true ){ $t = $result['endIndex'] ;//+ 1; $google_nav.="<a href='search.php?q="; $google_nav.=urlencode($q); $google_nav.="&amp;s="; $google_nav.=$t; $google_nav.="'>next</a> | "; } if ((($result['estimatedTotalResultsCount'] > $result['endIndex']) || !$result['estimateIsExact']) && $is_last_page!=true){ $t = $result['endIndex'] ;//+ 1; $google_nav.="<a href='search.php?q="; $google_nav.=urlencode($q); $google_nav.="&amp;s="; $google_nav.=$result['estimatedTotalResultsCount']-1; $google_nav.="'>last</a>"; } } //echo "</div>"; //echo "<hr>"; //echo "<pre>"; //print_r($result); //echo "</pre> "; // print_r($result); } else { echo " There was a problem with the search request.</p>"; } $smarty->assign("google_nav",$google_nav); //END GOOGLE SEARCH CODE }else{ $price_condition = $price_search_1?" AND $sql_tbl[pricing].price>='$price_search_1'":""; $price_condition .= $price_search_2?" AND $sql_tbl[pricing].price<='$price_search_2'":""; $sort_by_price = $price_condition?" ORDER BY price":""; $price_substring = $price_search_1?"&price_search_1=".urlencode($price_search_1):""; $price_substring .= $price_search_2?"&price_search_2=".urlencode($price_search_2):""; $search_category = addslashes(array_pop(func_query_first("select category from $sql_tbl[categories] where categoryid='$in_category'"))); //$search_query = "($sql_tbl[products].product like '%$substring%' or $sql_tbl[products].descr like '%$substring%') and $sql_tbl[categories].category like '$search_category%' and $sql_tbl[products].forsale='Y' $price_condition ".$sort_by_price; $search_query = "($sql_tbl[products].product like '%$substring%' or $sql_tbl[products].descr like '%$substring%') and $sql_tbl[categories].category like '$search_category%' and $sql_tbl[products].forsale='Y' $price_condition "; if ($current_area == "C") { $membership_condition = " AND ($sql_tbl[categories].membership='". $user_account['membership']."' OR $sql_tbl[categories].membership='') "; } else { $membership_condition = ""; } $search_query_count = "select count(*) from $sql_tbl[products], $sql_tbl[pricing], $sql_tbl[categories] where $sql_tbl[pricing].productid=$sql_tbl[products].productid and $sql_tbl[pricing].quantity=1 and $sql_tbl[products].categoryid=$sql_tbl[categories].categoryid $membership_condition and ($sql_tbl[pricing].membership='". $user_account['membership']."' or $sql_tbl[pricing].membership='') and ($sql_tbl[products].product like '%$substring%' or $sql_tbl[products].descr like '%$substring%') and $sql_tbl[categories].category like '$search_category%' and $sql_tbl[products].forsale='Y' $price_condition "; $total_products_in_search = array_pop(func_query_first($search_query_count)); # # Navigation code # $objects_per_page = $config["General"]["products_per_page"]; $total_nav_pages = ceil($total_products_in_search/$config["General"]["products_per_page"])+1; require "../include/navigation.php"; //$smarty->assign("products",func_search_products($search_query, $user_account['membership'],$first_page,$total_products_in_search)); $smarty->assign("products",func_search_products($search_query, $user_account['membership'],$first_page,$total_products_in_search,$sort_by_price)); $smarty->assign("navigation_script","search.php?substring=".urlencode($substring)."&in_category=$in_category".$price_substring); $HTTP_GET_VARS["substring"] = stripslashes($HTTP_GET_VARS["substring"]); $smarty->assign("main","search"); } } else { $smarty->assign("main","advanced_search"); } $smarty->display("customer/home.tpl"); ?>

Then you should replace your /skindirectory/customer/main/search_result.tpl with the following code:
Code:
{* $Id: search_result.tpl,v 1.10 2002/10/11 12:43:43 ambal Exp $ *} { include file="location.tpl" last_location=$lng.lbl_search_results} {include file="customer/main/navigation.tpl"} {capture name=dialog} {include file="customer/main/products.tpl"} {/capture} {if $smarty.capture.dialog ne ""} {include file="dialog.tpl" title=$lng.lbl_search_results content=$smarty.capture.dialog extra="width=100%"} {elseif $google_links ne ""} {capture name=dialog2} {section name=google_link loop=$google_links} {$google_links[google_link]} {/section} {if $google_nav ne ""} {$google_nav} {/if} {/capture} {if $smarty.capture.dialog2 ne ""} {include file="dialog.tpl" title=$lng.lbl_search_results content=$smarty.capture.dialog2 extra="width=100%"} {/if} {else} {include file="dialog.tpl" title=$lng.lbl_search_results content="No matching products found" extra="width=100%"} {/if} {include file="customer/main/navigation.tpl"}

Oh yeah, and replace the skindirectory/customer/main/advanced_search.tpl with the following code:
Code:
{* $Id: advanced_search.tpl,v 1.3 2002/10/08 12:01:17 alfiya Exp $ *} {include file="location.tpl" last_location=$lng.lbl_advanced_search} {capture name=google_search} <form action="search.php" method="get" name="googlesearch" id="googlesearch"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="21">Search this whole site using Google: <input name="hl" type="hidden" value="en" id="hl"> <input name="lr" type="hidden" id="lr"> <input name="ie" type="hidden" value="UTF-8" id="ie"> <input name="oe" type="hidden" value="UTF-8" id="oe"> <input name="as_qdr" type="hidden" value="all" id="as_qdr"></td> </tr> <tr> <td><input name="q" type="text" id="q"> {include file="buttons/search.tpl"} </td> </tr> </table> </form> {/capture} {include file="dialog.tpl" title="Google Search" content=$smarty.capture.google_search extra="width=100%"} {capture name=adv_search} <table border=0> <form action="search.php" name="productsearchbyprice_form"> <tr> <td>{$lng.lbl_product_title}</td> <td> <input type="text" name="substring" size="30" value="{$smarty.get.substring}"> </td> </tr> <tr><td>{$lng.lbl_price}, {$config.General.currency_symbol}</td> <td><input type="text" name="price_search_1" size="6" value="{$smarty.get.price_search_1|escape}"> - <input type="text" name="price_search_2" size="6" value="{$smarty.get.price_search_2|escape}"></td></tr> <tr><td>{$lng.lbl_category}</td> <td> <select name="in_category"> <option value="">All</option> {section name=cat_num loop=$categories} <option value="{ $categories[cat_num].categoryid}" {if $smarty.get.in_category eq $categories[cat_num].categoryid or $cat eq $categories[cat_num].categoryid}selected{/if}>{$categories[cat_num].category|escape}</option> {/section} </select> </td> </tr> <tr><td></td></tr> <tr><th>{include file="buttons/search.tpl"}</th></tr> </form> </table> {/capture} {include file="dialog.tpl" title=$lng.lbl_advanced_search content=$smarty.capture.adv_search extra="width=100%"} </p>

Now look at the customer/search.php page and fill in your key where it says:
Code:
$clef = 'insert_your_google_key_here'; $site_url = 'www.realestate-school.com';

Replace the parameters with your google key and your url, save and upload the file.

You're done. You now have a google search embedded in your site that will search only your site. It will also offer spelling suggestions whenever possible for misspelled queries that don't return any results. If you wish to change the format of the navigation links at the bottom of the results, just modify the search.php page, as I've not bothered to use $smarty logic for the nav link generation. It's just way easier to do it with php and then spit it out into the template once the links are formatted. Feel free to do whatever you want with this code. Someone else did all the hard work here, I just put it together with x-cart.
__________________
www.brettbrewer.com
Getting back into x-cart dev after a long hiatus. Modded lots of x-carts from version 3.1.x to 4.1.x. Developer of ImageScaler mod, Pre-login per user coupon mod, Wordpress feed mod, DigitalSubscriptions mod, Phonetic bulk download keys addon for DownloadExpander mod, Serial Number Generator for ESD products, Custom CMS/LMS integrations, external products mod, and more.
Reply With Quote
  #2  
Old 03-17-2003, 12:05 PM
  shan's Avatar 
shan shan is offline
 

X-Guru
  
Join Date: Sep 2002
Location: Birmingham, UK
Posts: 6,163
 

Default

Do you have a working version of this anywhere to take a look at ?

Sounds good
__________________
Looking for a reliable X-cart host ?
You wont go wrong with either of these.

EWD Hosting
Hands On Hosting
Reply With Quote
  #3  
Old 03-17-2003, 12:17 PM
  minorgod's Avatar 
minorgod minorgod is offline
 

X-Adept
  
Join Date: Sep 2002
Location: Arivaca, AZ
Posts: 402
 

Default Here's a link

Here'a link where you can see it in action, but please don't test our cart as we don't want our stats to be tainted any more than they already are...

http://www.realestate-school.com/online-store/customer/search.php

Also, there appears to be a slight problem with the search results navigation links because google estimates the number of results that will be returned and the links are built using this estimate. If the estimate is wrong, then there will be an incorrect number of page links to navigate the results. I think this can be fixed, but haven't tried to fix it yet because I just noticed it.
__________________
www.brettbrewer.com
Getting back into x-cart dev after a long hiatus. Modded lots of x-carts from version 3.1.x to 4.1.x. Developer of ImageScaler mod, Pre-login per user coupon mod, Wordpress feed mod, DigitalSubscriptions mod, Phonetic bulk download keys addon for DownloadExpander mod, Serial Number Generator for ESD products, Custom CMS/LMS integrations, external products mod, and more.
Reply With Quote
  #4  
Old 03-20-2003, 11:55 AM
 
sabrina sabrina is offline
 

Advanced Member
  
Join Date: Feb 2003
Location: Indianapolis, IN., USA
Posts: 53
 

Default Google search.

Hi, my name is Arthur, i got x-cart 3.3.4, and i did like you explain, but it's not working at all. Please help.
My page is: http://www.indymart.net/buy/customer/home.php
Reply With Quote
  #5  
Old 03-24-2003, 09:21 AM
  minorgod's Avatar 
minorgod minorgod is offline
 

X-Adept
  
Join Date: Sep 2002
Location: Arivaca, AZ
Posts: 402
 

Default

This code was written for x-cart 3.3.1 so I can't really help you until I upgrade which will probably not be any time soon. I'd suggest looking at the differences between the code in the original files for versions 3.3.1 and 3.3.4. If there aren't any major differences in the original files, then it should probably work, but that's all I can say for now.
__________________
www.brettbrewer.com
Getting back into x-cart dev after a long hiatus. Modded lots of x-carts from version 3.1.x to 4.1.x. Developer of ImageScaler mod, Pre-login per user coupon mod, Wordpress feed mod, DigitalSubscriptions mod, Phonetic bulk download keys addon for DownloadExpander mod, Serial Number Generator for ESD products, Custom CMS/LMS integrations, external products mod, and more.
Reply With Quote
  #6  
Old 03-25-2003, 10:51 AM
 
sabrina sabrina is offline
 

Advanced Member
  
Join Date: Feb 2003
Location: Indianapolis, IN., USA
Posts: 53
 

Default Google search.

Hi, when i click on submit button, it's redirect me to Google site, but on you page results shown on your site, please help fix it. Thank you, Arthur
Reply With Quote
  #7  
Old 03-25-2003, 01:00 PM
  minorgod's Avatar 
minorgod minorgod is offline
 

X-Adept
  
Join Date: Sep 2002
Location: Arivaca, AZ
Posts: 402
 

Default Oh yeah...sorry...

Ooops...my bad. I posted the wrong version of the advanced_search.tpl file. I have edited my post above to reflect the proper version. I had modified the script from home and forgot to download it to my work computer when I posted the code here so I ended up posting old code. Sorry for the trouble. Let me know if the new code works better...you should just need to fix your advanced_search.tpl file.

Good luck,
Brett.
__________________
www.brettbrewer.com
Getting back into x-cart dev after a long hiatus. Modded lots of x-carts from version 3.1.x to 4.1.x. Developer of ImageScaler mod, Pre-login per user coupon mod, Wordpress feed mod, DigitalSubscriptions mod, Phonetic bulk download keys addon for DownloadExpander mod, Serial Number Generator for ESD products, Custom CMS/LMS integrations, external products mod, and more.
Reply With Quote
  #8  
Old 03-29-2003, 04:48 AM
 
sabrina sabrina is offline
 

Advanced Member
  
Join Date: Feb 2003
Location: Indianapolis, IN., USA
Posts: 53
 

Default Google search.

Hi, still not work in 3.3.4 version.
Reply With Quote
  #9  
Old 03-31-2003, 07:09 AM
  minorgod's Avatar 
minorgod minorgod is offline
 

X-Adept
  
Join Date: Sep 2002
Location: Arivaca, AZ
Posts: 402
 

Default

bummer.

Uh...could you maybe tell me what you've done so far? Did you follow all the instructions from the beginning of this post? You do have to download some files from hotscripts.com so let me know what you've tried and I'll see if I can help. Like I said above, this modification was done to x-cart version 3.3.1. I've got a heavily modified version of an early release of the 3.3.x codebase, so I've had to fix many bugs that have already been fixed in newer releases. The newer versions may have crucial parts that now work completely differently (though most likely only minor diffs). So anyway, give me more info and I'll try to help you.

Oh yeah...and make sure you have PEAR installed on your server (part of standard PHP install). Though it is unlikely that PEAR would not be installed, it is possible. If it's not installed, then this script will certainly cause errors. Oh wait...that's another project....uhhh....forget about PEAR, it's not used here.
__________________
www.brettbrewer.com
Getting back into x-cart dev after a long hiatus. Modded lots of x-carts from version 3.1.x to 4.1.x. Developer of ImageScaler mod, Pre-login per user coupon mod, Wordpress feed mod, DigitalSubscriptions mod, Phonetic bulk download keys addon for DownloadExpander mod, Serial Number Generator for ESD products, Custom CMS/LMS integrations, external products mod, and more.
Reply With Quote
  #10  
Old 03-31-2003, 09:00 AM
 
jpsowin jpsowin is offline
 

X-Adept
  
Join Date: Sep 2002
Posts: 459
 

Default

I haven't tried it, but that's some good code right there.
Good work!
__________________
Joshua Sowin
...taking things one TPL at a time.
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 03:10 AM.

   

 
X-Cart forums © 2001-2020