2017-09-22 7 views
2

지금은 제품 변형 영역에 1 개의 사용자 정의 선택 메뉴를 추가 할 수 있습니다.WooCommerce 제품 변형 설정 탭에서 여러 개의 사용자 정의 필드 선택

그러나 제품 변형에 추가 선택 메뉴를 추가하고 싶지만 방법을 모르겠습니다.

여기 내 아이 테마 functions.php에 하나 개의 메뉴를 생성하는 내 코드입니다 :

// Add Variation Settings 
add_action('woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3); 
// Save Variation Settings 
add_action('woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2); 
/** 
* Create new fields for variations 
* 
*/ 
function variation_settings_fields($loop, $variation_data, $variation) { 

    // Select 
    woocommerce_wp_select( 
    array( 
     'id'   => '_select[' . $variation->ID . ']', 
     'label'  => __('My Select Field', 'woocommerce'), 
     'description' => __('Choose a value.', 'woocommerce'), 
     'value'  => get_post_meta($variation->ID, '_select', true), 
     'options' => array(
      'one' => __('Option 1', 'woocommerce'), 
      'two' => __('Option 2', 'woocommerce'), 
      'three' => __('Option 3', 'woocommerce') 
      ) 
     ) 
    ); 

} 
/** 
* Save new fields for variations 
* 
*/ 
function save_variation_settings_fields($post_id) { 

    // Select 
    $select = $_POST['_select'][ $post_id ]; 
    if(! empty($select)) { 
     update_post_meta($post_id, '_select', esc_attr($select)); 
    } 

} 

답변

1

제품 변형 탭 설정에서 "선택"유형 2 개 사용자 정의 필드를 추가하려면 매우 간단합니다, 당신은 필드 설정을 복제한다 각기 다른 ID 슬러그와 키 슬러그. 나는 코드를 가볍게 변경했고 너무 효과적이다. 어떤 플러그인 파일도

// Add 2 custom fields in Variation Settings 
add_action('woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3); 
function variation_settings_fields($loop, $variation_data, $variation) { 

    // Select 1 
    woocommerce_wp_select(array(
     'id'   => 'first_custom_field_' . $variation->ID, 
     'label'  => __('My Select Field 1', 'woocommerce'), 
     'value'  => get_post_meta($variation->ID, '_first_custom_field', true), 
     'options' => array(
      '' => __('Please choose a value', 'woocommerce'), 
      'value 1' => __('Option 1', 'woocommerce'), 
      'value 2' => __('Option 2', 'woocommerce'), 
      'value 3' => __('Option 3', 'woocommerce') 
     ) 
    )); 

    // Select 2 
    woocommerce_wp_select(array(
     'id'   => 'second_custom_field_' . $variation->ID, 
     'label'  => __('My Select Field 2', 'woocommerce'), 
     'value'  => get_post_meta($variation->ID, '_second_custom_field', true), 
     'options' => array(
      '' => __('Please choose a value', 'woocommerce'), 
      'value 1' => __('Option 1', 'woocommerce'), 
      'value 2' => __('Option 2', 'woocommerce'), 
      'value 3' => __('Option 3', 'woocommerce') 
     ) 
    )); 
} 
// Save 2 custom fields values in Variation post meta data 
add_action('woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2); 
function save_variation_settings_fields($post_id) { 

    // Select 1 
    $select = $_POST["first_custom_field_$post_id"]; 
    if(! empty($select)) { 
     update_post_meta($post_id, '_first_custom_field', esc_attr($select)); 
    } 

    // Select 2 
    $select = $_POST["second_custom_field_$post_id"]; 
    if(! empty($select)) { 
     update_post_meta($post_id, '_second_custom_field', esc_attr($select)); 
    } 
} 

코드 활성 자식 테마 (또는 테마)의 function.php 파일에 간다 나 : 여기

는 코드입니다.

모든 코드는 Woocommerce 3 이상에서 테스트되었으며 작동합니다. 당신이 얻을 것이다 : 사용하거나 (프론트 엔드에서)이 값을 표시하는

enter image description here


예 :

// You need to get the variation Id from somewhere 
$variation_id = 41; 

// Then you get this custom post meta data 
$value1 = get_post_meta($variation_id, '_first_custom_field', true); 
$value2 = get_post_meta($variation_id, '_second_custom_field', true); 

// And may be display it 
echo '<p>My value 1: ' . $value1 . '</p>'; 
echo '<p>My value 2: ' . $value2 . '</p>'; 
+1

많은 감사합니다. 30 분 안에 책상으로 돌아 가면 시험해 볼 것입니다 :-) – michaelmcgurk

+0

후속 조치입니다. 이 값을 프런트 엔드에 어떻게 표시합니까? – michaelmcgurk

+1

@michaelmcgurk 사용에 관한 내 대답이 업데이트되었습니다. – LoicTheAztec