【发布时间】:2021-12-25 10:29:46
【问题描述】:
我正在尝试安装谷歌广告代码来跟踪我的 woocommerce 商店的转化率
这是应安装在订单确认页面中的代码
<!-- Event snippet for Purchase BSS conversion page -->
<script>
gtag('event', 'conversion', {
'send_to': 'AW-608570056/jkryCPDw7_ACEMiVmKIC',
'value': 0.0,
'currency': 'USD',
'transaction_id': ''
});
</script>
我需要动态的值,
这是我在 function.php 中使用的代码,以便将此代码推送到感谢页面
<?php
// Add custom Theme Functions here
//
/**
* Add custom tracking code to the thank-you page
*/
add_action( 'woocommerce_thankyou', 'my_custom_tracking' );
function my_custom_tracking( $order_id ) {
// Lets grab the order
$order = wc_get_order( $order_id );
/**
* Put your tracking code here
* You can get the order total etc e.g. $order->get_total();
*/
?>
<script>
fbq('track', 'Purchase');
gtag('event', 'conversion', {
'send_to': 'AW-608570056/jkryCPDw7_ACEMiVmKIC',
'value': 0.0,
'currency': 'USD',
'transaction_id': ''
});
</script>
<?php
// This is the order total
$order->get_total();
// This is how to grab line items from the order
$line_items = $order->get_items();
// This loops over line items
foreach ( $line_items as $item ) {
// This will be a product
$product = $order->get_product_from_item( $item );
// This is the products SKU
$sku = $product->get_sku();
// This is the qty purchased
$qty = $item['qty'];
// Line item total cost including taxes and rounded
$total = $order->get_line_total( $item, true, true );
// Line item subtotal (before discounts)
$subtotal = $order->get_line_subtotal( $item, true, true );
}
}
【问题讨论】:
标签: javascript wordpress woocommerce