Friday, July 02, 2010

Amazon "Buy Now" Kludge For Drupal

I want to get people to go straight to the product. But, the inputter can be unwilling to search out the links neccessary to hook products on the website to products on Amazon.
Using Drupal 6 and the Amazon API module along with the kludge below, you can search out products, limit them by the product type and then provide that link to your readers to generate affiliate revenue.
  • Download and activate the module, the API and the Amazon Search part of this module
  • Get an affiliate id, if you don't yet have one.
  • Go to Amazon and get an API key and API secret key.
  • Plug those into the Amazon API

In your themeing or a block, use the following PHP code. In this example, I've been filtering the results for only video games. Likely, you want to filter for other types of results (DVDs, books, CDs, etc.).


$keys = $node->title." ".$node->keywordsearch;
if ($amazon_cache = cache_get("amazon_search_".$keys)) {
$products[0]['link'] = $amazon_cache->data;
}
else {
$products = array();
$items = amazon_search_simple_search($keys);
foreach ($items as $item) {
if (check_plain($item['productgroup']) == 'Video Games') {
$products[] = array(
'title' => check_plain($item['title']),
'link' => check_url($item['detailpageurl']),
'type' => check_plain($item['productgroup']),
'user' => isset($item['participants']) ? implode(', ', $item['participants']) : '',
'snippet' => isset($item['editorialreviews']) ? check_markup($item['editorialreviews'][0]['content']) : '',
);
}
}
cache_set("amazon_search_".$keys, $products[0]['link']);
}

if ($products[0]['link']) {
print "<br/><b>".l('Buy Now!', $products[0]['link'], array('absolute' => TRUE))."</b>";
}

No comments: