Created
May 10, 2026 18:13
-
-
Save master-roma/1f7a965b91d50569acbf2f0b50bcbbfb to your computer and use it in GitHub Desktop.
WooCommerce FIX: Product reviews "Show more comments" AJAX loading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* Plugin Name: WooCommerce FIX: Product reviews "Show more comments" AJAX loading | |
| * Description: Fixes duplicated reviews pagination AJAX loading and prematurely hidden "Show more comments" button | |
| * in WooCommerce product editor. Place this file in the /wp-content/mu-plugins/ directory or copy all the code after | |
| * the exit statement to your theme file functions.php | |
| * Author: https://github.com/master-roma | |
| */ | |
| defined( 'ABSPATH' ) || exit; | |
| /** | |
| * Fix #1: | |
| * WordPress admin JS counts only rows with class "comment". | |
| * WooCommerce reviews have class "review", so add "comment" too. | |
| */ | |
| add_filter( | |
| 'comment_class', | |
| function ( $classes, $css_class, $comment_id, $comment, $post_id ) { | |
| if ( ! is_admin() || ! $comment instanceof WP_Comment ) { | |
| return $classes; | |
| } | |
| if ( 'product' !== get_post_type( (int) $comment->comment_post_ID ) ) { | |
| return $classes; | |
| } | |
| if ( ! in_array( 'comment', $classes, true ) ) { | |
| $classes[] = 'comment'; | |
| } | |
| return $classes; | |
| }, | |
| 10, | |
| 5 | |
| ); | |
| /** | |
| * Fix #2: | |
| * Force "load more" to load 10 rows, not the WordPress default 20, | |
| * and correct the button visibility after each AJAX request. | |
| */ | |
| add_action( | |
| 'admin_footer-post.php', | |
| function () { | |
| $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; | |
| if ( ! $screen || 'product' !== $screen->post_type ) { | |
| return; | |
| } | |
| $post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; | |
| if ( ! $post_id ) { | |
| return; | |
| } | |
| $total = (int) get_comments( | |
| array( | |
| 'post_id' => $post_id, | |
| 'count' => true, | |
| 'orderby' => 'none', | |
| 'type__not_in' => array( 'note' ), | |
| ) | |
| ); | |
| ?> | |
| <script> | |
| (function($) { | |
| var wcProductReviewsTotal = <?php echo wp_json_encode( $total ); ?>; | |
| var wcProductReviewsPerPage = 10; | |
| function countLoadedRows() { | |
| return $('#the-comment-list tr[id^="comment-"]:visible').length; | |
| } | |
| function updateShowCommentsButton() { | |
| var $button = $('#show-comments'); | |
| if (!$button.length) { | |
| return; | |
| } | |
| if (countLoadedRows() >= wcProductReviewsTotal) { | |
| $button.hide(); | |
| } else { | |
| $button.show(); | |
| } | |
| } | |
| function isGetCommentsAjax(settings) { | |
| if (!settings || !settings.data) { | |
| return false; | |
| } | |
| if (typeof settings.data === 'string') { | |
| return settings.data.indexOf('action=get-comments') !== -1; | |
| } | |
| return settings.data.action === 'get-comments'; | |
| } | |
| $(function() { | |
| if (!window.commentsBox || typeof window.commentsBox.get !== 'function') { | |
| return; | |
| } | |
| window.commentsBox.load = function(total) { | |
| this.st = countLoadedRows(); | |
| this.total = wcProductReviewsTotal || total; | |
| this.get(this.total, wcProductReviewsPerPage); | |
| return false; | |
| }; | |
| $(document).ajaxComplete(function(event, xhr, settings) { | |
| if (!isGetCommentsAjax(settings)) { | |
| return; | |
| } | |
| window.setTimeout(updateShowCommentsButton, 0); | |
| }); | |
| updateShowCommentsButton(); | |
| }); | |
| })(jQuery); | |
| </script> | |
| <?php | |
| } | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment