我很想知道下面的代碼有什么問題。我正在使用 WordPress WooCommerce,當我上傳這個functions.php 時,我的站點標簽使我的網站崩潰 -> 全域站點標簽在頭部,由谷歌標簽 chrome 擴展程式檢測到 -> 轉換跟蹤代碼在函式中。 php 并且它只需要在訂單完成后在感謝頁面中可見。
這是我的functions.php 頁面。
歡迎任何幫助,我剛剛開始使用 PHP。
朱利安,
<?php
/**
* Add custom tracking code to the thank-you page
*/
add_action( 'woocommerce_thankyou', 'conversion_tracking' );
function conversion_tracking( $order_id ) {
// Lets grab the order
$order = new WC_Order( $order_id );
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('event', 'conversion', {
'send_to': 'AW-954963704/lbfjCPunp_kCEPitrscD',
'value': '<?php echo $order->get_total(); ?>',
'currency': '<?php echo $order->get_currency(); ?>',
'transaction_id': '<?php echo $order_id; ?>'
});
</script>
// This is the order total
$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 );
}
}
<?php
}
/*-----------------------------------------------------------------------------------*/
/* Init theme framework
/*-----------------------------------------------------------------------------------*/
require( 'auxin/auxin-include/auxin.php' );
/*-----------------------------------------------------------------------------------*/
這是一些 woocommerce 和 google 示例頁面 https://support.google.com/searchads/answer/9133542?hl=fr https://woocommerce.com/document/custom-tracking-code-for-the-thanks-page /#
uj5u.com熱心網友回復:
這應該可以(有些功能部分您不需要 您需要回顯腳本,因為您在PHP中):
add_action( 'woocommerce_thankyou', 'bloomer_conversion_tracking' );
function bloomer_conversion_tracking( $order_id ) {
$order = wc_get_order( $order_id );
?>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('event', 'conversion', {
'send_to': 'AW-954963704/lbfjCPunp_kCEPitrscD',
'value': '<?php echo $order->get_total(); ?>',
'currency': '<?php echo $order->get_currency(); ?>',
'transaction_id': '<?php echo $order_id; ?>'
});
</script>
<?php
}
uj5u.com熱心網友回復:
您的問題是您沒有使用 echo 或任何輸出腳本的方法。這會導致您的致命錯誤。
此外,應添加您的腳本,wc_enqueue_js以便干凈地執行腳本。
function conversion_tracking( $order_id ) {
// Lets grab the order
$order = new WC_Order( $order_id );
$script = "window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('event', 'conversion', {
'send_to': 'AW-954963704/lbfjCPunp_kCEPitrscD',
'value': '{$order->get_total()}',
'currency': {$order->get_currency()}',
'transaction_id': '{$order_id}'
});";
wc_enqueue_js($script);
}
您的其余功能可以清理,但這不是您的問題的一部分。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/352316.html
標籤:php WordPress的 求购 类型转换
