Skip to content

Instantly share code, notes, and snippets.

@RickardAhlstedt
Created February 2, 2023 08:30
Show Gist options
  • Save RickardAhlstedt/65f3f642d6e4aba8363d890adcf5f21e to your computer and use it in GitHub Desktop.
Save RickardAhlstedt/65f3f642d6e4aba8363d890adcf5f21e to your computer and use it in GitHub Desktop.
Polylang - show review count in tabs on other languages and show rating on translated products.

Thoughts behind this

A translated product(B) doesn't show the original product(A) review-count, but can show the the review from A on B in the comment-tab.
So the code in functions.php does a lookup and fetches the A and get the reviews for it, and modifies the title for the tab.
We also store the modified title in a transient available only for this product, quite simple.

The code in rating.php does a similiar thing, where we check if B doesn't have any reviews, and we're on a different language other than the default-language for the website.
If so, we fetch A and get the rating, review-count, and average-rating, and replace the data for B.
We also store the variables in an array, and cache it by creating a transient from it.

The transient are stored for 6 hours 6 * 60 * 60, you can increase/decrease this value by changing the 6 in the 3rd parameter in set_transient-method-calls.

Feel free to use this, or implement it as you see fit.

<?php
add_filter( 'woocommerce_product_tabs', 'ao_tabs' );
function ao_tabs( $tabs ) {
foreach( $tabs as $sKey => $aTab ) {
if( $aTab['callback'] == 'comments_template' ) {
if( $sTransTitle = get_transient( 'product_' . get_queried_object_id( ) . '_review_count' ) ) {
$tabs[$sKey]['title'] = $sTitle;
}
$sTitle = $aTab['title'];
$iOriginalCount = 0;
$iCount = 0;
$iPos = strpos($sTitle, "(");
if( $iPos ) {
$iCount = substr($sTitle, $iPos, strlen($sTitle) - 1);
$iCount = str_replace(["(", ")"], "", $iCount);
$iOriginalCount = $iCount;
}
if( pll_current_language() != pll_default_language() ) {
$iCurrentProductId = get_queried_object_id();
$iTranslatedId = pll_get_post($iCurrentProductId, pll_default_language());
$aCommentArgs = [
'post_type' => 'product',
'post_id' => $iTranslatedId
];
$aReviews = get_comments($aCommentArgs);
$iCount = count($aReviews);
$sTitle = str_replace($iOriginalCount, $iCount, $sTitle);
$tabs[$sKey]['title'] = $sTitle;
set_transient('product_' . get_queried_object_id() . '_review_count', $sTitle, 6 * 60 * 60);
}
}
}
return $tabs;
}
<?php
/**
* Single Product Rating
*
* This template can be overridden by copying it to yourtheme/woocommerce/single-product/rating.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 3.6.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
global $product;
if ( ! wc_review_ratings_enabled() ) {
return;
}
$rating_count = $product->get_rating_count();
$review_count = $product->get_review_count();
$average = $product->get_average_rating();
if( $rating_count <= 0 && pll_current_language() != pll_default_language() ) {
if( $aRating = get_transient( 'product_' . get_queried_object_id() . '_rating' ) ) {
// Extract the array into separate variables
extract( $aRating );
} else {
$iCurrentId = get_queried_object_id();
$iDefaultId = pll_get_post( $iCurrentId, pll_default_language() );
$product = wc_get_product( $iDefaultId );
$rating_count = $product->get_rating_count();
$review_count = $product->get_review_count();
$average = $product->get_average_rating();
$aRating = [
'rating_count' => $rating_count,
'review_count' => $review_count,
'average' => $average
];
// By caching the rating for 6 hours we save ~50 queries
set_transient( 'product_' . get_queried_object_id() . '_rating', $aRating, 6 * 60 * 60 );
}
}
?>
<?php if ( $rating_count > 0 ) : ?>
<div class="woocommerce-product-rating">
<?php echo wc_get_rating_html( $average, $rating_count ); // phpcs:ignore ?>
<?php if ( comments_open() ) : ?>
<?php //phpcs:disable ?>
<a href="<?php echo get_permalink( $product->get_id() ); ?>#reviews" class="woocommerce-review-link" rel="nofollow">(<?php printf( _n( '%s customer review', '%s customer reviews', $review_count, 'woocommerce' ), '<span class="count">' . esc_html( $review_count ) . '</span>' ); ?>)</a>
<?php // phpcs:enable ?>
<?php endif ?>
</div>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment