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
1 Answer
Reset to default 0Let'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
版权声明:本文标题:woocommerce offtopic - Get $order in wp_head 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744770042a2624283.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论