Aquí hay una solución limpia a su pregunta, esto también coloca la secuencia de comandos en el pie de página, al pasar su secuencia de comandos al footer_script
poner en cola
También puede enganchar reemplazar toda la subfunción add_action( 'wp_print_footer_scripts',
Con wp_add_inline_script( 'your-enqueued-script-handle', $script )
que seria mas limpio.
add_action( 'woocommerce_thankyou', 'my_custom_tracking' );
function my_custom_tracking( $order_id ) {
// Lets grab the order.
$order = wc_get_order( $order_id );
// 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 ) {
if ( 27490 === $item->get_product_id() ) {
$script = "gtag('event', 'conversion', {
'send_to': 'XXXXXX',
'value': 1.0,
'currency': 'USD',
'transaction_id': ''
});";
add_action(
'wp_print_footer_scripts',
function() use ( $script ) {
echo '<!-- Event snippet for Purchase Thank You Page for product 27490 -->
<script>' . wp_kses_post( $script ) . '</script>';
}
); // end of add_action to footer script.
}
}
}
.