2017-06-04 187 views
0

我試圖在單一產品和變量產品(自定義字段價格應根據所選變量選項更改)中創建自定義字段中的woo-commerce(對插件不感興趣),因爲該客戶端應該能夠輸入價格顯示在1 st圖片和客戶可以檢查選項。Wordpress Woocommerce自定義字段

我的要求

// Display Fields 
add_action('woocommerce_product_options_general_product_data', 'woocom_general_product_data_custom_field'); 

function woocom_general_product_data_custom_field() { 
    // Create a custom text field 

    // Text Field 
    woocommerce_wp_text_input( 
    array( 
     'id' => '_text_field', 
     'label' => __('Enter your choose', 'woocommerce'), 
     'placeholder' => 'Custom text field', 
     'desc_tip' => 'true', 
     'description' => __('Enter the custom value here.', 'woocommerce') 
    ) 
); 

    // Number Field 
    woocommerce_wp_text_input( 
    array( 
     'id' => '_number_field', 
     'label' => __('Enter your number', 'woocommerce'), 
     'placeholder' => '', 
     'description' => __('Enter the custom value here.', 'woocommerce'), 
     'type' => 'number', 
     'custom_attributes' => array(
     'step' => 'any', 
     'min' => '15' 
    ) 
    ) 
); 

    // Checkbox 
    woocommerce_wp_checkbox( 
    array( 
     'id' => '_checkbox', 
     'label' => __('Select', 'woocommerce'), 
     'description' => __('Check me!', 'woocommerce') 
    ) 
); 

    // Select 
    woocommerce_wp_select( 
    array( 
     'id' => '_select', 
     'label' => __('option', 'woocommerce'), 
     'options' => array(
     '1' => __('Custom Option 1', 'woocommerce'), 
     '2' => __('Custom Option 2', 'woocommerce'), 
     '3' => __('Custom Option 3', 'woocommerce') 
    ) 
    ) 
); 

    // Textarea 
    woocommerce_wp_textarea_input( 
    array( 
     'id' => '_textarea', 
     'label' => __('Description', 'woocommerce'), 
     'placeholder' => '', 
     'description' => __('Enter the custom value here.', 'woocommerce') 
    ) 
); 

} 

// Hook to save the data value from the custom fields 
add_action('woocommerce_process_product_meta', 'woocom_save_general_proddata_custom_field'); 

add_action('woocommerce_single_product_summary', 'woocommerce_template_top_category_desc', 1); 
    function woocommerce_template_top_category_desc(){ 
$terms = get_the_terms($post->ID, 'wc-attibute-class'); 
if (!empty($terms)) { 
     $term = array_pop($terms); 
       $text= get_field('txt-field', $term); 
       if (!empty($text)) { 
       echo $text; 
       } 
} 
     } 

/** Hook callback function to save custom fields information */ 
function woocom_save_general_proddata_custom_field($post_id) { 
    // Save Text Field 
    $text_field = $_POST['_text_field']; 
    if(! empty($text_field)) { 
    update_post_meta($post_id, '_text_field', esc_attr($text_field)); 
    } 

    // Save Number Field 
    $number_field = $_POST['_number_field']; 
    if(! empty($number_field)) { 
    update_post_meta($post_id, '_number_field', esc_attr($number_field)); 
    } 
    // Save Textarea 
    $textarea = $_POST['_textarea']; 
    if(! empty($textarea)) { 
    update_post_meta($post_id, '_textarea', esc_html($textarea)); 
    } 

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

    // Save Checkbox 
    $checkbox = isset($_POST['_checkbox']) ? 'yes' : 'no'; 
    update_post_meta($post_id, '_checkbox', $checkbox); 

    // Save Hidden field 
    $hidden = $_POST['_hidden_field']; 
    if(! empty($hidden)) { 
    update_post_meta($post_id, '_hidden_field', esc_attr($hidden)); 
    } 
} 

我已經嘗試了一些woocommerce自定義插件,但它並沒有解決我的要求,我有25個以上的自定義字段創建和插件似乎是很漫長的過程,我工作

我的輸出是在畫面2這是我從編碼不是我的要求得到了

我的輸出

回答

0

我知道你對插件不感興趣,但我強烈推薦ACF插件用於自定義字段。檢查一下,它會爲你節省很多時間。

+0

正如我已經提到,我必須創建超過25個自定義字段超過100個產品,所以它會成果豐碩.... – syner