After a customer has placed an order, I want to update the order meta data. I would like to remove the "/" from the phone number meta data. For example: 23/3245/235 -> 233245235 How should I do it?
I've tried this, which works, but it doesn't update the phone number, it creates a new custom field `
add_action( 'woocommerce_new_order', 'update_order_meta_phone' );
function update_order_meta_phone( $order_id ) {
$order = wc_get_order($order_id);
if ( ! empty( $_POST['billing_phone'] ) ) {
// Replace before saving translating )
$billing_phone = str_replace( array('/'), array(''), $_POST['billing_phone'] );
update_post_meta( $order_id, 'phone', sanitize_text_field( $billing_phone ) );
}
}
`
CodePudding user response:
Developer Resources state this for update_post_meta:
If the meta field for the post does not exist, it will be added and its ID returned.
I assume in your case, this code block causes it:
update_post_meta( $order_id, 'phone', sanitize_text_field( $billing_phone ) )
You can try this code block (or inspect your database table to get right column that refers to the "phone number"):
update_post_meta( $order_id, '_billing_phone', $phone )
Plus:
- You can also use woocommerce_thankyou by following https://www.businessbloomer.com/woocommerce-update-checkout-field-value-after-order/
- You can use the phone number value that is stored in Order object ($order), no need to check & get $_POST['billing_phone'].