Automattic\WooCommerce\Internal\Utilities
BlocksUtil::get_blocks_from_widget_area
Get all instances of the specified block from the widget area.
Method of the class: BlocksUtil{}
No Hooks.
Returns
Array. Array of blocks as returned by parse_blocks().
Usage
$result = BlocksUtil::get_blocks_from_widget_area( $block_name );
- $block_name(string) (required)
- The name (id) of a block, e.g.
woocommerce/mini-cart.
BlocksUtil::get_blocks_from_widget_area() BlocksUtil::get blocks from widget area code WC 10.5.0
public static function get_blocks_from_widget_area( $block_name ) {
$blocks = get_option( 'widget_block' );
if ( ! is_array( $blocks ) || empty( $blocks ) ) {
return array();
}
return array_reduce(
$blocks,
function ( $acc, $block ) use ( $block_name ) {
$parsed_blocks = ! empty( $block['content'] ) ? parse_blocks( $block['content'] ) : array();
if ( ! empty( $parsed_blocks ) && $block_name === $parsed_blocks[0]['blockName'] ) {
array_push( $acc, $parsed_blocks[0] );
}
return $acc;
},
array()
);
}