WooCommerce Code Reference

WC_Comments

Comments class.

Table of Contents

COMMENT_COUNT_CACHE_GROUP  = 'wc_comment_counts'
The cache group to use for comment counts.
PRODUCT_REVIEWS_PENDING_COUNT_CACHE_KEY  = 'woocommerce_product_reviews_pending_count'
The cache key to use for pending product reviews counts.
add_avatar_for_review_comment_type()  : array<string|int, mixed>
Make sure WP displays avatars for comments with the `review` type.
add_comment_purchase_verification()  : bool
Determine if a review is from a verified owner at submission.
add_comment_rating()  : mixed
Rating field for comments.
add_review_comment_filter()  : array<string|int, mixed>
Add Product Reviews filter for `review` comment type.
check_comment_rating()  : array<string|int, mixed>
Validate the comment ratings.
clear_transients()  : mixed
Ensure product average rating and review count is kept up to date.
comment_moderation_recipients()  : array<string|int, mixed>
Modify recipient of review email.
comments_open()  : bool
See if comments are open.
delete_comments_count_cache()  : mixed
Delete comments count cache whenever there is new comment or the status of a comment changes. Cache will be regenerated next time WC_Comments::wp_count_comments() is called.
exclude_action_log_comments()  : array<string|int, mixed>
Exclude action_log comments from queries.
exclude_action_log_comments_from_feed_where()  : string
Exclude action_log comments from queries and RSS.
exclude_order_comments()  : array<string|int, mixed>
Exclude order comments from queries and RSS.
exclude_order_comments_from_feed_join()  : mixed
Exclude order comments from feed.
exclude_order_comments_from_feed_where()  : string
Exclude order comments from queries and RSS.
exclude_webhook_comments()  : array<string|int, mixed>
Exclude webhook comments from queries and RSS.
exclude_webhook_comments_from_feed_join()  : mixed
Exclude webhooks comments from feed.
exclude_webhook_comments_from_feed_where()  : string
Exclude webhook comments from queries and RSS.
get_average_rating_for_product()  : float
Get product rating for a product. Please note this is not cached.
get_products_reviews_pending_moderation_counter()  : int
Fetches (and populates if needed) the counter.
get_rating_counts_for_product()  : array<string|int, int>
Get product rating count for a product. Please note this is not cached.
get_review_count_for_product()  : int
Get product review count for a product (not replies). Please note this is not cached.
get_review_counts_for_product_ids()  : array<string|int, mixed>
Utility function for getting review counts for multiple products in one query. This is not cached.
increment_comments_count_cache_on_wp_insert_comment()  : void
Callback for 'wp_insert_comment' to delete the comment count cache if the comment is included in the count.
init()  : mixed
Hook in methods.
maybe_adjust_products_reviews_pending_moderation_counter()  : void
Handles `transition_comment_status` hook processing and actualizes the counter.
maybe_bump_products_reviews_pending_moderation_counter()  : void
Handles `wp_insert_comment` hook processing and actualizes the counter.
update_comment_type()  : array<string|int, mixed>
Update comment type of product reviews.
update_comments_count_cache_on_comment_status_change()  : void
Callback for 'comment_status_change' to delete the comment count cache if the comment is included in the count.
validate_product_review_verified_owners()  : mixed
Validate product reviews if requires a verified owner.
wp_count_comments()  : object
Remove order notes, webhook delivery logs, and product reviews from wp_count_comments().
is_comment_excluded_from_wp_comment_counts()  : bool
Determines whether the given comment should be included in the core WP comment counts that are displayed in the WordPress admin.
is_default_comment_type()  : bool
Determines if a comment is of the default type.

Constants

COMMENT_COUNT_CACHE_GROUP

The cache group to use for comment counts.

private string COMMENT_COUNT_CACHE_GROUP = 'wc_comment_counts'

PRODUCT_REVIEWS_PENDING_COUNT_CACHE_KEY

The cache key to use for pending product reviews counts.

private string PRODUCT_REVIEWS_PENDING_COUNT_CACHE_KEY = 'woocommerce_product_reviews_pending_count'

Methods

add_avatar_for_review_comment_type()

Make sure WP displays avatars for comments with the `review` type.

public static add_avatar_for_review_comment_type(array<string|int, mixed> $comment_types) : array<string|int, mixed>
Parameters
$comment_types : array<string|int, mixed>

Comment types.

Tags
since
2.3
Return values
array<string|int, mixed>

