Privacy PHP Intermediate Requires WooCommerce

Anonymise Customer IP Addresses on New Orders

Store only a shortened IP address with each new WooCommerce order, like 203.0.113.0 instead of 203.0.113.42.

Tested on WordPress 7.1 PHP 8.3 Updated
functions.php 17 lines
add_action( 'woocommerce_new_order', function ( int $order_id, $order = null ): void {
	$order = $order instanceof WC_Order ? $order : wc_get_order( $order_id );
	if ( ! $order ) {
		return;
	}

	$ip = $order->get_customer_ip_address();
	if ( '' === $ip ) {
		return;
	}

	$anonymous = wp_privacy_anonymize_ip( $ip );
	if ( $anonymous !== $ip ) {
		$order->set_customer_ip_address( $anonymous );
		$order->save();
	}
}, 10, 2 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

WooCommerce saves the customer’s full IP address with every order and keeps it indefinitely. A shortened address is still useful for spotting the country or obvious fraud, while being much less personal.

How it works

woocommerce_new_order runs once when an order is created, for both the classic and block checkouts. The snippet reads the IP address, shortens it with wp_privacy_anonymize_ip() (the last part of an IPv4 address becomes 0, and IPv6 addresses are cut to the first 3 groups) and saves the order.

Works with both order storage options, HPOS and posts.

Things to watch

  • Payment gateways like Stripe receive the visitor’s real IP address directly for fraud checks, so this doesn’t weaken them.
  • Fraud plugins that read the IP from the order later will only see the shortened version.
  • Existing orders aren’t changed.

How to test it

Place a test order and open it in WooCommerce → Orders. The customer IP should end in .0.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only