Tuesday, March 06, 2007

Shopping Tie-Ins In Drupal

I feel that the killer app in Drupal is the "Panels" module. I find it's a capstone to so many of the other features in Drupal. When tasked to build a Shopping page for Game-Boyz.com, I turned to my ideal combo-move: panels + blocks.
I installed the panels module. I built a panel layout and gave it an alias path of "shopping"-- all "shopping" paths will be directed to this new panel. What's great? Arguments to the right of the shopping path can be ignored by the panels. So, inside of a set of panels, you can add one or more blocks. These blocks can read the arg(#) passed in. arg(0) is shopping; arg(1) can be a reference read by a block. In this example, my nodes have a link to the shopping page. It passes out the $nid or arg(1) appended to "shopping". The block I built reads this, loads the node, reads the node for keywords, then loads up the data from an API call and displays it to the users.




if (is_numeric(arg(1))) {

$shopping_node = node_load(arg(1));

// $shopping_node->title; - title
// $shopping_node->field_description[0]['value']; - review text

print "

Great deals for ".$shopping_node->title."

";


if ($shopping_node->field_image) {
print "
";
print $shopping_node->field_image[0]["view"];
}
print "
";

print '
';


$shopping_url = "http://api.shopping.yahoo.com/ShoppingService/v1/productSearch?appid=YahooDemo&query=".urlencode($shopping_node->title);
$response = file_get_contents($shopping_url);

if ($response === false) {
die('Request failed');
}

$phpobject = simplexml_load_string($response);

if ($phpobject === false) {
die('Parsing failed');
}

foreach ($phpobject->Result as $result) {
if (strlen($result->Offer->Price)) {
?>

Offer->Url.'">'.$result->Offer->ProductName.""; ?> - Offer->Price; ?>


Offer->Summary; ?>

}
}
}
?>


How can this improve?
- Caching the XML source for a time, so that you need not wait for/punish Yahoo.
- Using another API than the Yahoo example (I am getting a Shopping.com API soon).