Skip to content

Instantly share code, notes, and snippets.

@SmashBrando
Last active February 5, 2020 16:27
Show Gist options
  • Save SmashBrando/708982826af0767c2ef4d128bab2ecb7 to your computer and use it in GitHub Desktop.
Save SmashBrando/708982826af0767c2ef4d128bab2ecb7 to your computer and use it in GitHub Desktop.
Google Trusted Stores - Woocommerce Thank You Page Output
<?php
add_action( 'woocommerce_thankyou', 'google_trusted_stores_confirmation' );
function google_trusted_stores_confirmation( $order_id ) {
// Lets grab the order
$order = wc_get_order( $order_id );
$customer = get_current_user_id();
echo '<!-- START Google Trusted Stores Order -->';
echo '<div id="gts-order" style="display: none;" translate="no">';
echo '<!-- start order and merchant information -->';
/**
* Put your tracking code here
* You can get the order total etc e.g. $order->get_total();
*/
// Unique, alphanumeric order identifier created by you, the merchant. Must match the “order number” that you give to your customers.
echo '<span id="gts-o-id">';
echo $order_id;
echo '</span>';
// Valid email address for the customer.
echo '<span id="gts-o-email">';
echo get_user_meta( $customer, 'billing_email', true );
echo '</span>';
// The value of the country parameter should be a two-letter ISO 3166 country code. For example: US, GB, AU, FR, DE, or JP.
echo '<span id="gts-o-country">';
echo get_user_meta( $customer, 'billing_country', true );
echo '</span>';
// Unit of currency for the price, shipping cost, and any other monetary value associated with the order.
// The value of the currency parameter must be a three-letter ISO 4217 currency code.
// All prices for an order must be in the same currency, and must match the currency of the delivery country.
// Google Trusted Stores is currently available for the following currencies: USD, GBP, AUD, EUR, and JPY.
echo '<span id="gts-o-currency">';
echo $order->get_order_currency();
//echo get_user_meta( $customer, 'order_currency', true );
echo '</span>';
// Total price of the entire order, inclusive of tax, shipping, discounts, etc.
echo '<span id="gts-o-total">';
echo $order->get_total();
echo '</span>';
// Total discounts, usually negative.
echo '<span id="gts-o-discounts">';
echo $order->get_total_discount();
echo '</span>';
// Total shipping of all products.
echo '<span id="gts-o-shipping-total">';
echo $order->get_total_shipping();
echo '</span>';
// Total tax.
echo '<span id="gts-o-tax-total">';
echo $order->get_total_tax();
echo '</span>';
// Required Format: YYYY-MM-DD
// The estimated date on which you will ship the order; this is different from the estimated delivery date.
// If the order contains multiple items, select the latest estimated ship date.
// If you message to your customers a range of days, please include the later end of the range. (e.g., 7 days if your shipping range is 5-7 days).
// The date provided here will be provided to the customer via email.
// The customer will receive a post purchase survey after the Estimated Ship Date has passed.
echo '<span id="gts-o-est-ship-date">';
echo date('Y-m-d', strtotime("+3 day"));
echo '</span>';
// Required Format: YYYY-MM-DD
// The estimated date on which you expect delivery of the order to the customer.
// If the order contains multiple items, select the latest delivery date.
// If you message a range of days, please include the later end of the range. (e.g., 7 days if your range is 5-7 days).
echo '<span id="gts-o-est-delivery-date">';
echo date('Y-m-d', strtotime("+12 day"));
echo '</span>';
// Y if 1 or more items in the order is known to be on backorder or preorder. Otherwise N.
echo '<span id="gts-o-has-preorder">N</span>';
// Y if 1 or more items in the order is delivered digitally. Otherwise N.
echo '<span id="gts-o-has-digital">N</span>';
echo '<!-- end order and merchant information -->';
// This loops over line items
echo '<!-- start repeated item specific information -->';
// This is how to grab line items from the order
$line_items = $order->get_items();
foreach ( $line_items as $item ) {
echo '<span class="gts-item">';
// Required
// Name of the item as it appears in the customer’s order summary.
echo '<span class="gts-i-name">';
$product_name = $item['name'];
echo $product_name;
echo '</span>';
// This is the products SKU
// $sku = $product->get_sku();
// Quantity of the item.
echo '<span class="gts-i-quantity">';
$qty = $item['qty'];
echo $qty;
echo '</span>';
// Price per unit of the item including taxes and rounded
// $total = $order->get_line_total( $item, true, true );
// Price per unit of the item (before discounts)
echo '<span class="gts-i-price">';
$subtotal = $order->get_line_subtotal( $item, true, true );
echo $subtotal;
echo '</span>';
// Optional
// Provide this field only if you submit feeds for Google Shopping.
// Product ID from Google Shopping. This value should match the product ID in the data feed to Google Shopping.
echo '<span class="gts-i-prodsearch-id">';
echo '</span>';
// Optional
// Provide this field only if you submit feeds for Google Shopping.
// Account ID from Google Shopping. This value should match the account ID you use to submit your product data feed to Google Shopping.
echo '<span class="gts-i-prodsearch-store-id">';
echo '</span>';
echo '</span>';
}
echo '<!-- end repeated item specific information -->';
echo '</div>';
echo '<!-- END Google Trusted Stores Order -->';
}
@SmashBrando
Copy link
Author

This code is currently just for the thankyou page interaction.

This is an initial run using the base example from the woocommerce documentation - https://docs.woocommerce.com/document/custom-tracking-code-for-the-thanks-page/

The original code to grab the product name threw an error so I added $product_name = $item['name'];

I'm working on trying to get product feed data from the google product feed extension - https://docs.woocommerce.com/document/google-product-feed/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment