2017-10-28 31 views
1

WooCommerceでは、新製品の作成時に追加情報と同様にカスタムグローバルタブを追加したいと思います。Woocommerce管理製品ページのカスタム製品タブコンテンツの編集

新しいタブを作成できますが、新しい製品ページの作成時には何も更新できません。

私は表示ページで見ることができますが、商品編集ページから情報を追加する方法はありますか?私はカスタムフィールドを使うことができると知っていますが、私はショップのマネージャーや他の人がこの追加の配送タブを埋めることを許可するために製品ページにそれを載せようとしています。

私のコードは、ショップ管理者がカスタム製品]タブのコンテンツを追加することができます管理製品ページでカスタムMetaboxを追加することができます

add_filter('woocommerce_product_tabs', 'woo_new_product_tab'); 
function woo_new_product_tab($tabs) { 

    // Adds the new tab 
    $tabs['test_tab'] = array(
     'title'  => __('Shipping', 'woocommerce'), 
     'priority' => 50, 
     'callback' => 'woo_new_product_tab_content' 
    ); 

    return $tabs; 
} 

function woo_new_product_tab_content() { 
    // The new tab content 
    $prod_id = get_the_ID(); 
    echo'<p>'.get_post_meta($prod_id,'Shipping',true).'</p>'; 
} 

http://prntscr.com/h37kqv

+0

グローバルタブでどのような情報を追加したいですか? –

答えて

2

です。あなたは、この取得します:ここで

enter image description here

をコードは次のとおりです。

// Adding a custom Meta container to admin products pages 
add_action('add_meta_boxes', 'create_custom_meta_box'); 
if (! function_exists('create_custom_meta_box')) 
{ 
    function create_custom_meta_box() 
    { 
     add_meta_box(
      'custom_product_shipping_field', 
      __('Custom Shipping Tab information', 'woocommerce'), 
      'add_custom_content_meta_box', 
      'product', 
      'normal', 
      'high' 
     ); 
    } 
} 

// Custom metabox content in admin product pages 
if (! function_exists('add_custom_content_meta_box')) 
{ 
    function add_custom_content_meta_box($post) 
    { 
     $value = get_post_meta($post->ID, '_shipping_tab', true) ? get_post_meta($post->ID, '_shipping_tab', true) : ''; 
     wp_editor($value, 'custom_shipping_tab', array('editor_height' => 100)); 
     echo '<input type="hidden" name="custom_product_field_nonce" value="' . wp_create_nonce() . '">'; 

    } 
} 


//Save the data of the Meta field 
add_action('save_post', 'save_custom_content_meta_box', 10, 1); 
if (! function_exists('save_custom_content_meta_box')) 
{ 

    function save_custom_content_meta_box($post_id) { 

     // We need to verify this with the proper authorization (security stuff). 

     // Check if our nonce is set. 
     if (! isset($_POST[ 'custom_product_field_nonce' ])) { 
      return $post_id; 
     } 
     $nonce = $_REQUEST[ 'custom_product_field_nonce' ]; 

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

     // If this is an autosave, our form has not been submitted, so we don't want to do anything. 
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { 
      return $post_id; 
     } 

     // Check the user's permissions. 
     if ('page' == $_POST[ 'post_type' ]) { 

      if (! current_user_can('edit_page', $post_id)) { 
       return $post_id; 
      } 
     } else { 

      if (! current_user_can('edit_post', $post_id)) { 
       return $post_id; 
      } 
     } 
     // --- Its safe for us to save the data ! --- // 

     // Sanitize user input and update the meta field in the database. 
     update_post_meta($post_id, '_shipping_tab', wp_kses_post($_POST[ 'custom_shipping_tab' ])); 
    } 
} 

そして、自分のコードは次のようになります。

// Add product custom "Shipping" tab 
add_filter('woocommerce_product_tabs', 'woo_new_product_tab'); 
function woo_new_product_tab($tabs) { 

    $tabs['test_tab'] = array(
     'title'  => __('Shipping', 'woocommerce'), 
     'priority' => 50, 
     'callback' => 'shipping_product_tab_content' 
    ); 

    return $tabs; 
} 

// The Shipping tab content 
function shipping_product_tab_content() { 
    // The new tab content 
    $prod_id = get_the_ID(); 
    echo'<div><p>'.get_post_meta(get_the_ID(), '_shipping_tab' ,true).'</p></div>'; 
} 

コードがアクティブなのfunction.phpファイルに行きます子供のテーマ(またはテーマ)、またはすべてのプラグインファイルに保存されます。

テスト済みで動作します。

+0

が完璧に機能しました。 –

+0

@jacktriun詳細を気にしないで新しい質問をしてください。 – LoicTheAztec

関連する問題