Close

June 27, 2012

Magento Google Trusted Store Feeds

UPDATE 12/20/2013: We’ve moved a few clients away from the Google Plugin, in favor of the PHP file feeds. The Google Plugin, in our experience has caused a lot of problem and bugs with other plugins and was not consistent (for whatever reason) in reporting the data to Google.
Also, we have added an important note below regarding the “Success” page stance and guest users.

If you run a Magento shop and you depend on Google as part of your sales strategy, the Google Trusted Merchant badge is almost a requirement. From the MediaPost (Source), merchants are seeing upwards of 4% increase in sales just by having the badge on their store.

Do not wait! Contact Us today to have us setup your Google Trusted Store account ASAP.

UPDATE 5/21/2013: There is a plugin available for Magento now (Here). We are not a fan of it, since it forces you to create an FTP account to send your feed. To us that isn’t really a “Feed” then. People have posted mixed results installing it. As for anything, if you need any expert Magento advice do not hesitate to Contact Us


As a part of Googles new trusted store feeds, merchants are required to implement several real-time tracking devices as well as a daily feed for shipments/cancellations.

https://support.google.com/trustedstoresmerchant/bin/answer.py?hl=en&answer=2609890&ctx=cb&src=cb&cbid=-13rfhp9gxwqd5&cbrank=0

We found a great post on how to do some of the Magento real time implementations (see: here), but we had to build our shipment and cancellations feeds from scratch.  We’ll leave out the gory details of how to test/upload your feeds to Google Merchant, but listed below are code snippets for both of our feeds.

7/10/12: Thanks to David Baker at (Acorn Computer Solutions) for the updates to change the ID to use the Magento Incremental ID based on the GA Magento plugin! Also help with fine-tuning the shipment feed queries.

googletrusted_shipmentfeed.php

<!--?php /* Author: JTS 	Description: Magento Google "Shipment Feed"  for Trusted Stores 	Notes:  If you have more shippers you'll need to translate them in the "shiptrans" array. 			Right now it pulls the past day's worth of date.  You can adjust the range via the "from_date" and "to_date". Google Feed Instructions: 	https://support.google.com/trustedstoresmerchant/bin/answer.py?hl=en&answer=2609890&ctx=go */ require_once 'app/Mage.php'; Mage::app(); //setup translations 	$shiptrans['United Parcel Service']='UPS'; 	$shiptrans['United States Postal Service']='USPS'; //setup variables $SEP="\t"; $from_date_time = strtotime("-1 day"); // begin date //$period_date_to = $this--->getRequest()-&gt;getParam('report_to');
$to_date_time = strtotime("+1 day");
 
$orders=Mage::getModel('sales/order');
$to_date = date('Y-m-d' . ' 00:00:00', $to_date_time);
$from_date = date('Y-m-d' . ' 00:00:00', $from_date_time);
$orders = Mage::getModel('sales/order')-&gt;getCollection()
    -&gt;addFieldToFilter('updated_at', array('from'=&gt;$from_date, 'to'=&gt;$to_date))
	-&gt;addAttributeToSort('updated_at', 'DSC')
    ;
 
$string='';
print"merchant order id\ttracking number\tcarrier code\tother carrier name\tship date\n";
foreach ($orders as $order) {
	$id=$order-&gt;getId() ;
	$objOrder	= Mage::getModel('sales/order')-&gt;load($id);
 
	$inc_id=$order-&gt;getIncrementId(); //7/10/12: Change to increment id
		//we need: order_id/tracking number/carrier code /ship date
		$shipments = $objOrder-&gt;getShipmentsCollection();
		if(count($shipments)):
			foreach($shipments as $shipment):
				$tracking = $shipment-&gt;getAllTracks();
				#var_dump($tracking);
				#return;
				$shipdate=cleanString($shipment-&gt;getCreatedAt());
				if($shipdate!=''){
					$shipdate_time=strtotime($shipdate);
					$shipdate=date('Y-m-d', $shipdate_time);
				}else{
					continue;
				}
				//7/10/12 make sure the shipment is greater than our from date
				if($shipdate_time&lt;$from_date_time){ 					continue; 				} 				if(count($tracking)): 					foreach($tracking as $track): 						$track_no=cleanString($track-&gt;getNumber());
						$other='';
						$carrier=cleanString($track-&gt;getTitle());
						$cartrans=$shiptrans[$carrier];
						//if we've selected other..make it into Google's feed
						if($cartrans==''){ $cartrans="Other"; $other='OTHER';}
						#print "
 HERE ID:$id Carrier".$cartrans." DATE:".$date. " OTHER:".$other." TRACKING:".$track_no;
 
						//google's feed tab formated
						//order_id/tracking number/carrier code/other carrier/shipdate;
						print $inc_id.$SEP;
						print $track_no.$SEP;
						print $cartrans.$SEP;
						print $other.$SEP;
						print $shipdate;
						print "\n";
 
						#$sTrackingBlock .= '
';
						#$sTrackingBlock .= $track-&gt;getTitle() . ' - ';
						#$sTrackingBlock .= $track-&gt;getNumber();
						#$sTrackingBlock .= '

‘; endforeach; endif; endforeach; endif; } function cleanString($string){ $string = str_replace(“\\t”,”,$string); $string = str_replace(“\\n”,”,$string); $string = str_replace(“\\r”,”,$string); return trim($string); } ?>

googletrusted_cancellationfeed.php

<!--?php /* Author: JTS 	Description: Magento Google "Cancelled Feed"  for Trusted Stores 	Notes:  Just sends the transaction as "MerchantCancelled" as there is no real way to tell in Magento 			Right now it pulls the past day's worth of date.  You can adjust the range via the "from_date" and "to_date". Google Feed Instructions: 	https://support.google.com/trustedstoresmerchant/bin/answer.py?hl=en&answer=2609890&ctx=go */ require_once 'app/Mage.php'; Mage::app(); //setup variables $SEP="\t"; $from_date = strtotime("-1 day"); // begin date $to_date = strtotime("+1 day"); //end date (really in the future and note necessary..just in case something shipped while we just executed $orders=Mage::getModel('sales/order'); $to_date = date('Y-m-d' . ' 00:00:00', $to_date); $from_date = date('Y-m-d' . ' 00:00:00', $from_date); $orders = Mage::getModel('sales/order')--->getCollection()
    -&gt;addFieldToFilter('created_at', array('from'=&gt;$from_date, 'to'=&gt;$to_date))
	-&gt;addAttributeToFilter('status', 'canceled')
	-&gt;addAttributeToSort('created_at', 'DSC')
    ;
$string='';
print"merchant order id\treason\n";
foreach ($orders as $order) {
 
        $id=$order-&gt;getIncrementId() ; //7/10/12: Change to increment id
	#$objOrder	= Mage::getModel('sales/order')-&gt;load($id);
	#$iOrderNum	= $objOrder-&gt;getIncrementId();
		print $id.$SEP;
		print "MerchantCanceled";
		print "\n";
 
 
}
function cleanString($string){
	$string = str_replace("\\t",'',$string);
	$string = str_replace("\\n",'',$string);
	$string = str_replace("\\r",'',$string);
	return trim($string);
}
 
?&gt;

Also a very important note for your “success.phtml” page. Make sure you factor into consideration, the email addresses of your “Guest” checkouts as well.

$orderId = $this->getOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$customer = Mage::getModel('customer/customer')->load($order->getCustomerId());
$address = $order->getBillingAddress();
 
////12/18/2013 get billing email for guests
$email_address2=$address->getEmail(); //check guest email
$email=$customer->getEmail(); //get the customer email, if it exists
if($email=="") $email=$email_address2;