Automattic\WooCommerce\Internal\OrderReviews
ItemEligibility::find_existing_review
Look up the customer's review for a specific (product, variation) row on this order.
Method of the class: ItemEligibility{}
No Hooks.
Returns
WP_Comment|null.
Usage
$result = ItemEligibility::find_existing_review( $product_id, $variation_id, $order ): ?WP_Comment;
- $product_id(int) (required)
- Product id.
- $variation_id(int) (required)
- Variation id (0 for simple products).
- $order(WC_Order) (required)
- Order being reviewed.
Changelog
| Since 10.8.0 | Introduced. |
ItemEligibility::find_existing_review() ItemEligibility::find existing review code WC 10.9.4
private static function find_existing_review( int $product_id, int $variation_id, WC_Order $order ): ?WP_Comment {
$email = $order->get_billing_email();
$order_id = (int) $order->get_id();
if ( '' === $email || $order_id <= 0 || $product_id <= 0 ) {
return null;
}
$key = self::cache_key( $order_id, $product_id, $variation_id, $email );
if ( array_key_exists( $key, self::$review_cache ) ) {
return self::$review_cache[ $key ];
}
$comments = get_comments(
array(
'post_id' => $product_id,
'author_email' => $email,
'type' => 'review',
'status' => 'approve',
'include_unapproved' => array( $email ),
'number' => 1,
'orderby' => 'comment_date_gmt',
'order' => 'DESC',
'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- bounded by post_id + author_email.
'relation' => 'AND',
array(
'key' => self::ORDER_META_KEY,
'value' => (string) $order_id,
),
array(
'key' => self::VARIATION_META_KEY,
'value' => (string) $variation_id,
),
),
)
);
if ( ! is_array( $comments ) || empty( $comments ) ) {
self::$review_cache[ $key ] = null;
return null;
}
$first = reset( $comments );
$found = $first instanceof WP_Comment ? $first : null;
self::$review_cache[ $key ] = $found;
return $found;
}