Dissect street address only in saved order (not on checkout page)

< 1 min read

In cases where you need the full address returned by Google for some logic on your checkout page, but do not want the address to look duplicated in your saved orders, then you might want to Dissect the customer’s address after they click the place order button.

The below snippet should be sufficient to achieve this.

PHP
function sl_cc_adjust_customer_addresses($data){
	$data['billing_address_1'] = explode(',', $data['billing_address_1'])[0];
	$data['shipping_address_1'] = explode(',', $data['shipping_address_1'])[0];
	return $data;
}
add_filter('woocommerce_checkout_posted_data', 'sl_cc_adjust_customer_addresses');

NOTE: The following snippet assumes that you have the “Remove Plus Code From Address” option turned On. Also, the logic is simple, we’re breaking up the address returned by Google and taking the first part of it which is usually the street address.

There are cases that might not be true, so you would have to change the snippet to handle this if you live in a country where Google doesn’t return the street address as the first part of an address.