function schedule_recalculate_ratings() { if ( false === as_has_scheduled_action( 'recalculate_all_product_ratings' ) ) { as_schedule_single_action( time() + 10, 'recalculate_all_product_ratings' ); } } add_action( 'admin_init', 'schedule_recalculate_ratings' ); function recalculate_all_product_ratings() { global $wpdb; // Get all product IDs $product_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'product' AND post_status = 'publish'" ); if ( $product_ids ) { foreach ( $product_ids as $product_id ) { // Recalculate the average rating and review count if ( function_exists( 'wc_update_product_review_counts' ) ) { wc_update_product_review_counts( $product_id ); } } } } add_action( 'recalculate_all_product_ratings', 'recalculate_all_product_ratings' );