2

I have a custom thank you page for after checkout is finished in WooCommerce where I need to insert order data into a Google ecommerce tracking tag to record the sale in analytics. One part of that is adding the following for each item in the order...

ga('ecommerce:addItem', {
  'id': <?php echo $order_id?>,    // Transaction ID. Required.
  'name': 'ACME Product',          // Product name. Required.
  'sku': '1234',                   // SKU/code.
  'category': 'Product Category',  // Category or variation.
  'price': '10.00',                // Unit price.
  'quantity': '1'                  // Quantity.
});

but with inserting the order item's real data using PHP, not the placeholders you see there for name, sku, category, price, and quantity.

In Googling around for answers, I see that I must now use
wc_display_item_meta ( $item ); rather than the deprecated $item_meta = new WC_Order_Item_Meta( $item['item_meta'], $_product );

What I need help with, because I don't yet fully know PHP and I can't seem to find any close examples, is how do I begin to grab the values? Is it a foreach of some kind, or is there a way to directly parse out each item's individual properties out of the order item into a variable that I can then insert in these placeholders?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
rw-intechra
  • 197
  • 1
  • 14
  • You need to iterate through order items using a foreach loop… And what about if you have 2 or more items in the customer order? How do you think to manage each item? Your actual code seems to manage just one item and you should better use existing integrated plugins… – LoicTheAztec Sep 04 '18 at 23:23
  • I do know I need to use a loop to do this same output for multiple items, hence the question. I would love to use a plugin for this. But there isn't one for a custom thankyou page in WooCommerce. Not that I can find. Do you know of one? – rw-intechra Sep 05 '18 at 15:01
  • 1
    I have tried to answer… The php code works without mistakes and errors… Now you may have o make some little changes, testing that for real. But this answer your question title. – LoicTheAztec Sep 05 '18 at 16:52
  • @rw-intechra, please look at this [question](https://stackoverflow.com/questions/54130384/google-analytics-integration-for-orders-without-online-payment-transaction-in-wo). Maybe you will see what I'm doing wrong. – Александра Кузнецова Jan 10 '19 at 15:01

1 Answers1

2

Try the following (you may have to make some changes and add the affiliation Id):

?>
<script>
ga('require', 'ecommerce');
<?php

// GET the WC_Order object instance from, the Order ID
$order = wc_get_order( $order_id );

$order_key = $order->get_order_key();

$transaction_id = $order->get_transaction_id(); // Doesn't always exist

$transaction_id = $order_id; // (Or the order key or the transaction ID if it exist)

?>
ga('ecommerce:addTransaction', {
    'id':        '<?php echo $transaction_id; // To be checked ?>',
    'affiliation': '<?php echo 'UA-XXXXX-Y'; // replace by yours ?>',
    'revenue':   '<?php echo $order->get_total(); ?>',
    'shipping':      '<?php echo $order->get_shipping_total(); ?>',
    'tax':       '<?php echo $order->get_total_tax(); ?>',
    'currency':      '<?php echo get_woocommerce_currency(); // Optional ?>' 
}); <?php

// LOOP START: Iterate through order items
foreach( $order->get_items() as $item_id => $item ) :
    // Get an instance of the WC_Product object
    $product = $item->get_product();

    // Get the product categories for the product
    $categories = wp_get_post_terms( $item->get_product_id(), 'product_cat', array( 'fields' => 'names' ) );
    $category = reset($categories); // Keep only the first product category
?>
ga('ecommerce:addItem', {
    'id':     '<?php echo $transaction_id; ?>',
    'name':       '<?php echo $item->get_name(); ?>',
    'sku':    '<?php echo $product->get_sku(); ?>',
    'category': '<?php echo $category; ?>',
    'price':      '<?php echo wc_get_price_excluding_tax($product);  // OR wc_get_price_including_tax($product) ?>',
    'quantity': '<?php echo $item->get_quantity(); ?>',
    'currency': '<?php echo get_woocommerce_currency(); // Optional ?>' 
});
<?php
endforeach; // LOOP END
?>
ga('ecommerce:send');
</script>
<?php

This code is partially tested and doesn't throw errors… But it need to be tested for real. I hope it will work.

Related:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399