WC_Product_Collection_Block_Tracking::track_collection_instances
Track feature usage of the Product Collection block within the site editor.
Method of the class: WC_Product_Collection_Block_Tracking{}
No Hooks.
Returns
null. Nothing (null).
Usage
$WC_Product_Collection_Block_Tracking = new WC_Product_Collection_Block_Tracking(); $WC_Product_Collection_Block_Tracking->track_collection_instances( $post_id, $post );
- $post_id(int) (required)
- The post ID.
- $post(WP_Post) (required)
- The post object.
WC_Product_Collection_Block_Tracking::track_collection_instances() WC Product Collection Block Tracking::track collection instances code WC 10.6.2
public function track_collection_instances( $post_id, $post ) {
if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST || ! wp_is_block_theme() ) {
return;
}
if ( ! $post instanceof \WP_Post ) {
return;
}
// Don't track autosaves and drafts.
$post_status = $post->post_status;
if ( 'publish' !== $post_status ) {
return;
}
// Important: Only track instances within specific types.
$post_type = $post->post_type;
if ( ! in_array( $post_type, array( 'post', 'page', 'wp_template', 'wp_template_part', 'wp_block' ), true ) ) {
return;
}
if ( ! has_block( 'woocommerce/product-collection', $post ) && ! has_block( 'core/template-part', $post ) && ! has_block( 'core/block', $post ) ) {
return;
}
$blocks = parse_blocks( $post->post_content );
if ( empty( $blocks ) ) {
return;
}
$instances = $this->parse_blocks_track_data( $blocks );
if ( empty( $instances ) ) {
return;
}
// Count orders.
// Hint: Product count included in Track event. See WC_Tracks::get_blog_details().
$order_count = 0;
foreach ( wc_get_order_statuses() as $status_slug => $status_name ) {
$order_count += wc_orders_count( $status_slug );
}
$additional_data = array(
'editor_context' => $this->parse_editor_location_context( $post ),
'order_count' => $order_count,
);
foreach ( $instances as $instance ) {
$event_properties = array_merge(
$additional_data,
$instance
);
\WC_Tracks::record_event(
'product_collection_instance',
$event_properties
);
}
}