add_comment_purchase_verification()

Determine if a review is from a verified owner at submission.

public static add_comment_purchase_verification(int $comment_id) : bool
Parameters
$comment_id : int

Comment ID.

Return values
bool

add_comment_rating()

Rating field for comments.

public static add_comment_rating(int $comment_id) : mixed
Parameters
$comment_id : int

Comment ID.

Return values
mixed

add_review_comment_filter()

Add Product Reviews filter for `review` comment type.

public static add_review_comment_filter(array<string|int, mixed> $comment_types) : array<string|int, mixed>
Parameters
$comment_types : array<string|int, mixed>

Array of comment type labels keyed by their name.

Tags
since
6.0.0
Return values
array<string|int, mixed>

check_comment_rating()

Validate the comment ratings.

public static check_comment_rating(array<string|int, mixed> $comment_data) : array<string|int, mixed>
Parameters
$comment_data : array<string|int, mixed>

Comment data.

Return values
array<string|int, mixed>

clear_transients()

Ensure product average rating and review count is kept up to date.

public static clear_transients(int $post_id) : mixed
Parameters
$post_id : int

Post ID.

Return values
mixed

comment_moderation_recipients()

Modify recipient of review email.

public static comment_moderation_recipients(array<string|int, mixed> $emails, int $comment_id) : array<string|int, mixed>
Parameters
$emails : array<string|int, mixed>

Emails.

$comment_id : int

Comment ID.

Return values
array<string|int, mixed>

comments_open()

See if comments are open.

public static comments_open(bool $open, int $post_id) : bool
Parameters
$open : bool

Whether the current post is open for comments.

$post_id : int

Post ID.

Tags
since
3.1.0
Return values
bool

delete_comments_count_cache()

Delete comments count cache whenever there is new comment or the status of a comment changes. Cache will be regenerated next time WC_Comments::wp_count_comments() is called.

public static delete_comments_count_cache() : mixed
Return values
mixed

exclude_action_log_comments()

Exclude action_log comments from queries.

public static exclude_action_log_comments(array<string|int, mixed> $clauses, WP_Comment_Query $comment_query) : array<string|int, mixed>
Parameters
$clauses : array<string|int, mixed>

A compacted array of comment query clauses.

$comment_query : WP_Comment_Query

The WP_Comment_Query being filtered.

Tags
since
9.9
Return values
array<string|int, mixed>

exclude_action_log_comments_from_feed_where()

Exclude action_log comments from queries and RSS.

public static exclude_action_log_comments_from_feed_where(string $where) : string
Parameters
$where : string

The WHERE clause of the query.

Tags
since
9.9
Return values
string

exclude_order_comments()

Exclude order comments from queries and RSS.

public static exclude_order_comments(array<string|int, mixed> $clauses) : array<string|int, mixed>

This code should exclude shop_order comments from queries. Some queries (like the recent comments widget on the dashboard) are hardcoded. and are not filtered, however, the code current_user_can( 'read_post', $comment->comment_post_ID ) should keep them safe since only admin and. shop managers can view orders anyway.

The frontend view order pages get around this filter by using remove_filter('comments_clauses', array( 'WC_Comments' ,'exclude_order_comments'), 10, 1 );

Parameters
$clauses : array<string|int, mixed>

A compacted array of comment query clauses.

Return values
array<string|int, mixed>

exclude_order_comments_from_feed_join()

Exclude order comments from feed.

public static exclude_order_comments_from_feed_join(mixed $join) : mixed
Parameters
$join : mixed

Deprecated.

Tags
deprecated
3.1
Return values
mixed

exclude_order_comments_from_feed_where()

Exclude order comments from queries and RSS.

public static exclude_order_comments_from_feed_where(string $where) : string
Parameters
$where : string

The WHERE clause of the query.

Return values
string

exclude_webhook_comments()

Exclude webhook comments from queries and RSS.

public static exclude_webhook_comments(array<string|int, mixed> $clauses) : array<string|int, mixed>
Parameters
$clauses : array<string|int, mixed>

A compacted array of comment query clauses.

Tags
since
2.2
Return values
array<string|int, mixed>

exclude_webhook_comments_from_feed_join()

Exclude webhooks comments from feed.

public static exclude_webhook_comments_from_feed_join(mixed $join) : mixed
Parameters
$join : mixed

Deprecated.

Tags
deprecated
3.1
Return values
mixed

exclude_webhook_comments_from_feed_where()

Exclude webhook comments from queries and RSS.

