Skip to content

Instantly share code, notes, and snippets.

@donnamcmaster
Last active June 16, 2025 23:32
Show Gist options
  • Save donnamcmaster/983b056be069d8b5900cd4f607d75d2e to your computer and use it in GitHub Desktop.
Save donnamcmaster/983b056be069d8b5900cd4f607d75d2e to your computer and use it in GitHub Desktop.
Woocommerce orders: trim the customer's first and last names during checkout. (AI generated, not yet tested)
/* Generated by Google's AI in response to search for
"woocommerce trim customer name with trailing space site:stackoverflow.com".
Not yet tested.
StackOverflow had several pieces of the solution but no single post addressed this
specific issue. I'm not a big AI fan so I was both surprised and impressed that
Google AI seems to have merged several pieces of code into a woocommerce hook.
*/
add_action('woocommerce_checkout_update_order_meta', 'trim_customer_name_on_checkout');
function trim_customer_name_on_checkout($order_id) {
$order = wc_get_order($order_id);
if ($order) {
$billing_first_name = $order->get_billing_first_name();
$billing_last_name = $order->get_billing_last_name();
$order->set_billing_first_name(trim($billing_first_name));
$order->set_billing_last_name(trim($billing_last_name));
$order->save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment