Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Product Rating block: Hide rating when the product has no reviews #9556

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions assets/js/atomic/blocks/product-elements/rating/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ type RatingProps = {
parentClassName?: string;
};

type AddReviewProps = {
href?: string;
};

const getAverageRating = (
product: Omit< ProductResponseItem, 'average_rating' > & {
average_rating: string;
Expand All @@ -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
Expand Down Expand Up @@ -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 ? (
<a className="wc-block-components-product-rating__link" href={ href }>
{ label }
</a>
) : null;
};

const ReviewsCount = ( props: { reviews: number } ): JSX.Element => {
const { reviews } = props;

Expand Down Expand Up @@ -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,
Expand All @@ -147,6 +131,13 @@ export const Block = ( props: ProductRatingProps ): JSX.Element | null => {
[ `has-text-align-${ textAlign }` ]: textAlign,
}
);
const mockedRatings = shouldDisplayMockedReviewsWhenProductHasNoReviews ? (
<Rating
rating={ 0 }
reviews={ 0 }
parentClassName={ parentClassName }
/>
) : null;

const content = reviews ? (
<Rating
Expand All @@ -155,7 +146,7 @@ export const Block = ( props: ProductRatingProps ): JSX.Element | null => {
parentClassName={ parentClassName }
/>
) : (
<AddReview href={ href } />
mockedRatings
);

return (
Expand Down
1 change: 1 addition & 0 deletions assets/js/atomic/blocks/product-elements/rating/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const Edit = ( {
const blockAttrs = {
...attributes,
...context,
shouldDisplayMockedReviewsWhenProductHasNoReviews: true,
};
const isDescendentOfQueryLoop = Number.isFinite( context.queryId );
const { isDescendentOfSingleProductBlock } =
Expand Down
3 changes: 1 addition & 2 deletions src/BlockTypes/ProductRating.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<a class="wc-block-components-product-rating__link" href="' . $product_review_url . '">' . __( 'Add review', 'woo-gutenberg-products-block' ) . '</a>';
$html = '';
}

return $html;
Expand Down