WooCommerce Google Customer Reviews


About The Author
Cabe Nolan is the founder of WP Cover where he shares his insight into WordPress, development, & entrepreneurship. Outside of WPCover, Cabe continues to run a successful WordPress development firm, Bold City Design as well as a few high profile websites, Arrivala, Two Way Resume, Dock Skipper, and a successful outdoors brand, DolfinPack.







WooCommerce Google Customer Reviews

A while back Google switched its Google Trusted Stores platform over to Google Customer Reviews.  The change had a lot of quirks and issues.  I’ve had talks with numerous large e-commerce stores who had to fight and claw to get their reputations and reviews transferred over to the new platform (even though it was supposed to happen automatically).  In conjunction with the move, the checkout receipt page also needed to be modified and a custom function written to communicate the customer information and order information to the new platform.

I’m not going to go into tons of explanation on this as each line should be fairly self explanatory.  And if you don’t understand it, it’s not a big deal because you can basically just copy and paste.  The only thing you’ll need to modify is within the javascript tag at the bottom, replacing merchant_id with your specific Google Merchant ID.

Here’s your function that will go in your active themes function.php file:

add_action( 'woocommerce_thankyou', 'google_customer_reviews_custom_tracking' );

function google_customer_reviews_custom_tracking( $order_id ) {
	$order = wc_get_order( $order_id );
	$order_meta = get_post_meta($order_id);
	$ordersubtotal = $order->get_subtotal();
	$ordertotal = $order->get_total();
	$taxtotal = $order->get_total_tax();
	$shippingtotal = $order->get_total_shipping();
	$shippingpostcode = $order_meta['_shipping_postcode'][0];
	$billingemail = $order_meta['_billing_email'][0];
	$shippingcountry = $order_meta['_shipping_country'][0];
	if($shippingcountry == 'US') {
		$recdate = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + 7, date('Y')));
	} else {
		$recdate = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + 14, date('Y')));
	}
	$line_items = $order->get_items();
	
	$post_data = '';
	// This loops over line items
	$itemcount = count($line_items);
	$i=0;
	foreach ( $line_items as $item ) { $i++;
  		$product = $order->get_product_from_item( $item );
		$sku = $product->get_sku();
		$product_name = $item['name'];
		$product_id = $item['product_id'];
		$qty = $item['qty'];
		$subtotal =  strval($order->get_line_subtotal( $item, false, true ));
		$sep = ',';
		if($i == $itemcount) {
			$sep = '';
		}
		$post_data = $post_data . '{product_id: \'' . $product_id . '\', unit_price: \'' . $subtotal . '\', quantity: \'' . $qty . '\'}' . $sep;
	}

	echo '
 <script>
 window.renderOptIn = function() {
 window.gapi.load("surveyoptin", function() {
 window.gapi.surveyoptin.render(
 {
 "merchant_id": 11111111,
 "order_id": "' . $order_id . '",
 "email": "' . $billingemail . '",
 "delivery_country": "' . $shippingcountry . '",
 "estimated_delivery_date": "' . $recdate . '"
 });
 });
 }
 </script>';

}

Share Your Thoughts

Leave a Reply

Your email address will not be published.


Related Stuff You Might Like







WordPress News, Tips, & Code Snippets

Join the WP Cover mailing list and get wordpress news, tips, code snippets, security warnings, and more delivered right to your inbox.  We won't flood your inbox, newsletters typically go out every 1-2 weeks unless it involves an important security release.

You have Successfully Subscribed!