This page is written for WooCommerce developers who want to customize Datalayer for WooCommerce. Requires an understanding of PHP and WordPress development.
Summary
↑ Back to topThis guide shows the hooks available to you who want to modify the default Datalayer for WooCommerce behavior.
It is important to be clear that we will not give complete examples of the possibilities, but only an idea of what is possible to do.
We believe that in most cases you don’t need to use the hooks, be careful when using them, and only use them if you really need them.
Filter Reference
↑ Back to topNAME | DESCRIPTION |
---|---|
datalayer_before_impressions_loop_items | Fires in the category/product page before the start of the product loop that builds the datalayer. This hook contains an array of products (WC_Product) and can be used for extra checks. |
datalayer_before_checkout_loop_items | Fires in the checkout pages before the start of the product loop that builds the datalayer. This hook contains an array of products (WC_Product) and can be used for extra checks. |
datalayer_before_order_loop_items | Fires in the thankyou page before the start of the product loop that builds the datalayer. This hook contains an array of products (WC_Order_Item) and can be used for extra checks. |
datalayer_during_impressions_loop_items_before_push | Fires on the category/product page during the items loop that builds the data layer before the push. This hook contains an array of the item ready for the datalayer and the product (WC_Product). Can be used to insert, change or remove new parameters in the ‘items’ array of the datalayer. |
datalayer_during_checkout_loop_items_before_push | Fires on the checkout pages during the items loop that builds the data layer before the push. This hook contains an array of the item ready for the datalayer and the product (WC_Product). Can be used to insert, change or remove new parameters in the ‘items’ array of the datalayer. |
datalayer_during_order_loop_items_before_push | Fires on the thankyou page during the items loop that builds the data layer before the push. This hook contains an array of the item ready for the datalayer and the product (WC_Order_Item). Can be used to insert, change or remove new parameters in the ‘items’ array of the datalayer. |
datalayer_before_user_phone_push | Fires on all pages before sending the User Phone to the data layer. This hook contains a string with user phone and can be used for extra modifications. |
Examples
↑ Back to topdatalayer_before_impressions_loop_items
↑ Back to topRemove a specify product in the category/product pages:
function my_custom_hook_datalayer_impressions_verify_product( $products ) {
foreach ( $products as $key => $product ) {
if($product->get_id() === 225){
unset( $products[$key] );
}
}
$products = array_values($products);
return $products;
}
add_filter( 'datalayer_before_impressions_loop_items', 'my_custom_hook_datalayer_impressions_verify_product' );
Add a specify product in the category/product page:
function my_custom_hook_datalayer_impressions_add_product( $products ) {
$products[] = $new_product = new WC_Product( 175 ); // Create a new product object
return $products;
}
add_filter( 'datalayer_before_impressions_loop_items', 'my_custom_hook_datalayer_impressions_add_product' );
datalayer_before_checkout_loop_items
↑ Back to topCheck if the product is part of a WooCommerce package and remove it in the checkout pages:
function my_custom_hook_datalayer_checkout_verify_product( $products ) {
foreach ( $products as $key => $product ) {
// Check if the product is part of a WooCommerce package
if ( ! empty( wc_pb_get_bundled_product_map( $product['product'] ) ) ) {
// If it's part of a package, remove the product from the array
unset( $products[$key] );
}
}
return $products;
}
add_filter( 'datalayer_before_checkout_loop_items', 'my_custom_hook_datalayer_checkout_verify_product' );
Add a specify product in the category/product in the checkout pages:
function my_custom_hook_datalayer_checkout_add_product( $products ) {
$quantity = 1;
$variation_id = null;
$products[] = array(
'quantity' => $quantity,
'product' => $new_product = new WC_Product( 225 ), // Create a new product object
'variation_id' => $variation_id
);
return $products;
}
add_filter( 'datalayer_before_checkout_loop_items', 'my_custom_hook_datalayer_checkout_add_product' );
datalayer_before_order_loop_items
↑ Back to topCheck if the product is part of a WooCommerce package and remove it in the thankyou page:
function my_custom_hook_datalayer_order_verify_product( $products ) {
foreach ( $products->get_items() as $key => $item ) {
// Check if the item is part of a WooCommerce package
if ( wc_pb_is_bundled_order_item( $item ) ) {
// If it's part of a package, remove the item from the array
$products->remove_item( $key );
}
}
return $products;
}
add_filter( 'datalayer_before_order_loop_items', 'my_custom_hook_datalayer_order_verify_product' );
Add a specify product in the thankyou page:
function my_custom_hook_datalayer_order_add_product( $products ) {
$new_product_id = 225; // ID of the product you want to add
$new_product = wc_get_product( $new_product_id );
if ( $new_product ) {
$order_item = new WC_Order_Item_Product();
$order_item->set_props(
array(
'product' => $new_product,
'quantity' => 1
)
);
$products->add_item( $order_item );
}
return $products;
}
add_filter( 'datalayer_before_order_loop_items', 'my_custom_hook_datalayer_order_add_product' );
datalayer_during_impressions_loop_items_before_push
↑ Back to topGet a product specific attribute (pa_color) and add it as a new parameter (item_category2) in the datalayer on category/product pages.
function my_custom_hook_datalayer_impressions_verify_product( $datalayer, $product ) {
$attribute = $product->get_attribute('pa_color');
if($attribute){
$datalayer['item_category2'] = $attribute;
}
return $datalayer;
}
add_filter( 'datalayer_during_impressions_loop_items_before_push', 'my_custom_hook_datalayer_impressions_verify_product', 10, 2 );
datalayer_during_checkout_loop_items_before_push
↑ Back to topGet a product specific attribute (pa_color) and add it as a new parameter (item_category2) in the datalayer on checkout pages.
function my_custom_hook_datalayer_checkout_verify_product( $datalayer, $product ) {
$attribute = $product->get_attribute('pa_color');
if($attribute){
$datalayer['item_category2'] = $attribute;
}
return $datalayer;
}
add_filter( 'datalayer_during_checkout_loop_items_before_push', 'my_custom_hook_datalayer_checkout_verify_product', 10, 2 );
datalayer_during_order_loop_items_before_push
↑ Back to topGet a product specific attribute (pa_color) and add it as a new parameter (item_category2) in the datalayer on thankyou page.
function my_custom_hook_datalayer_order_verify_product( $datalayer, $product ) {
$product_attributes_data = wc_get_product( $product->get_product_id() );
$attribute = $product_attributes_data->get_attribute('pa_color');
if($attribute){
$datalayer['item_category2'] = $attribute;
}
return $datalayer;
}
add_filter( 'datalayer_during_order_loop_items_before_push', 'my_custom_hook_datalayer_order_verify_product', 10, 2 );
datalayer_before_user_phone_push
↑ Back to topRemove the + sign from the phone number.
function my_custom_hook_datalayer_before_user_phone_push( $user_phone ){
$user_phone = str_replace('+','',$user_phone);
return $user_phone;
}
add_filter( 'datalayer_before_user_phone_push', 'my_custom_hook_datalayer_before_user_phone_push', 10, 1 );