Allow relevant block templates to be listed in the edit product view

This commit is contained in:
Tom Cafferkey 2022-01-11 11:21:42 +00:00
parent 0a1b850fb3
commit c01e9e6582
1 changed files with 13 additions and 6 deletions

View File

@ -68,7 +68,7 @@ class WC_Admin_Meta_Boxes {
add_action( 'admin_notices', array( $this, 'output_errors' ) );
add_action( 'shutdown', array( $this, 'save_errors' ) );
add_filter( 'theme_product_templates', array( $this, 'remove_block_templates' ), 10, 1 );
add_filter( 'theme_product_templates', array( $this, 'filter_product_block_templates' ), 10, 1 );
}
/**
@ -226,14 +226,15 @@ class WC_Admin_Meta_Boxes {
}
/**
* Remove block-based templates from the list of available templates for products.
* Remove irrelevant block templates from the list of available templates for products.
* This will also remove custom created templates.
*
* @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 || ! function_exists( 'gutenberg_get_block_template' ) ) {
public function filter_product_block_templates( $templates ) {
if ( count( $templates ) === 0 || ! wc_current_theme_is_fse_theme() || ! function_exists( 'gutenberg_get_block_template' ) ) {
return $templates;
}
@ -241,9 +242,15 @@ class WC_Admin_Meta_Boxes {
$filtered_templates = array();
foreach ( $templates as $template_key => $template_name ) {
$gutenberg_template = gutenberg_get_block_template( $theme . '//' . $template_key );
// Filter out the single-product.html template as this is a duplicate of "Default Template".
if ( 'single-product' === $template_key ) {
continue;
}
if ( ! $gutenberg_template ) {
$block_template = gutenberg_get_block_template( $theme . '//' . $template_key );
// If the block template has the product post type specified, include it.
if ( in_array( 'product', $block_template->post_types ) ) {
$filtered_templates[ $template_key ] = $template_name;
}
}