admin管理员组

文章数量:1391850

I am trying to get the purchased products on the order-received page to do a remarketing dataLayer. The script must go in the <head> section of the html code. How can I get the $order to use it in the <head> section?

I can get the $order_id in woocommerce_thankyou but not in wp_head

function tracking_thank_you_page( $order_id ) {
?>
    <script>
        console.log( 'order_id: <?php echo $order_id; ?>' );
    </script>
<?php
}
add_action( 'woocommerce_thankyou', 'tracking_thank_you_page' );

I have the $order_id with the code above.

function tracking_in_head( $order_id ) {
?>
    <script>
        console.log( 'order_id: <?php echo $order_id; ?>' );
    </script>
<?php
}
add_action( 'wp_head', 'tracking_in_head' );

I don't have access to the $order_id in the wp_head

UPDATE: After Hectors useful reply I edited my code. It creates the javascript dataLayer with the products SKU:

function tracking_in_head() {
    $product_skus = '';
    global $wp_query;
    if( !empty( $wp_query->query_vars[ 'order-received' ] ) ) {
        $order = wc_get_order( $wp_query->query_vars[ 'order-received' ] );
        $items = $order->get_items();
        echo "<script>
            dataLayer = [];
            dataLayer.push({
                'event': 'purchase',
                ";
                if( !empty( $items ) ) {
                    echo "'items': [";
                        foreach( $items as $item ) {
                            $product = wc_get_product( $item[ 'product_id' ] );
                            echo "{
                                'id': '" . $product->get_sku() . "',
                                'google_business_vertical': 'retail'
                            },";
                        }
                    echo "]";
                }
            echo "});
        </script>";
    }
}
add_action( 'wp_head', 'tracking_in_head' );

Is there anything to improve on this code?

I am trying to get the purchased products on the order-received page to do a remarketing dataLayer. The script must go in the <head> section of the html code. How can I get the $order to use it in the <head> section?

I can get the $order_id in woocommerce_thankyou but not in wp_head

function tracking_thank_you_page( $order_id ) {
?>
    <script>
        console.log( 'order_id: <?php echo $order_id; ?>' );
    </script>
<?php
}
add_action( 'woocommerce_thankyou', 'tracking_thank_you_page' );

I have the $order_id with the code above.

function tracking_in_head( $order_id ) {
?>
    <script>
        console.log( 'order_id: <?php echo $order_id; ?>' );
    </script>
<?php
}
add_action( 'wp_head', 'tracking_in_head' );

I don't have access to the $order_id in the wp_head

UPDATE: After Hectors useful reply I edited my code. It creates the javascript dataLayer with the products SKU:

function tracking_in_head() {
    $product_skus = '';
    global $wp_query;
    if( !empty( $wp_query->query_vars[ 'order-received' ] ) ) {
        $order = wc_get_order( $wp_query->query_vars[ 'order-received' ] );
        $items = $order->get_items();
        echo "<script>
            dataLayer = [];
            dataLayer.push({
                'event': 'purchase',
                ";
                if( !empty( $items ) ) {
                    echo "'items': [";
                        foreach( $items as $item ) {
                            $product = wc_get_product( $item[ 'product_id' ] );
                            echo "{
                                'id': '" . $product->get_sku() . "',
                                'google_business_vertical': 'retail'
                            },";
                        }
                    echo "]";
                }
            echo "});
        </script>";
    }
}
add_action( 'wp_head', 'tracking_in_head' );

Is there anything to improve on this code?

Share Improve this question edited Feb 7, 2020 at 10:05 István asked Feb 7, 2020 at 7:32 IstvánIstván 851 silver badge11 bronze badges 3
  • What do you expect as an improvement? Isn't it working? – Hector Commented Feb 9, 2020 at 11:20
  • It does work perfectly. I thought maybe there is a cleaner way to get the ordered items SKU or to transform the data into the javascript datalayer prettier. – István Commented Feb 10, 2020 at 12:41
  • 1 Your codes looks fine. Especially if it works, then it is good to go. – Hector Commented Feb 15, 2020 at 11:48
Add a comment  | 

1 Answer 1

Reset to default 0

Let's check both hooks. The woocommerce_thankyou hook is added on line 80 of thankyou.php file. The hook is getting $order->get_id() as an argument. So, you have access to the order ID in when you hook to woocommerce_thankyou in your first example.

But the wp_head action which is loading from wp_head() function doesn't have any argument. So, in the second snippet, you don't have any $order_id variable.

If you want to get order in head section, you may need to use WooCommerce functions to get the order and then print it as javascript in head.

Example:

function tracking_in_head() {
// Get needed data here.
$all_orders = wc_get_orders();
// Check https://docs.woocommerce/wc-apidocs/source-function-wc_get_order.html to see WC order functions.
?>
    <script>
        console.log( 'order_id: <?php echo $order_id; ?>' );
    </script>
<?php
}
add_action( 'wp_head', 'tracking_in_head' );

本文标签: woocommerce offtopicGet order in wphead