From 3a2ab1c4a59f936027f823122c9d31f9e760bcf1 Mon Sep 17 00:00:00 2001 From: Alexandre Lara Date: Thu, 25 May 2023 11:49:02 -0300 Subject: [PATCH] Remove Add Review link and hide rating when the Product has no rating (#9556) --- .../blocks/product-elements/rating/block.tsx | 37 +++++++------------ .../blocks/product-elements/rating/edit.tsx | 1 + src/BlockTypes/ProductRating.php | 3 +- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/assets/js/atomic/blocks/product-elements/rating/block.tsx b/assets/js/atomic/blocks/product-elements/rating/block.tsx index 8de5e5ebbc2..3e846e0be17 100644 --- a/assets/js/atomic/blocks/product-elements/rating/block.tsx +++ b/assets/js/atomic/blocks/product-elements/rating/block.tsx @@ -22,10 +22,6 @@ type RatingProps = { parentClassName?: string; }; -type AddReviewProps = { - href?: string; -}; - const getAverageRating = ( product: Omit< ProductResponseItem, 'average_rating' > & { average_rating: string; @@ -36,11 +32,6 @@ const getAverageRating = ( return Number.isFinite( rating ) && rating > 0 ? rating : 0; }; -const getReviewsHref = ( product: ProductResponseItem ) => { - const { permalink } = product; - return `${ permalink }#reviews`; -}; - const getRatingCount = ( product: ProductResponseItem ) => { const count = isNumber( product.review_count ) ? product.review_count @@ -89,17 +80,6 @@ const Rating = ( props: RatingProps ): JSX.Element => { ); }; -const AddReview = ( props: AddReviewProps ): JSX.Element | null => { - const { href } = props; - const label = __( 'Add review', 'woo-gutenberg-products-block' ); - - return href ? ( - - { label } - - ) : null; -}; - const ReviewsCount = ( props: { reviews: number } ): JSX.Element => { const { reviews } = props; @@ -128,16 +108,20 @@ interface ProductRatingProps { isDescendentOfQueryLoop: boolean; postId: number; productId: number; + shouldDisplayMockedReviewsWhenProductHasNoReviews: boolean; } export const Block = ( props: ProductRatingProps ): JSX.Element | null => { - const { textAlign, isDescendentOfSingleProductBlock } = props; + const { + textAlign, + isDescendentOfSingleProductBlock, + shouldDisplayMockedReviewsWhenProductHasNoReviews, + } = props; const styleProps = useStyleProps( props ); const { parentClassName } = useInnerBlockLayoutContext(); const { product } = useProductDataContext(); const rating = getAverageRating( product ); const reviews = getRatingCount( product ); - const href = getReviewsHref( product ); const className = classnames( styleProps.className, @@ -147,6 +131,13 @@ export const Block = ( props: ProductRatingProps ): JSX.Element | null => { [ `has-text-align-${ textAlign }` ]: textAlign, } ); + const mockedRatings = shouldDisplayMockedReviewsWhenProductHasNoReviews ? ( + + ) : null; const content = reviews ? ( { parentClassName={ parentClassName } /> ) : ( - + mockedRatings ); return ( diff --git a/assets/js/atomic/blocks/product-elements/rating/edit.tsx b/assets/js/atomic/blocks/product-elements/rating/edit.tsx index bdc91ed662e..78562b950e1 100644 --- a/assets/js/atomic/blocks/product-elements/rating/edit.tsx +++ b/assets/js/atomic/blocks/product-elements/rating/edit.tsx @@ -32,6 +32,7 @@ const Edit = ( { const blockAttrs = { ...attributes, ...context, + shouldDisplayMockedReviewsWhenProductHasNoReviews: true, }; const isDescendentOfQueryLoop = Number.isFinite( context.queryId ); const { isDescendentOfSingleProductBlock } = diff --git a/src/BlockTypes/ProductRating.php b/src/BlockTypes/ProductRating.php index 4384756312f..310f78b1dba 100644 --- a/src/BlockTypes/ProductRating.php +++ b/src/BlockTypes/ProductRating.php @@ -166,8 +166,7 @@ protected function render( $attributes, $content, $block ) { $is_descendent_of_single_product_block ? $reviews_count_html : '' ); } else { - $product_review_url = esc_url( $product_permalink . '#reviews' ); - $html = '' . __( 'Add review', 'woo-gutenberg-products-block' ) . ''; + $html = ''; } return $html;