2017-10-28 16 views
1

WooCommerce에서 새 제품을 만들 때 추가 정보처럼 사용자 지정 글로벌 제품 탭을 추가하고 싶습니다.Woocommerce 관리 제품 페이지의 사용자 정의 제품 탭 내용 편집

새 탭을 만들 수 있지만 새 제품 페이지를 만들 때 아무것도 업데이트 할 수 없습니다.

디스플레이 페이지에서 볼 수 있지만 제품 수정 페이지를 통해 정보를 추가하는 방법은 알 수 있습니다. 사용자 정의 필드를 사용할 수 있지만 상점 관리자 나 다른 사람들이이 추가 배송 탭을 채울 수 있도록 제품 페이지에 표시하려고합니다.

내 코드

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

당신은 상점 관리자가 사용자 정의 제품 탭에 대한 내용을 추가 할 수 있도록 관리 제품 페이지에 사용자 정의 Metabox 옵션을 추가 할 수 있습니다. 당신이 얻을 것이다 : 그런 다음 자신의 코드가 될 것입니다

// 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 파일에 간다

여기 enter image description here

코드입니다 자식 테마 (또는 테마) 또는 모든 플러그인 파일에서도 사용할 수 있습니다.

테스트를 거쳐 작동합니다.

+0

완벽하게 작동했습니다. –

+0

@jacktriun 새로운 질문을하고 명확하게 세부 사항을 처리하고 사용 된 코드를 작성해야합니다. – LoicTheAztec