diff --git a/plugins/woocommerce-blocks/tests/e2e/tests/single-product-template/single-product-template.product-rating.spec.ts b/plugins/woocommerce-blocks/tests/e2e/tests/single-product-template/single-product-template.product-rating.spec.ts new file mode 100644 index 00000000000..2fab767912d --- /dev/null +++ b/plugins/woocommerce-blocks/tests/e2e/tests/single-product-template/single-product-template.product-rating.spec.ts @@ -0,0 +1,101 @@ +/** + * External dependencies + */ +import { Editor, test as base, expect } from '@woocommerce/e2e-utils'; + +/** + * Internal dependencies + */ + +const blockData = { + name: 'Single Product', + slug: 'woocommerce/single-product', + product: 'Hoodie', + productSlug: 'hoodie', +}; + +class BlockUtils { + editor: Editor; + admin; + + constructor( { editor, admin }: { editor: Editor; admin } ) { + this.editor = editor; + this.admin = admin; + } + + async configureSingleProductBlockForProduct( product: string ) { + const singleProductBlock = await this.editor.getBlockByName( + 'woocommerce/single-product' + ); + + await singleProductBlock + .locator( `input[type="radio"][value="${ product }"]` ) + .nth( 0 ) + .click(); + + await singleProductBlock.getByText( 'Done' ).click(); + } + + async insertBlockAndVisit( block: string, product: string ) { + await this.admin.createNewPost(); + await this.editor.insertBlock( { name: block } ); + await this.configureSingleProductBlockForProduct( product ); + await this.editor.publishAndVisitPost(); + } +} + +const test = base.extend< { blockUtils: BlockUtils } >( { + blockUtils: async ( { editor, admin }, use ) => { + await use( new BlockUtils( { editor, admin } ) ); + }, +} ); + +test.describe( `${ blockData.slug } Block`, () => { + test( 'Product Rating block is not visible if ratings are disabled for product', async ( { + admin, + page, + } ) => { + await test.step( `Disable reviews for ${ blockData.productSlug }`, async () => { + await page.goto( `/products/${ blockData.productSlug }` ); + await page.click( 'text=Edit product' ); + await page.getByRole( 'link', { name: 'Inventory' } ).click(); + await page.getByRole( 'link', { name: 'Advanced' } ).click(); + await page.waitForSelector( 'text=Enable reviews' ); + await admin.page + .getByRole( 'checkbox', { + name: 'Enable reviews', + } ) + .uncheck(); + await page.getByRole( 'button', { name: 'Update' } ).click(); + } ); + + await page.goto( '/product/hoodie/' ); + + await expect( + page.locator( '.wc-block-components-product-rating' ) + ).not.toBeVisible(); + } ); + + test( 'Product Rating block is not visible if ratings are disabled globally in the store', async ( { + admin, + page, + } ) => { + await test.step( `Disable reviews in the store`, async () => { + await page.goto( + '/wp-admin/admin.php?page=wc-settings&tab=products' + ); + await admin.page + .getByRole( 'checkbox', { + name: 'Enable product reviews', + } ) + .uncheck(); + await page.getByRole( 'button', { name: 'Save changes' } ).click(); + } ); + + await page.goto( '/product/hoodie/' ); + + await expect( + page.locator( '.wc-block-components-product-rating' ) + ).not.toBeVisible(); + } ); +} ); diff --git a/plugins/woocommerce-blocks/tests/e2e/tests/single-product/product-rating-single-product.spec.ts b/plugins/woocommerce-blocks/tests/e2e/tests/single-product/product-rating-single-product.spec.ts new file mode 100644 index 00000000000..0a137f2fc92 --- /dev/null +++ b/plugins/woocommerce-blocks/tests/e2e/tests/single-product/product-rating-single-product.spec.ts @@ -0,0 +1,113 @@ +/** + * External dependencies + */ +import { Editor, test as base, expect } from '@woocommerce/e2e-utils'; + +/** + * Internal dependencies + */ + +const blockData = { + name: 'Single Product', + slug: 'woocommerce/single-product', + product: 'Hoodie', + productSlug: 'hoodie', +}; + +class BlockUtils { + editor: Editor; + admin; + + constructor( { editor, admin }: { editor: Editor; admin } ) { + this.editor = editor; + this.admin = admin; + } + + async configureSingleProductBlockForProduct( product: string ) { + const singleProductBlock = await this.editor.getBlockByName( + 'woocommerce/single-product' + ); + + await singleProductBlock + .locator( `input[type="radio"][value="${ product }"]` ) + .nth( 0 ) + .click(); + + await singleProductBlock.getByText( 'Done' ).click(); + } + + async insertBlockAndVisit( block: string, product: string ) { + await this.admin.createNewPost(); + await this.editor.insertBlock( { name: block } ); + await this.configureSingleProductBlockForProduct( product ); + await this.editor.publishAndVisitPost(); + } +} + +const test = base.extend< { blockUtils: BlockUtils } >( { + blockUtils: async ( { editor, admin }, use ) => { + await use( new BlockUtils( { editor, admin } ) ); + }, +} ); + +test.describe( `${ blockData.slug } Block`, () => { + test( 'Product Rating block is not visible if ratings are disabled for product', async ( { + admin, + page, + blockUtils, + } ) => { + await test.step( `Disable reviews for ${ blockData.productSlug }`, async () => { + await page.goto( `/products/${ blockData.productSlug }` ); + await page.click( 'text=Edit product' ); + await page.getByRole( 'link', { name: 'Inventory' } ).click(); + await page.getByRole( 'link', { name: 'Advanced' } ).click(); + await page.waitForSelector( 'text=Enable reviews' ); + await admin.page + .getByRole( 'checkbox', { + name: 'Enable reviews', + } ) + .uncheck(); + await page.getByRole( 'button', { name: 'Update' } ).click(); + } ); + + await test.step( `Insert the block in a post and visit it`, async () => { + await blockUtils.insertBlockAndVisit( + blockData.slug, + blockData.productSlug + ); + } ); + + await expect( + page.locator( '.wc-block-components-product-rating' ) + ).not.toBeVisible(); + } ); + + test( 'Product Rating block is not visible if ratings are disabled globally in the store', async ( { + admin, + page, + blockUtils, + } ) => { + await test.step( `Disable reviews in the store`, async () => { + await page.goto( + '/wp-admin/admin.php?page=wc-settings&tab=products' + ); + await admin.page + .getByRole( 'checkbox', { + name: 'Enable product reviews', + } ) + .uncheck(); + await page.getByRole( 'button', { name: 'Save changes' } ).click(); + } ); + + await test.step( 'Insert the block in a post and visit it', async () => { + await blockUtils.insertBlockAndVisit( + blockData.slug, + blockData.productSlug + ); + } ); + + await expect( + page.locator( '.wc-block-components-product-rating' ) + ).not.toBeVisible(); + } ); +} ); diff --git a/plugins/woocommerce/changelog/50979-42947-disable-product-rating b/plugins/woocommerce/changelog/50979-42947-disable-product-rating new file mode 100644 index 00000000000..6eb724796d1 --- /dev/null +++ b/plugins/woocommerce/changelog/50979-42947-disable-product-rating @@ -0,0 +1,4 @@ +Significance: minor +Type: update + +Product Rating: hide reviews in the front end when disabled at store or product level. \ No newline at end of file diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/ProductRating.php b/plugins/woocommerce/src/Blocks/BlockTypes/ProductRating.php index 55e95ccca87..1d729677171 100644 --- a/plugins/woocommerce/src/Blocks/BlockTypes/ProductRating.php +++ b/plugins/woocommerce/src/Blocks/BlockTypes/ProductRating.php @@ -114,7 +114,9 @@ class ProductRating extends AbstractBlock { $post_id = isset( $block->context['postId'] ) ? $block->context['postId'] : ''; $product = wc_get_product( $post_id ); - if ( $product && $product->get_review_count() > 0 ) { + if ( $product && $product->get_review_count() > 0 + && $product->get_reviews_allowed() + && wc_reviews_enabled() ) { $product_reviews_count = $product->get_review_count(); $product_rating = $product->get_average_rating(); $parsed_attributes = $this->parse_attributes( $attributes );