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>";
}

An AddToAny Kludge For Drupal

AddtoAny is a cool widget. For Drupal 6, there is the AddToAny project (http://drupal.org/project/addtoany). It's good. It's really granular in its ability to control access and features. For me, though, the mambo of the Page Title, CCK and AddToAny gave my install indigestion. I knew that I wanted all nodes to show off the AddToAny widget, permissions didn't enter into, and I knew exactly where in the theming I wanted it to appear. So, rather than add one more module to the mix, instead I put the code directly into the theming for content-field. Bad form? Maybe, but that's what makes me a Drupal heretic.

$link_name = $node->title;
$link_url = url('node/' . $node->nid, array('absolute' => 1));

$addany = '<!-- AddToAny BEGIN --><a class="a2a_dd" href="http://www.addtoany.com/share_save?';
$addany .= "linkurl=".urlencode($link_url);
$addany .= "&linkname=".urlencode($link_name);
$addany .= '"><img src="http://static.addtoany.com/buttons/share_save_171_16.png" width="171" height="16" border="0" alt="Share/Bookmark" /></a>'."\n";
$addany .= '<script type="text/javascript">'."\n";
$addany .= 'var a2a_config = a2a_config || {};'."\n";
$addany .= 'a2a_config.linkname = "'.$link_name.'";'."\n";
$addany .= 'a2a_config.linkurl = "'.$link_url.'";'."\n";
$addany .= 'a2a_config.onclick = 1;'."\n";
$addany .= 'a2a_config.show_title = 1;'."\n";
$addany .= 'a2a_config.num_services = 20;'."\n";
$addany .= '</script>'."\n";
$addany .= '<script type="text/javascript" src="http://static.addtoany.com/menu/page.js"></script>'."\n";
$addany .= '<!-- AddToAny END -->';

print $addany;