2017-09-22 230 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 slu and和關鍵slu。。我輕微改變了代碼,它也起作用。

這裏是代碼:

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

相關問題