Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Show Product Ratings and Reviews in all languages. #120

Open
Jon007 opened this issue Feb 3, 2017 · 4 comments
Open

Show Product Ratings and Reviews in all languages. #120

Jon007 opened this issue Feb 3, 2017 · 4 comments

Comments

@Jon007
Copy link
Contributor

Jon007 commented Feb 3, 2017

Idea: Add option for product page to show product reviews in all languages.

For example a product may have reviews in one language only, so when viewing product in another language reviews and ratings do not appear.

It may be useful and informative to customers to show the ratings and reviews from all languages.

@Jon007
Copy link
Contributor Author

Jon007 commented Feb 3, 2017

Actually the following code works for the showing comments/reviews in all languages across posts and products, with the limitations that:

  • Reviews total doesn't work for products (comment total works for posts)
  • but Ratings from other languages show up but they wouldn't be considered in an average rating (if there was such a thing) or affect Sort by popularity
function sort_merged_comments($a, $b) { 
	return $a->comment_ID - $b->comment_ID;
}

function merge_comments($comments, $post_ID) {
	global $sitepress;
	remove_filter( 'comments_clauses', array( $sitepress, 'comments_clauses' ) );
	// get all the languages for which this post exists
	$languages = icl_get_languages('skip_missing=1');
	$post = get_post( $post_ID );
	$type = $post->post_type;
	foreach($languages as $code => $l) {
		// in $comments are already the comments from the current language
		if(!$l['active']) {
			$otherID = icl_object_id($post_ID, $type, false, $l['language_code']);
			$othercomments = get_comments( array('post_id' => $otherID, 'status' => 'approve', 'order' => 'ASC') );
			$comments = array_merge($comments, $othercomments);
		}
	}
	if ($languages) {
		// if we merged some comments in we need to reestablish an order
		usort($comments, 'sort_merged_comments');
	}
	//
	add_filter( 'comments_clauses', array( $sitepress, 'comments_clauses' ) );

	return $comments;
}

function merge_comment_count($count, $post_ID) {
	// get all the languages for which this post exists
	$languages = icl_get_languages();
	$post = get_post( $post_ID );
	$type = $post->post_type;

	foreach($languages as $l) {
			// in $count is already the count from the current language
		if(!$l['active']) {
			$otherID = icl_object_id($post_ID, $type, false, $l['language_code']);
			if($otherID) {
				// cannot use call_user_func due to php regressions
				if ($type == 'page') {
					$otherpost = get_page($otherID);
				} else {
					$otherpost = get_post($otherID);
				}
				if ($otherpost) {
					// increment comment count using translation post comment count.
					$count = $count + $otherpost->comment_count;
				}
			}
		}
	}
	return $count;
}

add_filter('comments_array', 'merge_comments', 100, 2);
add_filter('get_comments_number', 'merge_comment_count', 100, 2);

/*stop Polylang filtering comments*/
function polylang_remove_comments_filter() {
    global $polylang;
    remove_filter('comments_clauses', array(&$polylang->filters, 'comments_clauses'));
}
add_action('wp','polylang_remove_comments_filter');

@hyyan
Copy link
Owner

hyyan commented Jun 5, 2017

@Jon007 seems as a good idea , I will give the code a try

@Jon007
Copy link
Contributor Author

Jon007 commented Jun 5, 2017

I haven't tried this again on WooCommerce3. Currently I use this on Post pages and disable it on Product pages as I didn't get a clean implementation for Products.

Instead on the Product pages at the moment I remove the Reviews tab and instead run a modified Decent Comments loop to print out a selection of reviews from the related Product Category as well as the Product itself after the product information.
The Decent Comments modification was to turn off the Decent Comments renderer part and instead use native rendering, the key lines to enable standard WooCommerce output being:
$options['callback'] = 'woocommerce_comments';
$output .= wp_list_comments($options, $comments);
This was affected by some issue with Woo3 but Mike Jolley included a fix in one of the recent patches :)

So I just mention this in case it gives any extra clues...

@Jon007 Jon007 removed their assignment Jun 20, 2017
@Jon007
Copy link
Contributor Author

Jon007 commented Jun 30, 2020

This is requested by me, but I appear to be the only active maintainer and I have a custom solution so I have no incentive to implement it ...

.. the problem is that once you implement a second language, the number of rating and positive comments you have for each product is divided by 2.. or rather your products in the second language are zero-rated even if they are really popular in the original language, and new ratings that come in will be split between different language versions of the product.

This obviously continues the more languages there are.

Amazon pulls in review for other Amazon sites, Tripadvisor pulls in reviews in other languages, commercially you can't afford not to keep all your reviews.

I'll leave this open a bit longer for a keen volunteer!
if you would like a clue to get started, the half-baked solution I would like to see an improved version of in this plugin is located here, and depends on the Decent Comments plugin.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants