WC_Admin_Meta_Boxes::remove_block_templates
Remove irrelevant block templates from the list of available templates for products. This will also remove custom created templates.
Method of the class: WC_Admin_Meta_Boxes{}
No Hooks.
Returns
String[]. Templates array excluding block-based templates.
Usage
$WC_Admin_Meta_Boxes = new WC_Admin_Meta_Boxes(); $WC_Admin_Meta_Boxes->remove_block_templates( $templates );
- $templates(string[]) (required)
- Array of template header names keyed by the template file name.
WC_Admin_Meta_Boxes::remove_block_templates() WC Admin Meta Boxes::remove block templates code WC 10.6.2
public function remove_block_templates( $templates ) {
if ( count( $templates ) === 0 || ! wp_is_block_theme() ) {
return $templates;
}
$theme = wp_get_theme()->get_stylesheet();
$filtered_templates = array();
foreach ( $templates as $template_key => $template_name ) {
// Filter out the single-product.html template as this is a duplicate of "Default Template".
if ( 'single-product' === $template_key ) {
continue;
}
$block_template = get_block_template( $theme . '//' . $template_key );
// If the block template has the product post type specified, include it.
if ( $block_template && is_array( $block_template->post_types ) && in_array( 'product', $block_template->post_types ) ) {
$filtered_templates[ $template_key ] = $template_name;
}
}
return $filtered_templates;
}