Drupal random Amazon wishlist item

I got this wild hair up my nose about "wouldn't it be cool to show a random item from my Amazon wishlist on my site?" the other day. I looked around, found plenty of wishlist scripts, but none of them did what I wanted. They all showed the first X items on your list sorted by Y, for various Xs and Ys. Nothing random out there.

So I dug around and learned and read and dug some more. I found the Drupal Amazon module, which seemed promising. It had item lookup and wishlist support. Seems great! I downloaded and installed it and found it didn't do wishlists. Bummer. But it did include a way to make raw AWS calls, so I should be set.

I was up all night, but I got it to work. You can see it in the bottom-most block on the right -- a random item from my wishlist! I'm here to show you how I did it..but first a disclaimer. I'm not a PHP programmer, I've never worked with the AWS before, and it was late at night. Forgive my code.

Once the Amazon module is installed, I plugged this code into a block:

<?php
$list = '2ZR320UHXZ47K';

$params = array(
  'ListType' => 'WishList',
  'ListId' => $list,
  'ProductGroup' => 'Book',
  'ResponseGroup' => 'ListFull',
);
$results = amazon_http_request('ListLookup', $params);
if (!empty($results->Error)) {
  return;
}

$total_items = (int)$results->Lists->List->TotalItems;
$random_item = rand(1, $total_items) - 1;
$page = (int)($random_item / 10) + 1;
$item_on_page = $random_item % 10;

$params = array(
  'ListType' => 'WishList',
  'ListId' => $list,
  'ResponseGroup' => 'ListItems',
  'ProductGroup' => 'Book',
  'ProductPage' => $page
);
$results = amazon_http_request('ListLookup', $params);
if (empty($results->Error)) {
  $asin = $results->Lists->List->ListItem[$item_on_page]->Item->ASIN;

  $items = amazon_item_lookup($asin);
  if (is_array($items)) {
    $item = array_pop($items);
    $item[detailpageurl] .= '%26colid='.$list;
    amazon_item_save($item);
    $markup = theme('amazon_item', $item, 'details');
    print $markup;
  }
}

?>

The code basically asks AWS to pass us back some information about the wishlist -- in particular, we want the total number of items in the list. From that, we pick a random item and calculate which page it's on. Each page has 10 items on it when you get the data this way. Handy. Then we just slurp down that page of the wishlist and extract the item we're interested in.

The $list variable at the top is the unique ID of your wishlist. Go view your wishlist and look at the URL...you can find it there. I also made some changes to the tpl files the module uses to display the information, but that's just seasoning to taste.

So there you have it. A slightly sloppy way to retrieve a random item from an Amazon wishlist!

Published in

Now that is something. I also agree it's kinda cool to add something from Amazon. Like when I did a homework help of a research paper, we add a little addition from Amazon which really helps solve the problem.

Now i actually want to display 4 items rather than 1, how do i go about that? Regards Platform 8

Is it possible to use this with items other than Amazon? i.e. Clickbank and/or Commission Junction products?

If you have an associate ID with Amazon, the Drupal Amazon module will take care of adding that to the URL for you.

In sites/all/modules/amazon/amazon.module, find the function amazon_get_associate_id and change it to return your associate ID. For example, mine looks like:

function amazon_get_associate_id() {
  return 'gararn-20';
}

Over and out.

-g.

you should add the IsOmitPurchasedItems param to not show items already bought from the wishlist.

$params = array( 'ListType' => 'WishList', 'ListId' => $list, 'ResponseGroup' => 'ListFull', 'IsOmitPurchasedItems' => 1, );

Thanks for the code :)

Seems like that change works just fine. Thanks for the suggestion and you're welcome!

-g.

Sorry for the double post...I figured it out...I don't have any books on the list, but once I added one (since you specified ProductGroup to be 'Book') it popped up on the page. Do I just remove that info to make anything and everything pop up?

I haven't tried it myself, but it looks like if you changed the ProductsGroup from 'Book' to 'All', then everything would show up. I believe this page tells you what codes are available.

Let me know if that works for ya!

you remove the two lines in the code completely: 'ProductGroup' => 'Book',

I tried that because the 'All' code for the ProductGroup is not available for the german locale and it displays all entries from my wishlist.

regards, Oliver

Hi all, Came across this after installing the amazon module and it worked first time! displaying your books randomly. (will add my own wishlist id later) My problem is when I change the locale to the uk in the configure amazon api on my drupal site it stops working and I get the text:

Manufacturer: Part Number: Price

appearing in the block with no products displayed. Same deal for Germany when I tested. Also used my UK wishlistID in all configs to no avail. So it only seems to work for me only if the locale is the US and the amazon wishlist ID is from the US. So I'm wondering if there was something else anyones done to get it working for your different locales? Help appreciated Regards Platform8

Thanks for clearing that up.

-g.

Not sure what I'm doing wrong, but I have an idea that it's something I didn't check off on amazon's side. When I have your list id number in it loads fine, but when I put mine in it doesn't load. Anything special you have checked off on your list?

Not sure what I'm doing wrong, but I have an idea that it's something I didn't check off on amazon's side. When I have your list id number in it loads fine, but when I put mine in it doesn't load. Anything special you have checked off on your list?

Hey, thanks for the awesome hack. I was looking for something similar for wordpress. Do you have any writeup for that?

Cheers, Ericia

I've never used Wordpress, so I wouldn't have any idea how to go about getting something similar working there. All the code is PHP, so it shouldn't be too difficult putting something generic together.

Hi!

I found your Drupal-Amazon wish list solution while searching for a way to show my wish list on my web page. Thanks for sharing your code! Now I copied your code and everything is fine except that I get an annoying PHP-error. I use Drupal 6.5 and amazon-module 6.x-1.0-beta3. The log says "Illegal offset type in isset or empty in ... amazon.module on line 235". Do you know what that means? I should say that I know a bit about HTML and CSS but not PHP.

Anyway, thanks for your code!

I ran into that, too, and forgot to mention it in my blog post! I don't recall what the problem was, exactly...something along the lines of trying to index an array with something that wasn't an integer, or maybe trying to index something that wasn't an array.

In any case, to fix it, change line 235 of amazon.module. It should currently read:

if (!isset($items[$item_id])) {

Replace that line with these two:

$asin = (string)$item_id[0];
if (!isset($items[$asin])) {

That should fix the problem!

Thanks a lot! Everything fine now.


Theme port sponsored by Duplika Web Hosting.
Home Back To Top