Woocommerce 3 中自定义订单接收页面的 Google 分析集成

我有一个自定义的感谢页面,用于在 WooCommerce 中完成结帐后,我需要将订单数据插入到 Google 电子商务跟踪标签中以在分析中记录销售.其中一部分是为订单中的每个项目添加以下内容...

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.
});

但是使用 PHP 插入订单商品的真实数据,而不是您在那里看到的名称、sku、类别、价格和数量的占位符.

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

在谷歌上搜索答案,我发现我现在必须使用
wc_display_item_meta ( $item ); 而不是弃用的 $item_meta = new WC_Order_Item_Meta( $item['item_meta'], $_product );

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

我需要什么帮助,因为我还不完全了解 PHP,而且我似乎找不到任何接近的例子,我该如何开始获取这些值?它是某种形式的 foreach,还是有一种方法可以直接将每个项目的各个属性从订单项目中解析为一个变量,然后我可以将其插入到这些占位符中?

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?

推荐答案

请尝试以下(您可能需要进行一些更改并添加隶属关系 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.

相关:

  • Google Analytics 相关文档(由此文档)
  • 如何获取 WooCommerce 订单详情
  • 获取订单商品和 Woocommerce 3 中的 WC_Order_Item_Product

相关文章