Google Shopping是由之前免费的Google Base所衍生出来的产品,现在Google Shopping的广告PLA(Product Listing Ads)成为Google产品线中的一个重要产品,尤其是现在移动端流量越来越大,其重要性也在逐步增强。Alan个人认为Google Shopping从转化率,投入成本方面而言要优于传统的文本的广告。在网上搜索生成Google Shopping的feed代码时,都是收费的插件,对于很多用惯了免费软件的朋友自然是不愿意去购买的,Alan也是由于好奇,稍微研究了一下Magento的api,使用水平有限的php写了一个feed生成文件,存在本地,然后手动上传的Merchant Center后台。这个代码并不太成熟,没有加入ftp上传功能,但是可以使用,欢迎大神协助优化。
使用下面代码时需要在Magento后台创建一个针对Soap的api用户名密码,并进行适当修改,如保存路径,产品类别等。
由于公司有技术团队负责这块任务,以下产品只进行过少量次数的使用,所以在您的使用过程中会出现bug,欢迎反映出来,一起探讨。
2014年6月23日:2014年8月底Google将会使用Shopping Campaigns作为PLA的默认选项,在新的版本中无法通过adwords_labels进行筛选,因此将adwords_labels更新为custom_label_0
注:2015年4月Google增加了增量feed上传(Online Product Inventory Update),只需在feed中更新price, availability(促销信息)即’price’,’availability’,’sale price’ 和 ‘sale price effective date’字段
<?php
set_time_limit(0); //避免文件执行时超时
date_default_timezone_set("America/Los_Angeles"); //设置时区,这里使用太平洋时间
define('DOMAIN',''); //替换成自己网站的域名
define('API_USER',''); //填写在后台创建的API用户名
define('API_PASS',''); //填写在后台创建的API密码
define('FILE_PATH','d://data_feed.txt'); //写入feed的路径及文件名,如d://data_feed.txt
define('BRAND',''); //这里假设您使用自己的商标,如果对应字段则需对代码进行修改
//ini_set('max_execution_time', 100000); // just put a lot of time
//ini_set('default_socket_timeout', 100000); // same
$client = new SoapClient(DOMAIN.'api/soap/?wsdl',array("connection_timeout"=>10000));
$session=$client->login(API_USER,API_PASS);
//文件头
$init="id\ttitle\tdescription\tgoogle_product_category\tproduct_type\tlink\tmobile_link\timage_link\tadditional_image_link\tcondition\tavailability\tprice\tsale_price\tsale_price_effective_date\tbrand\tgtin\tmpn\tcustom_label_0\tshipping\ttax\tgender\tcolor\tage_group\n";
$handle=fopen(FILE_PATH,'w') or die("can't open file");
if(fwrite($handle,$init)===FALSE){
echo"Can't write to ".FILE_PATH;
}else{
echo "generating feed";
}
fclose($handle);
$category_list = $client->call($session, 'catalog_category.tree'); //获取category列表并存入数组中
for($n=0; $n<count($category_list['children'][0]['children']); $n++){
$cat_list[$category_list['children'][0]['children'][$n]['category_id']]= $category_list['children'][0]['children'][$n]['name'];
}
$handle=fopen(FILE_PATH,'a') or die("can't open file");
if($plist=$client->call($session,'catalog_product.list'))
//遍历所有产品
for($i=0;$i<count($plist);$i++){
$pid=$plist[$i]["product_id"]; //产品id
$result=$client->call($session,'catalog_product.info',$pid);
$sku=$plist[$i]['sku']; //产品sku
$inventory=$client->call($session,'product_stock.list',$sku);
$qty=$inventory[0]['qty']; //库存数量
$title=$plist[$i]['name']; //产品名(标题)
$product_desc=$result['description'];
$url=DOMAIN.$result['url_key'].'.html?cid=GOOGS'; //拼接出产品的url,这里假设您启用了rewrite,后面添加cid以方便追踪
$cat_id="";
for($j=0;$j<count($result['category_ids']);$j++){
$cat_id.=$result['category_ids'][$j].",";
}
$cat_id=explode(",",rtrim($cat_id,","));
$price=round($result['price'],2)." USD"; //产品价格
if(!empty($result['special_price']))
$sp_price=round($result['special_price'],2)." USD"; //打折价格
else
$sp_price='';
//打折有效期,默认当天到第二天
if(!empty($sp_price)){
$eff_date= date("Y-m-d\TH:i-0800/");
$eff_date.=date("Y-m-");
$temp=date("d")+1;
$eff_date.=$temp.date("\TH:i-0800");
}else{
$eff_date="";
}
$gcategory="Apparel & Accessories > Clothing"; //Google产品分类,可遵照http://www.google.com/basepages/producttype/taxonomy.en-US.txt
$ptype="Apparel & Accessories > Clothing > ". $cat_list[$cat_id[0]]; //此处可自定义,这里取产品所属的一个目录
$image=$client->call($session,'catalog_product_attribute_media.list',$pid);
$image_url=$image[0]['url']; //image地址
$add_img="";
if(count($image)>1){
for($k=1; $k<count($image);$k++){
$add_img .=$image[$k]['url'].",";
}
}
$add_img = rtrim($add_img,',');
$condition="new"; //可根据具体要求修改,这里假设没有二手产品
//是否oos
if($qty>0){
$availability='in stock';
}else{
$availability='out of stock';
}
$shipping="US:::5 USD"; //这里根据具体运费标准来设置,此处假设美国国内运费为$5
$tax="US:CA:8.25:y"; //这里根据具体收税标准来设置,这里取默认值加州收8.25点的税
$gender="Unisex"; //根据具体情况进行修改
$color="multi-color"; //由于Alan的网站上没有填写颜色,这里没有通过api调取
$age_group="adult"; //根据实际情况而定
$gtin="";
$custom_label_0="";
//判断是否有库存
if($availability='in stock'){
$add=$sku."\t".$title."\t".$product_desc."\t".$gcategory."\t".$ptype."\t".$url."\t".$url."\t".$image_url."\t".$add_img."\t".$condition."\t".$availability."\t".$price."\t".$sp_price."\t".$eff_date."\t".BRAND."\t".$gtin."\t".$sku."\t".$custom_label_0."\t".$shipping."\t".$tax."\t".$gender."\t".$color."\t".$age_group."\n";
if(fwrite($handle,$add)===FALSE){
echo"Can't write to ".FILE_PATH;
}else{
echo "-";
}
}
}
fclose($handle);
$client->endSession($session); //关闭session
?>

