Check if is required a verified product owner before placing a review

This commit is contained in:
Nestor Soriano 2021-03-30 11:07:34 +02:00
parent 0045df2cb0
commit b3d5930279
1 changed files with 33 additions and 0 deletions

View File

@ -51,6 +51,9 @@ class WC_Comments {
// Set comment type.
add_action( 'preprocess_comment', array( __CLASS__, 'update_comment_type' ), 1 );
// Validate product reviews if requires verified owners.
add_action( 'pre_comment_on_post', array( __CLASS__, 'validate_product_review_verified_owners' ) );
}
/**
@ -444,6 +447,36 @@ class WC_Comments {
return $comment_data;
}
/**
* Validate product reviews if requires a verified owner.
*
* @param int $comment_post_id Post ID.
*/
public static function validate_product_review_verified_owners( $comment_post_id ) {
// Only validate if option is enabled.
if ( 'yes' !== get_option( 'woocommerce_review_rating_verification_required' ) ) {
return;
}
// Validate only products.
if ( 'product' !== get_post_type( $comment_post_id ) ) {
return;
}
// Skip if is a verified owner.
if ( wc_customer_bought_product( '', get_current_user_id(), $comment_post_id ) ) {
return;
}
wp_die(
esc_html__( 'Only logged in customers who have purchased this product may leave a review.', 'woocommerce' ),
esc_html__( 'Reviews can only be left by "verified owners"', 'woocommerce' ),
array(
'code' => 403,
)
);
}
/**
* Determines if a comment is of the default type.
*