Hide reviews in the frontend when they are disabled at store or product level (#50979)

* Not show reviews in the frontend when they are disabled at store or product level

* Add tests for single product template

* Add changefile(s) from automation for the following project(s): woocommerce-blocks, woocommerce

* Add changefile(s) from automation for the following project(s): woocommerce-blocks, woocommerce

* Fix typo

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Alba Rincón 2024-08-28 10:01:55 +02:00 committed by GitHub
parent eb868090b1
commit 1d21197f77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 221 additions and 1 deletions

View File

@ -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();
} );
} );

View File

@ -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();
} );
} );

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
Product Rating: hide reviews in the front end when disabled at store or product level.

View File

@ -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 );