Exclude block templates from showing up in product edit page

This commit is contained in:
Albert Juhé Lluveras 2021-06-21 20:19:09 +02:00
parent 6f812b2689
commit ebee0e157e
1 changed files with 28 additions and 0 deletions

View File

@ -67,6 +67,8 @@ class WC_Admin_Meta_Boxes {
// Error handling (for showing errors from meta boxes on next page load). // Error handling (for showing errors from meta boxes on next page load).
add_action( 'admin_notices', array( $this, 'output_errors' ) ); add_action( 'admin_notices', array( $this, 'output_errors' ) );
add_action( 'shutdown', array( $this, 'save_errors' ) ); add_action( 'shutdown', array( $this, 'save_errors' ) );
add_filter( 'theme_product_templates', array( $this, 'remove_block_templates' ), 10, 1 );
} }
/** /**
@ -222,6 +224,32 @@ class WC_Admin_Meta_Boxes {
do_action( 'woocommerce_process_' . $post->post_type . '_meta', $post_id, $post ); do_action( 'woocommerce_process_' . $post->post_type . '_meta', $post_id, $post );
} }
} }
/**
* Remove block-based templates from the list of available templates for products.
*
* @param string[] $templates Array of template header names keyed by the template file name.
*
* @return string[] Templates array excluding block-based templates.
*/
public function remove_block_templates( $templates ) {
if ( count( $templates ) === 0 ) {
return $templates;
}
$theme = wp_get_theme()->get_stylesheet();
$filtered_templates = array();
foreach ( $templates as $template_key => $template_name ) {
$gutenberg_template = gutenberg_get_block_template( $theme . '//' . $template_key );
if ( ! $gutenberg_template ) {
$filtered_templates[ $template_key ] = $template_name;
}
}
return $filtered_templates;
}
} }
new WC_Admin_Meta_Boxes(); new WC_Admin_Meta_Boxes();