diff --git a/plugins/woocommerce-blocks/tests/e2e/tests/templates/template-customization.block_theme.spec.ts b/plugins/woocommerce-blocks/tests/e2e/tests/templates/template-customization.block_theme.spec.ts index b7bd223859f..7e98aeff5b8 100644 --- a/plugins/woocommerce-blocks/tests/e2e/tests/templates/template-customization.block_theme.spec.ts +++ b/plugins/woocommerce-blocks/tests/e2e/tests/templates/template-customization.block_theme.spec.ts @@ -11,6 +11,8 @@ import { CUSTOMIZABLE_WC_TEMPLATES, WC_TEMPLATES_SLUG } from './constants'; CUSTOMIZABLE_WC_TEMPLATES.forEach( ( testData ) => { const userText = `Hello World in the ${ testData.templateName } template`; const fallbackTemplateUserText = `Hello World in the fallback ${ testData.templateName } template`; + const templateTypeName = + testData.templateType === 'wp_template' ? 'template' : 'template part'; test.describe( `${ testData.templateName } template`, async () => { test( 'can be modified and reverted', async ( { @@ -31,6 +33,14 @@ CUSTOMIZABLE_WC_TEMPLATES.forEach( ( testData ) => { attributes: { content: userText }, } ); await editorUtils.saveTemplate(); + // Verify template name didn't change. + // See: https://github.com/woocommerce/woocommerce/issues/42221 + await expect( + page.getByRole( 'heading', { + name: `Editing ${ templateTypeName }: ${ testData.templateName }`, + } ) + ).toBeVisible(); + await testData.visitPage( { frontendUtils, page } ); await expect( page.getByText( userText ).first() ).toBeVisible(); diff --git a/plugins/woocommerce-blocks/tests/e2e/tests/templates/template-customization.block_theme_with_templates.spec.ts b/plugins/woocommerce-blocks/tests/e2e/tests/templates/template-customization.block_theme_with_templates.spec.ts index f841be90643..259c4b0404c 100644 --- a/plugins/woocommerce-blocks/tests/e2e/tests/templates/template-customization.block_theme_with_templates.spec.ts +++ b/plugins/woocommerce-blocks/tests/e2e/tests/templates/template-customization.block_theme_with_templates.spec.ts @@ -15,6 +15,8 @@ CUSTOMIZABLE_WC_TEMPLATES.forEach( ( testData ) => { } const userText = `Hello World in the ${ testData.templateName } template`; const fallbackTemplateUserText = `Hello World in the fallback ${ testData.templateName } template`; + const templateTypeName = + testData.templateType === 'wp_template' ? 'template' : 'template part'; test.describe( `${ testData.templateName } template`, async () => { test( "theme template has priority over WooCommerce's and can be modified", async ( { @@ -35,6 +37,13 @@ CUSTOMIZABLE_WC_TEMPLATES.forEach( ( testData ) => { attributes: { content: userText }, } ); await editorUtils.saveTemplate(); + // Verify template name didn't change. + // See: https://github.com/woocommerce/woocommerce/issues/42221 + await expect( + page.getByRole( 'heading', { + name: `Editing ${ templateTypeName }: ${ testData.templateName }`, + } ) + ).toBeVisible(); // Verify the template is the one modified by the user. await testData.visitPage( { frontendUtils, page } ); diff --git a/plugins/woocommerce/changelog/44254-fix-42221-wrong-template-title b/plugins/woocommerce/changelog/44254-fix-42221-wrong-template-title new file mode 100644 index 00000000000..600f83f7448 --- /dev/null +++ b/plugins/woocommerce/changelog/44254-fix-42221-wrong-template-title @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Fix the Site Editor showing the template slug instead of the template title when saving a WooCommerce block template customized by the theme. \ No newline at end of file diff --git a/plugins/woocommerce/src/Blocks/BlockTemplatesController.php b/plugins/woocommerce/src/Blocks/BlockTemplatesController.php index b78fb978f47..3b713561b49 100644 --- a/plugins/woocommerce/src/Blocks/BlockTemplatesController.php +++ b/plugins/woocommerce/src/Blocks/BlockTemplatesController.php @@ -58,6 +58,7 @@ class BlockTemplatesController { add_action( 'template_redirect', array( $this, 'render_block_template' ) ); add_filter( 'pre_get_block_template', array( $this, 'get_block_template_fallback' ), 10, 3 ); add_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 ); + add_filter( 'get_block_template', array( $this, 'add_block_template_details' ), 10, 1 ); add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 ); add_filter( 'current_theme_supports-block-templates', array( $this, 'remove_block_template_support_for_shop_page' ) ); add_filter( 'taxonomy_template_hierarchy', array( $this, 'add_archive_product_to_eligible_for_fallback_templates' ), 10, 1 ); @@ -346,6 +347,25 @@ class BlockTemplatesController { return $template; } + /** + * Add the template title and description to WooCommerce templates. + * + * @param WP_Block_Template|null $block_template The found block template, or null if there isn't one. + * @return WP_Block_Template|null + */ + public function add_block_template_details( $block_template ) { + if ( ! $block_template ) { + return $block_template; + } + if ( ! BlockTemplateUtils::template_has_title( $block_template ) ) { + $block_template->title = BlockTemplateUtils::get_block_template_title( $block_template->slug ); + } + if ( ! $block_template->description ) { + $block_template->description = BlockTemplateUtils::get_block_template_description( $block_template->slug ); + } + return $block_template; + } + /** * Add the block template objects to be used. * @@ -389,9 +409,7 @@ class BlockTemplatesController { if ( 'custom' !== $template_file->source ) { $template = BlockTemplateUtils::build_template_result_from_file( $template_file, $template_type ); } else { - $template_file->title = BlockTemplateUtils::get_block_template_title( $template_file->slug ); - $template_file->description = BlockTemplateUtils::get_block_template_description( $template_file->slug ); - $query_result[] = $template_file; + $query_result[] = $template_file; continue; } @@ -456,15 +474,13 @@ class BlockTemplatesController { } } - if ( 'theme' === $template->origin && BlockTemplateUtils::template_has_title( $template ) ) { - return $template; - } - if ( $template->title === $template->slug ) { + if ( ! BlockTemplateUtils::template_has_title( $template ) ) { $template->title = BlockTemplateUtils::get_block_template_title( $template->slug ); } if ( ! $template->description ) { $template->description = BlockTemplateUtils::get_block_template_description( $template->slug ); } + return $template; }, $query_result