public static exclude_webhook_comments_from_feed_where(string $where) : string
Parameters
$where : string

The WHERE clause of the query.

Tags
since
2.1
Return values
string

get_average_rating_for_product()

Get product rating for a product. Please note this is not cached.

public static get_average_rating_for_product(WC_Product &$product) : float
Parameters
$product : WC_Product

Product instance.

Tags
since
3.0.0
Return values
float

get_products_reviews_pending_moderation_counter()

Fetches (and populates if needed) the counter.

public static get_products_reviews_pending_moderation_counter() : int
Return values
int

get_rating_counts_for_product()

Get product rating count for a product. Please note this is not cached.

public static get_rating_counts_for_product(WC_Product &$product) : array<string|int, int>
Parameters
$product : WC_Product

Product instance.

Tags
since
3.0.0
Return values
array<string|int, int>

get_review_count_for_product()

Get product review count for a product (not replies). Please note this is not cached.

public static get_review_count_for_product(WC_Product &$product) : int
Parameters
$product : WC_Product

Product instance.

Tags
since
3.0.0
Return values
int

get_review_counts_for_product_ids()

Utility function for getting review counts for multiple products in one query. This is not cached.

public static get_review_counts_for_product_ids(array<string|int, mixed> $product_ids) : array<string|int, mixed>
Parameters
$product_ids : array<string|int, mixed>

Array of product IDs.

Tags
since
5.0.0
Return values
array<string|int, mixed>

increment_comments_count_cache_on_wp_insert_comment()

Callback for 'wp_insert_comment' to delete the comment count cache if the comment is included in the count.

public static increment_comments_count_cache_on_wp_insert_comment(int $comment_id, WP_Comment $comment) : void
Parameters
$comment_id : int

The comment ID.

$comment : WP_Comment

Comment object.

Return values
void

maybe_adjust_products_reviews_pending_moderation_counter()

Handles `transition_comment_status` hook processing and actualizes the counter.

public static maybe_adjust_products_reviews_pending_moderation_counter(int|string $new_status, int|string $old_status, WP_Comment $comment) : void
Parameters
$new_status : int|string

New status.

$old_status : int|string

Old status.

$comment : WP_Comment

Comment object.

Return values
void

maybe_bump_products_reviews_pending_moderation_counter()

Handles `wp_insert_comment` hook processing and actualizes the counter.

public static maybe_bump_products_reviews_pending_moderation_counter(int $comment_id, WP_Comment $comment) : void
Parameters
$comment_id : int

Comment ID.

$comment : WP_Comment

Comment object.

Return values
void

update_comment_type()

Update comment type of product reviews.

public static update_comment_type(array<string|int, mixed> $comment_data) : array<string|int, mixed>
Parameters
$comment_data : array<string|int, mixed>

Comment data.

Tags
since
3.5.0
Return values
array<string|int, mixed>

update_comments_count_cache_on_comment_status_change()

Callback for 'comment_status_change' to delete the comment count cache if the comment is included in the count.

public static update_comments_count_cache_on_comment_status_change(int|string $new_status, int|string $old_status, WP_Comment $comment) : void
Parameters
$new_status : int|string

The new comment status.

$old_status : int|string

The old comment status.

$comment : WP_Comment

Comment object.

Return values
void

validate_product_review_verified_owners()

Validate product reviews if requires a verified owner.

public static validate_product_review_verified_owners(int $comment_post_id) : mixed
Parameters
$comment_post_id : int

Post ID.

Return values
mixed

wp_count_comments()

Remove order notes, webhook delivery logs, and product reviews from wp_count_comments().

public static wp_count_comments(array<string|int, mixed>|object $stats, int $post_id) : object
Parameters
$stats : array<string|int, mixed>|object

Comment stats.

$post_id : int

Post ID.

Tags
since
2.2
Return values
object

is_comment_excluded_from_wp_comment_counts()

Determines whether the given comment should be included in the core WP comment counts that are displayed in the WordPress admin.

private static is_comment_excluded_from_wp_comment_counts(WP_Comment $comment) : bool
Parameters
$comment : WP_Comment

Comment object.

Return values
bool

is_default_comment_type()

Determines if a comment is of the default type.

private static is_default_comment_type(string $comment_type) : bool

Prior to WordPress 5.5, '' was the default comment type. As of 5.5, the default type is 'comment'.

Parameters
$comment_type : string

Comment type.

Tags
since
4.3.0
Return values
bool