2017-07-31 4 views
2

enter image description here投稿の管理領域、または個々の注文ページのカスタムフィールドのプレースホルダに値を表示しようとしています。Woocommerce編集オーダーページのカスタムフィールド値を取得するカスタム代謝物質

私はまだそれが空であってはならないが、私は、更新をクリックすると、そのはまだ空を示し引用符の内側get_post_meta(get_the_id(), 'total-usd', true)value=""を配置しています。

私のコードではどうなりますか?私はあなたの助けに感謝します。

<?php 

function cpmb_add_meta_box() { 

    add_meta_box(
     'woocommerce-order-my-custom', 
     'USD Currency display', 
     'cpmb_display_meta_box', 
     'shop_order',   
     'normal',    
     'core' 
    ); 
} 
add_action('add_meta_boxes', 'cpmb_add_meta_box'); 

function cpmb_display_meta_box($post) { 

    wp_nonce_field(plugin_basename(__POST__), 'cpmb-nonce-field'); 

    $html = '<label id="total-usd" for="total-usd">'; 
    $html .= '<strong>Total USD Currency</strong>'; 
    $html .= '</label><br />'; 
    $html .= '<input type="text" id="total-usd" name="Total USD Currency" value="' . get_post_meta(get_the_id(), 'total-usd', true) . '" placeholder="Enter Total here" />'; 
    echo $html; 
} 

function cpmb_save_meta_box_data($post_id) { 

    if (cpmb_user_can_save($post_id, 'cpmb-nonce-field')){ 
    if (isset($_POST['total-usd']) && 0 < count(strlen(trim($_POST['total-usd'])))) { 

      $total_usd = stripslashes(strip_tags($_POST['total-usd'])); 
      update_post_meta($post_id, 'total-usd', $total_usd); 
     } 
    } 
} 
add_action('save_post', 'cpmb_save_meta_box_data'); 

function cpmb_user_can_save($post_id, $nonce) { 

    $is_autosave = wp_is_post_autosave($post_id); 
    $is_revision = wp_is_post_revision($post_id); 
    $is_valid_nonce = (isset($_POST[ $nonce ]) && wp_verify_nonce($_POST[ $nonce ], plugin_basename(__POST__))); 
    return ! ($is_autosave || $is_revision) && $is_valid_nonce; 
} 

?> 

答えて

0

あなたの主な問題は、あなたの<imput>テキストフィールドに位置している属性name="Total USD Currency"

は、ここに私のコードです。代わりにname="total-usd"にする必要があります。また、html idの属性は一意である必要があります(あなたとあなたのhtmlタグで同じIDを設定することはできません)。すべてのプラグインファイルでも

// Adding the metabox (on the right side) 
add_action('add_meta_boxes', 'cpmb_add_meta_box'); 
function cpmb_add_meta_box() { 

    add_meta_box(
     'woocommerce-order-my-custom', 
     __('USD Currency display'), 
     'cpmb_display_meta_box', 
     'shop_order', 
     'side', 
     'core' 
    ); 
} 
// The metabox content 
function cpmb_display_meta_box($post) { 
    // Get 
    $total_usd = get_post_meta($post->ID, 'total-usd', true); 

    echo '<input type="hidden" name="cpmb_total_usd_nonce" value="' . wp_create_nonce() . '"> 
    <label class="total-usd" for="total-usd"> 
    <strong>Total USD Currency</strong></label><br /> 
    <input type="text" id="total-usd" name="total-usd" value="' . $total_usd . '" placeholder="'. __("Enter Total here").'" />'; 
} 

// Save/Update the meta data 
add_action('save_post', 'cpmb_save_meta_box_data'); 
function cpmb_save_meta_box_data($post_id) { 

    // Only for shop order 
    if ('shop_order' != $_POST[ 'post_type' ]) 
     return $post_id; 

    ## Security verifications. ## 

    // Check if our nonce is set (and our cutom field) 
    if (! isset($_POST[ 'cpmb_total_usd_nonce' ]) && isset($_POST['total-usd'])) 
     return $post_id; 

    $nonce = $_POST[ 'cpmb_total_usd_nonce' ]; 

    // Verify that the nonce is valid. 
    if (! wp_verify_nonce($nonce)) 
     return $post_id; 

    // Checking that is not an autosave 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
     return $post_id; 

    // Check the user’s permissions (for 'shop_manager' and 'administrator' user roles) 
    if (! current_user_can('edit_shop_order', $post_id) && ! current_user_can('edit_shop_orders', $post_id)) 
     return $post_id; 


    ## SETTING AND UPDATING DATA (SECURITY PASSED) ## 

    update_post_meta($post_id, 'total-usd', sanitize_text_field($_POST[ 'total-usd' ])); 
} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルに行くか:

私はあなたのコードを変更しました。

このコードは、WooCommerceバージョン3以降で動作確認されています。

あなたは、この取得します:私は多くのことを感謝し

enter image description here

+0

を、あなたとフォローアップの質問には申し訳ありませんが宜しくお願い致します。表示されたディスプレイがすでに0.75のような数字で分けられていることをどのようにエコーしますか? 100の代わりに133.33と表示されますか? –

+0

@FloranteFerrerしてください、[この回答はあなたの質問に答えていますか?](https://stackoverflow.com/help/someone-answers)...私はそう答えていると思います...あなたの質問?だからあなたは "計算"についてのあなたの質問を明確にする必要があります... – LoicTheAztec

関連する問題