我需要在自定义结帐字段中替换一些字符。
这是我的自定义结帐字段的整个代码,(也许我们可以在这里使用str_replace)
/* Add the field to the checkout */
add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field">';
woocommerce_form_field( 'phone_sabet', array(
'type' => 'tel',
'required' => true,
'clear' => true,
'class' => array('my-field-class form-row-first'),
'label' => __(''),
'placeholder' => __(''),
'description' => '',
), $checkout->get_value(('phone_sabet')));
echo '</div>';
}
当自定义字段要更新时,这是代码的一部分
/* Update the order meta with field value */
add_action( 'woocommerce_checkout_update_order_meta','my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['phone_sabet'] ) ) {
update_post_meta( $order_id, 'Phone', sanitize_text_field( $_POST['phone_sabet'] ) );
}
}
我厌倦了使用str_replace并将其更改为下面但没有运气。
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['phone_sabet'] ) ) {
update_post_meta( $order_id, 'Phone', sanitize_text_field( $_POST['phone_sabet'] ) );
$getMeta = get_post_meta( get_the_ID(), 'Phone', true);
$newMeta = str_replace(array('۱'), '1', $getMeta);
update_post_meta(get_the_ID(), 'Phone', $newMeta);
}
}
这是结帐字段进行处理时的一部分。如果我们可以在这里用str_replace完成它,那没关系。
/* Process the checkout */
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
if ( $_POST['phone_sabet'] )
// do something
}
答案 0 :(得分:1)
正确的钩子是 woocommerce_checkout_update_order_meta
,所以你可以试试这个:
## Save the order meta with custom field value
add_action( 'woocommerce_checkout_update_order_meta', 'custom_update_order_meta' );
function custom_update_order_meta( $order_id ) {
if ( ! empty( $_POST['phone_sabet'] ) ) {
// Replace before saving translating )
$phone_sabet = str_replace( array('۱'), array('1'), $_POST['phone_sabet'] );
update_post_meta( $order_id, 'phone', sanitize_text_field( $phone_sabet ) );
}
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
经过测试和工作