Add Mini Cart Block Tracking

This commit is contained in:
Luigi 2022-04-26 10:04:58 +02:00
parent 542f7ef918
commit d4c540c561
2 changed files with 51 additions and 0 deletions

View File

@ -59,6 +59,27 @@ class WC_Blocks_Utils {
); );
} }
/**
* Get all instances of the specified block on a specific template part.
*
* @param string $block_name The name (id) of a block, e.g. `woocommerce/mini-cart`.
* @param string $template_part_slug The woo page to search, e.g. `header`.
* @return array Array of blocks as returned by parse_blocks().
*/
public static function get_block_from_template_part( $block_name, $template_part_slug ) {
$template = get_block_template( get_stylesheet() . '//' . $template_part_slug, 'wp_template_part' );
$blocks = parse_blocks( $template->content );
return array_values(
array_filter(
$blocks,
function ( $block ) use ( $block_name ) {
return ( $block_name === $block['blockName'] );
}
)
);
}
/** /**
* Check if a given page contains a particular block. * Check if a given page contains a particular block.
* *

View File

@ -166,6 +166,9 @@ class WC_Tracker {
// Cart & checkout tech (blocks or shortcodes). // Cart & checkout tech (blocks or shortcodes).
$data['cart_checkout'] = self::get_cart_checkout_info(); $data['cart_checkout'] = self::get_cart_checkout_info();
// Mini Cart block.
$data['mini_cart_block'] = self::get_mini_cart_info();
// WooCommerce Admin info. // WooCommerce Admin info.
$data['wc_admin_disabled'] = apply_filters( 'woocommerce_admin_disabled', false ) ? 'yes' : 'no'; $data['wc_admin_disabled'] = apply_filters( 'woocommerce_admin_disabled', false ) ? 'yes' : 'no';
@ -776,6 +779,33 @@ class WC_Tracker {
); );
} }
/**
* Get info about the Mini Cart Block.
*
* @return array
*/
public static function get_mini_cart_info() {
$mini_cart_block_name = 'woocommerce/mini-cart';
$mini_cart_block_data = wc_current_theme_is_fse_theme() ? WC_Blocks_Utils::get_block_from_template_part( $mini_cart_block_name, 'header' ) :
array_reduce(
get_option( 'widget_block' ),
function ( $acc, $block ) use ( $mini_cart_block_name ) {
$parsed_blocks = ! empty( $block ) && is_array( $block ) ? parse_blocks( $block['content'] ) : array();
if ( ! empty( $parsed_blocks ) && $mini_cart_block_name === $parsed_blocks[0]['blockName'] ) {
array_push( $acc, $parsed_blocks[0] );
return $acc;
}
return $acc;
},
array()
);
return array(
'mini_cart_used' => empty( $mini_cart_block_data[0] ) ? 'No' : 'Yes',
'mini_cart_block_attributes' => empty( $mini_cart_block_data[0] ) ? array() : $mini_cart_block_data[0]['attrs'],
);
}
/** /**
* Get info about WooCommerce Mobile App usage * Get info about WooCommerce Mobile App usage
* *