My amazing wordpress widget with google search shopping API
Hello guys,
This time I got my hands wet with Google shopping Search API.
I read about this api yesterday and thought that I should try to implement this API functionality in my wordpress widget.
The Search API for Shopping is intended for developers who want to programmatically query data that has been uploaded to Google via the Google Merchant Center.
The Search API for Shopping targets the following use cases:
1) Developers can build applications that query product offers across merchants.
2) Merchants can use the API to query their own data by becoming a Commerce Search user.
3) Publishers in the Google Affiliate Network can use the API to access product offers from their advertisers of choice.
In fact this API is too much extensive to it’s scope and can be very useful to the sites which are mainly intended towards the product search for consumers, also it can be very useful to more revenue generation because with the help of this API you can programatically display any offers and products from a perticular publisher.
All the above mentioned stuff was intro (or you can also call it a speech if you wish so..
) about the Search API for shopping.
now it’s time to get practical:->
You can copy and paste the following code to create custom widget implementing google shopping api.
/******************** Code Starts Here *************************************/
function widget($args, $instance){
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance['title']) ? ‘ ‘ : apply_filters(‘widget_title’, $instance['title']);
$api_key = $instance['api_key'];
$search_q = $instance['search_q'];
$by_brand = $instance['by_brand'];
$results_to_show = $instance['results_to_show'];
if(empty($results_to_show)){
$results_to_show = 6; /* Default results to show is 6 */
}
$get_search_q = explode(‘ ‘, $search_q);
if(count($get_search_q)>1){
$more_term = 1;
}
//$product_url = ‘https://www.googleapis.com/shopping/search/v1/public/products?key=’.trim($api_key).’&country=US&q=netbook’;
$product_url = ‘https://www.googleapis.com/shopping/search/v1/public/products?key=’;
if($api_key && $search_q){
$product_url .= trim($api_key).’&country=US&q=’;
if(isset($more_term)){
for($p=0;$p1){
$more_brands = 1;
}
}
/* If brands are present and more than one */
if(isset($more_brands)){
for($p=0;$pitems as $key=>$val){
array_push($img_arr,$val->product->images[0]);
array_push($title_arr,$val->product->title);
array_push($inventory_arr,$val->product->inventories[0]);
}
//print_r($img_arr[0]->link);
//exit;
if (!empty($title))
echo $before_title . $title . $after_title;
if(count($title_arr)){
if(count($title_arr)>$results_to_show){
$result_to_display = $results_to_show;
}else
$result_to_display = count($title_arr);
?>
<?php }
}
echo $after_widget;
}
}
function rtp_child_register_widgets() {
register_widget( 'google_shop_widget' );
}
add_action( 'widgets_init', 'rtp_child_register_widgets', 11 );
/******************** Code Ends Here *************************************/
you can make your widget as much extensible as you wish it to be.
Currently it will ask you title for widget, what products (one or more) to search, which brands to search (optional) and number of results to display on widget.
there is one more option for API key it's because u hav to register urself for using google API's for getting access to API through API key.
You can also watch out my working example at http://ankit.rtcamp.info
Hope this will be helpful to you all any point of time.
( Do not forget to vote if you like it
)
Thanks
In case you want to read entire article from a single page, check
http://www.chipbennett.net/2011/02/17/incorporating-the-settings-api-in-wordpress-themes/?all=1&doing_wp_cron