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

Product Rating: Remove empty markup if no ratings are present #9822

Merged
merged 14 commits into from
Jun 27, 2023
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
22 changes: 12 additions & 10 deletions assets/js/atomic/blocks/product-elements/rating/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ interface ProductRatingProps {
shouldDisplayMockedReviewsWhenProductHasNoReviews: boolean;
}

export const Block = ( props: ProductRatingProps ): JSX.Element | null => {
export const Block = ( props: ProductRatingProps ): JSX.Element | undefined => {
const {
textAlign,
isDescendentOfSingleProductBlock,
Expand Down Expand Up @@ -168,16 +168,18 @@ export const Block = ( props: ProductRatingProps ): JSX.Element | null => {
mockedRatings
);

return (
<div className={ className } style={ styleProps.style }>
<div className="wc-block-components-product-rating__container">
{ content }
{ reviews && isDescendentOfSingleProductBlock ? (
<ReviewsCount reviews={ reviews } />
) : null }
if ( reviews || shouldDisplayMockedReviewsWhenProductHasNoReviews ) {
danieldudzic marked this conversation as resolved.
Show resolved Hide resolved
return (
<div className={ className } style={ styleProps.style }>
<div className="wc-block-components-product-rating__container">
{ content }
{ reviews && isDescendentOfSingleProductBlock ? (
<ReviewsCount reviews={ reviews } />
) : null }
</div>
</div>
</div>
);
);
}
};

export default withProductDataContext( Block );
3 changes: 2 additions & 1 deletion src/BlockTypes/ProductRating.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected function render( $attributes, $content, $block ) {
$post_id = $block->context['postId'];
$product = wc_get_product( $post_id );

if ( $product ) {
if ( $product && $product->get_review_count() > 0 ) {
$product_reviews_count = $product->get_review_count();
$product_rating = $product->get_average_rating();
$parsed_attributes = $this->parse_attributes( $attributes );
Expand Down Expand Up @@ -202,5 +202,6 @@ protected function render( $attributes, $content, $block ) {
$rating_html
);
}
return '';
}
}