Forum Replies Created

Viewing 1 replies (of 1 total)
  • 1. Update functions.php with the following code:

    // Use the real IP address for customers when creating an order
    add_action('woocommerce_checkout_update_order_meta', 'set_real_customer_ip', 10, 2);

    function set_real_customer_ip($order_id, $data) {
    // Check if Cloudflare provides the real IP address in the HTTP_CF_CONNECTING_IP header
    if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
    $real_ip = sanitize_text_field($_SERVER['HTTP_CF_CONNECTING_IP']);
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    // Fallback to another common proxy header
    $real_ip = sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']);
    } else {
    // If neither header is set, use the default REMOTE_ADDR
    $real_ip = sanitize_text_field($_SERVER['REMOTE_ADDR']);
    }

    // Save the real IP address in the order meta under the '_customer_ip_address' key
    update_post_meta($order_id, '_customer_ip_address', $real_ip);
    }

    1. Hook woocommerce_checkout_update_order_meta:
    ? This action runs whenever a new WooCommerce order is created during the checkout process.

    2. Retrieve the Real IP Address:
    ? First, it checks if Cloudflare’s HTTP_CF_CONNECTING_IP header is available and uses it.
    ? If not, it falls back to HTTP_X_FORWARDED_FOR, which is used by other proxies.
    ? If neither header is present, it defaults to REMOTE_ADDR (the server-reported IP address).

    3. Save the Real IP Address:
    ? The real IP address is stored as order metadata under the key _customer_ip_address.

    • This reply was modified 3 months, 4 weeks ago by yolocalhost.
    • This reply was modified 3 months, 4 weeks ago by yolocalhost.
Viewing 1 replies (of 1 total)