2017-09-27 6 views
1

私のカスタムチェックアウトフィールドの文字を置き換える必要があります。WooCommerceチェックアウトフィールド:ポストメタフィールド値に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>'; 
} 

これはカスタムフィールドを更新しようとしてコードの一部です(多分私達はここにstr_replaceを使用することができます)

/* 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でここでそれを行うことができれば大丈夫です。すべてのプラグインファイルでも

## 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ファイルに行くか:あなたはこれを試すことができるように

/* 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 
} 

答えて

1

正しいフックは、woocommerce_checkout_update_order_metaです。

テスト済み作品

+1

ありがとうございます。魅力的に働いた。 – Mostafa

+1

通常の請求電話欄でも使用できますので、 '$ _POST ['billing_phone']'をターゲットにして交換を行いますが、ユーザの電話データをテストし、その値をオーダに保存する必要がありますメタデータ(キーは「_billing_phone」です)。 – LoicTheAztec

関連する問題