Merge pull request #30138 from woocommerce/fix/30136-no-block-templates-for-products

Exclude block templates from showing up in product edit page
This commit is contained in:
Vedanshu Jain 2021-06-29 17:12:17 +05:30 committed by GitHub
commit 89aac04387
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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).
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 );
}
/**
@ -222,6 +224,32 @@ class WC_Admin_Meta_Boxes {
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 || ! function_exists( 'gutenberg_get_block_template' ) ) {
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();