Last updated 7 June 2014. Created on 18 January 2011.
Edited by acabouet, queryblitz, LeeHunter, arianek. Log in to edit this page.

This tutorial creates an Amazon search widget in a block that will show results based on a portion of the current node's title. The code in this tutorial was originally developed to show products related to YouTube videos that were being automatically pulled in through a feed. It would have been difficult to go into each video node and add the ASIN for each product. You can see how it's used at

Create a block and set the Block body input format to PHP, then paste the following code into the textfield.

$getthetitle  = drupal_get_title();
$removewords = array(" a ", "A ", " to ", " this ", " all ", " the ", " from ", "&amp;amp;", "&", " amp; ", ":", ";", " - ", "- ", " -", "-", "  ");
$searchquery = str_replace($removewords, " ", $getthetitle);
$finalquery = truncate_utf8($searchquery, 25, TRUE, FALSE); ?>

<script type='text/javascript'>
var amzn_wdgt={widget:'Search'}
amzn_wdgt.defaultSearchTerm='<?php echo $finalquery; ?>';
<script type='text/javascript' src=''>

After "truncate_utf8($searchquery, ", replace the number 25 with the length of characters you'd actually like to search on Amazon. Setting it higher will (obviously) limit the results.

At "amzn_wdgt.tag='lickivideorev-20';" replace lickivideorev-20 with your own Associate ID, leaving the single quotes.

Set the block to show only on full node pages, save it, and go check out one of your nodes.

The PHP above does a few things. First, it finds the node title. Then, it takes that title and subtracts any of those things between the quotes in the removewords array (you can add more or take out any of those depending on what you want to get passed into the Amazon search). Lastly, we shorten the search query further by cutting off any words that would make the query exceed 25 characters. Where it says "TRUE, FALSE", the TRUE makes sure no words get cut in half, and the FALSE makes it so no ellipsis gets added. You could change them if you want.

For reference, in the Amazon Associates Menu, you can go to Widgets, then click on Widget Source. This will get you to the page where most this code is found. There are a few parameters you can change, although setting the border to false, does not appear to change anything.

Looking for support? Visit the forums, or join #drupal-support in IRC.


seward’s picture

I tried according to the tips. No Related Products appear. IE says: "amzn_wdgt not defined". I hope someone can help me out!

queryblitz’s picture

I'm not sure why you're getting that, but I'm seeing a lot of similar things around I think it has something to do with the script trying to run before javascript has loaded. Go ahead and look around Drupal for "variable is not defined." I think it can be fixed by placing the code inside an "if" statement, but I don't know enough PHP or Javascript to do it correctly. If someone can help him better than that please feel free.

BeaPower’s picture

any update for drupal 7?

queryblitz’s picture

Should work through Drupal 8 as is.