1. Documentação /
  2. Datalayer for WooCommerce /
  3. Developer Resources

Developer Resources

This page is written for WooCommerce developers who want to customize Datalayer for WooCommerce. Requires an understanding of PHP and WordPress development.

Summary

↑ Voltar Para o Topo

This 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

↑ Voltar Para o Topo
NAMEDESCRIPTION
datalayer_before_impressions_loop_itemsFires 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_itemsFires 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_itemsFires 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_pushFires 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_pushFires 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_pushFires 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.

Examples

↑ Voltar Para o Topo

datalayer_before_impressions_loop_items

↑ Voltar Para o Topo

Remove 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

↑ Voltar Para o Topo

Check 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

↑ Voltar Para o Topo

Check 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

↑ Voltar Para o Topo

Get 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

↑ Voltar Para o Topo

Get 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

↑ Voltar Para o Topo

Get 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 );