Last active
June 16, 2025 23:32
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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