WooCommerce Google Customer Reviews

Feb 14, 2018

WooCommerce Google Customer Reviews


This post was originally created on our sister site WP Cover.
View Original Article
February 14, 2018

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 ' ';

}

The post WooCommerce Google Customer Reviews appeared first on WP Cover.

Additional News

Perfect!

Let's get started. Fill out the form below to email us or request a call back.