Showing Products based on a Keyword Query

This example will show 24 products, with their thumbnails, product name, price and brand (if it exists)

The first block of code, same as always:

<?php
	define('API', 'PS');
	require_once('constants.inc.php');
	require_once('classes/class.ClientFactory.php');
	$oClient = ClientFactory::getClient(API_USERNAME, API_PASSWORD, API_USER_TYPE);
	?>

The business end of the code, using the Category ID, but this time its been set to 0, which is all categories, and a keyword query, in this case ‘firetrap shoe’:

<?php
	$oRefineBy = new stdClass();
	$oRefineBy -> iId = 4;
	$oRefineBy -> sName = 'Category';
	$oRefineByDefinition = new stdClass();
	$oRefineByDefinition -> sId = 0;
	$oRefineByDefinition -> sName = '';
	$oRefineBy -> oRefineByDefinition = $oRefineByDefinition;
 
	$searchquery = 'firetrap shoe';
 
	$listproducts = array( "iLimit"=>24, "oActiveRefineByGroup" => $oRefineBy,  "sQuery" => urlencode($searchquery), "bIncludeTree" => true);
	$oResponse= $oClient->call('getProductList', $listproducts);
	foreach($oResponse->oProduct as $details){
		$image = $details->sAwThumbUrl;
		$name = $details->sName;
		$name = $details->sName;
		$price = $details->fPrice;
		$link = $details->sAwDeepLink;
		$brand = $details->sBrand;
		echo "<div  style=\"width:31%;margin: 5px;padding:2px;display:inline-block;min-height:110px;height:110px;vertical-align:top;\">
<a href=".$link.">
<img src=\"".$image."\" width=\"70\" height=\"70\" alt=\"".$name."\" style=\"float:left; margin:5px;border:0px\" />
</a>".$name."<br/>&pound;".number_format($price,2,'.',',')."<br/>".$brand."</div>";
	}
 
	?>

The Results of this query (new window)
There are one or two differences in this code to the category based query.

Firstly, there is a new parameter in the call “sQuery” => urlencode($searchquery). It should be evident what this parameter does, and shouldn’t need any explanation here, suffice to say that the line above it $searchquery = ‘firetrap shoe’;

Comments

10 Comments on Showing Products based on a Keyword Query

  1. Dean on Tue, 22nd Sep 2009 2:30 pm
  2. I have a question but first can I say thank you sooo much for putting this site up! The wiki and forum are very little help and I’ve been struggling… Now for my question – Is it possible to query a more general term such as ‘high heel shoes’ instead of just shoes? Can you filter out results too, say if you dont want ‘red high heels’ just the rest? Thanks again!!
    Dean

  3. Dean on Tue, 22nd Sep 2009 2:31 pm
  4. sorry I meant query a more ‘specific term’, not general

  5. apimonkey on Tue, 22nd Sep 2009 2:43 pm
  6. Hi Dean, of course, the query is your choice, with V3 you can also use negative keywords (using the “sMode” =>boolean parameter in your API call)

    I’ll knock up a quick page later to show how this is done

  7. hardyk on Tue, 16th Mar 2010 11:41 am
  8. Hi,

    I’m new to AW and wouldn’t have known how to start using the api tool if I hadn’t chanced upon your site – Thanks a lot !

    I’m trying to ‘construct’ a key word query using negative keywords but with no joy.

    Here is my latest effort:

    $searchquery = “+Shoe+Black-Flat” (N.b. also tried using ! to indicate negative)

    $listproducts = array( “iLimit”=>100, “sSort” => “hi”, “oActiveRefineByGroup” => array( $oRefineBy,$oRefineBy1,$oRefineBy2,$oRefineBy3 ), “sQuery” => $searchquery, “sMode” => “boolean”, “bIncludeTree” => true, “bUseGlobalList” => false);

    N.b. RefineBy = CatId, RefineBy1 thru 3 = Merchants

    Trying to retrieve Black Shoes that don’t have Flat heels !

    Regards

  9. apimonkey on Thu, 18th Mar 2010 1:47 am
  10. You need some spacing in there : “+Shoe +Black -Flat” should do the job for the query

  11. Joe on Sat, 7th Aug 2010 5:07 pm
  12. Anyone else notice that if only one item is found in a search the code that is returns freaks out the above code? Seems that the API returns different responds when only one item is found.

  13. apimonkey on Sat, 7th Aug 2010 5:39 pm
  14. Joe, it does because of the way I coded it, I should have put a check in the code to see if there was only one product requested, then, instead of running a foreach loop, just read the details as one block.

  15. apimonkey on Sat, 7th Aug 2010 5:47 pm
  16. So, it should, when there is only one product use this code:

     
    		$image = $oResponse->oProduct->sAwThumbUrl;
    		$name = $oResponse->oProduct->sName;
    		$name = $oResponse->oProduct->sName;
    		$price = $oResponse->oProduct->fPrice;
    		$link = $oResponse->oProduct->sAwDeepLink;
    		$brand = $oResponse->oProduct->sBrand;
    echo "<div  style=\"width:31%;margin: 5px;padding:2px;display:inline-block;min-height:110px;height:110px;vertical-align:top;\">
    <a href=".$link." rel="nofollow">
    <img src=\"".$image."\" width=\"70\" height=\"70\" alt=\"".$name."\" style=\"float:left; margin:5px;border:0px\" />
    </a>".$name."<br/>&pound;".number_format($price,2,'.',',')."<br/>".$brand."</div>";

    and remove the foreach line and the end of the foreach loop:

    remove: foreach($oResponse->oProduct as $details){

    and remove: }

  17. Joe on Sat, 7th Aug 2010 7:38 pm
  18. Cheers apimonkey, perfect. I’ve used

    if ($oResponse->iTotalCount == 1){
    # display one item
    }else{
    # for each loop
    }

    Works a treat now that I’ve used your example code.

  19. apimonkey on Sat, 7th Aug 2010 7:42 pm
  20. Good idea to check against the response total Joe, nice one :)

    Tony

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

You must be logged in to post a comment.