Woocommerce订单,更新电话号码元数据,删除特殊字符



客户下订单后,我想更新订单元数据。我想去掉"/"从电话号码元数据。例如:23/3245/235 ->233245235我该怎么做呢?

我试过这个,它工作,但它不更新电话号码,它创建一个新的自定义字段'

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 ) );
}
}

Developer Resources声明update_post_meta:

如果文章的meta字段不存在,它将被添加并返回它的ID。

我假设在你的情况下,是这个代码块导致的:

update_post_meta( $order_id, 'phone', sanitize_text_field( $billing_phone ) )

您可以尝试以下代码块(或检查数据库表以获得引用"电话号码"的正确列):

update_post_meta( $order_id, '_billing_phone', $phone )

+:

  1. 您也可以通过https://www.businessbloomer.com/woocommerce-update-checkout-field-value-after-order/
  2. 使用woocommerce_thankyou
  3. 您可以使用存储在Order对象($ Order)中的电话号码值,无需检查&得到$ _POST [' billing_phone ']。