<?php
/**
* Plugin Name: Limit Points for First Review Only
* Description: Adjusts WooCommerce Points and Rewards to only award points for the first product review by a customer, with nonce verification for security.
* Author: Your Name
* Version: 1.0
*/
// Add a nonce field to the product review form
add_action('comment_form_logged_in_after', 'add_review_nonce_to_form');
add_action('comment_form_after_fields', 'add_review_nonce_to_form');
function add_review_nonce_to_form() {
if (get_post_type() === 'product') {
wp_nonce_field('add_product_review_nonce', 'product_review_nonce');
}
}
// Hook into the 'comment_post' action which is triggered when a new comment is created.
add_action('comment_post', 'limit_review_points_to_first', 10, 3);
function limit_review_points_to_first($comment_ID, $comment_approved, $commentdata) {
if ('product' !== get_post_type($commentdata['comment_post_ID'])) {
return;
}
// Verify the nonce. If it's not set or is invalid, bail out.
if (!isset($_POST['product_review_nonce']) || !wp_verify_nonce($_POST['product_review_nonce'], 'add_product_review_nonce')) {
wp_die('Security check failed.');
}
if (empty($commentdata['user_id'])) {
return;
}
// Check if user has already made a review and received points.
$user_id = $commentdata['user_id'];
$has_reviewed = get_user_meta($user_id, '_has_reviewed', true);
if ('1' === $has_reviewed) {
// User has already made a review and should not receive points again.
remove_action('woocommerce_review_order_before_cart_contents', ['WC_Points_Rewards_Product_Review', 'maybe_add_review_points']);
} else {
// Mark that the user has reviewed a product.
update_user_meta($user_id, '_has_reviewed', '1');
}
}
Limit Points for First Review Only
In