Automattic\WooCommerce\Internal\OrderReviews
SubmissionHandler::maybe_mark_order_complete
Set the completed-at meta when every eligible item has a review by this customer (approved or pending moderation), whether posted in this submission or an earlier one. Spam/trash comments are excluded.
Method of the class: SubmissionHandler{}
Hooks from the method
Returns
null. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->maybe_mark_order_complete( $order ): void;
- $order(WC_Order) (required)
- Order being reviewed.
SubmissionHandler::maybe_mark_order_complete() SubmissionHandler::maybe mark order complete code WC 10.8.1
private function maybe_mark_order_complete( WC_Order $order ): void {
// Recording the moment the order first became fully reviewed; never overwrite.
if ( $order->get_meta( self::COMPLETED_META_KEY ) ) {
return;
}
$customer_email = $order->get_billing_email();
if ( '' === $customer_email ) {
return;
}
// Build the same eligible-row set the page uses, then count required
// reviews per parent product. Same product appearing on N rows needs
// N reviews, not 1.
// phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment -- documented at the page-template invocation site.
$eligible_items = (array) apply_filters( 'woocommerce_review_order_eligible_items', $order->get_items(), $order );
$required_reviews = array();
foreach ( $eligible_items as $item ) {
if ( ! $item instanceof \WC_Order_Item_Product ) {
continue;
}
$product_id = (int) $item->get_product_id();
if ( $product_id > 0 ) {
$required_reviews[ $product_id ] = ( $required_reviews[ $product_id ] ?? 0 ) + 1;
}
}
if ( empty( $required_reviews ) ) {
return;
}
// Single grouped lookup, fetching the comment objects directly so we
// can read comment_post_ID without a follow-up query per row. Limit
// to approved + pending-moderation so spam/trash never count as
// completion. number=>0 disables the default 20-row cap so this still
// works for orders with many reviewable items.
$comments = get_comments(
array(
'post__in' => array_keys( $required_reviews ),
'author_email' => $customer_email,
'type' => 'review',
'status' => array( 'approve', 'hold' ),
'number' => 0,
)
);
if ( ! is_array( $comments ) || empty( $comments ) ) {
return;
}
$review_counts = array();
foreach ( $comments as $comment ) {
if ( $comment instanceof \WP_Comment ) {
$post_id = (int) $comment->comment_post_ID;
$review_counts[ $post_id ] = ( $review_counts[ $post_id ] ?? 0 ) + 1;
}
}
foreach ( $required_reviews as $product_id => $required ) {
if ( ( $review_counts[ $product_id ] ?? 0 ) < $required ) {
return;
}
}
$order->update_meta_data( self::COMPLETED_META_KEY, (string) time() );
$order->save();
}