2016-06-12 49 views
4

我正在爲Worpdress/WooCommerce創建一個插件。我已經完成了所有的工作,現在我想爲woocommerce API設置添加一個選項選項卡,就像這個instruction tutorial一樣。爲WooCommerce創建插件:將選項選項卡添加到API設置

這是我的代碼:

add_filter('woocommerce_get_sections_api', 'some_function_to_add_tab'); 

function some_function_to_add_tab($sections) { 
    $sections['some_settings'] = __('Some Settings', 'text-domain'); 
    return $sections; 
} 

add_filter('woocommerce_get_settings_api', 'some_tab_settings', 10, 2); 

function some_tab_settings($settings, $current_section) { 
    if ($current_section == 'some_settings') { 
     echo "settings here"; 
    } else { 
     return $settings; 
    } 
} 

但我得到了一些錯誤:

警告:缺少對some_tab_settings(c)中的說法2:\ OpenServer的\域\ WP-dev的\第30行的wp-content \ plugins \ some-plugin \ some_plugin.php

注意:未定義的變量:current_section位於C:\ OpenServer \ domains \ wp-dev \ wp-content \ plugins \ some-plugin \ some_plugin中。 php 31行

涉及:

的add_filter( 'woocommerce_get_settings_api', 'some_tab_settings',10,2); ==>線:30個

功能some_tab_settings($設置,$ current_section){==>線:31

我怎樣才能做到這一點?

+0

@LoicTheAzte c我還有一個問題:)你能解釋我怎樣才能使用教程中的窗體插入數據到數據庫?像我正在寫輸入點擊「保存」按鈕和數據插入到我的表中。我現在如何插入,但不是如何使用array() – pwnz22

回答

2

首先看的WC for WooCommerce API Settings核心源代碼,並this useful only tutorial(2016)我已經在受試者中發現。以下是代碼:

// creating a new sub tab in API settings 
add_filter('woocommerce_get_sections_api','add_subtab'); 
function add_subtab($settings_tabs) { 
    $settings_tabs['custom_settings'] = __('Custom Settings', 'woocommerce-custom-settings-tab'); 
    return $settings_tabs; 
} 


// adding settings (HTML Form) 
add_filter('woocommerce_get_settings_api', 'add_subtab_settings', 10, 2); 
function add_subtab_settings($settings) { 
    $current_section = (isset($_GET['section']) && !empty($_GET['section']))? $_GET['section']:''; 
    if ($current_section == 'custom_settings') { 
     $custom_settings = array(); 
     $custom_settings[] = array('name' => __('Custom Settings', 'text-domain'), 
            'type' => 'title', 
            'desc' => __('The following options are used to ...', 'text-domain'), 
            'id' => 'custom_settings' 
           ); 

     $custom_settings[] = array(
            'name'  => __('Field 1', 'text-domain'), 
            'id'  => 'field_one', 
            'type'  => 'text', 
            'default' => get_option('field_one'), 

           ); 

     $custom_settings[] = array('type' => 'sectionend', 'id' => 'test-options');    
     return $custom_settings; 
    } else { 
     // If not, return the standard settings 
     return $settings; 
    } 
} 

它用於在woocommerce設置選項卡API中創建新標籤和子標籤。第二個功能顯示HTML字段,您可以在其中編輯和保存設置。

由於Daniel Halmagean


更新:使用新的設置:

你會現在只需要使用新創建的設置,就像使用任何其他的WordPress/WooCommerce設置,通過get_option()功能和設置的定義ID(請參閱下面的參考)。

例如,如果您的設置array ID是'custom_settings',您將使用get_option('custom_settings')。有關將設置添加到WooCommerce的更多具體信息,請查看wooThemes: Settings API。可能是你可以使用echo var_dump();功能看到輸出的數據:

$my_data = get_option('custom_settings'); 
echo var_dump($my_data); 

參考:

+0

沒有真正理解如何使用這種形式:(。但我會繼續閱讀documentaion :) thanx – pwnz22

0

請使用「woocommerce_get_settings_products」鉤子嘗試此操作。您無法從此(woocommerce_get_sections_api)掛鉤獲取當前部分。

add_filter('woocommerce_get_settings_products', 'some_tab_settings', 10, 2); 

function some_tab_settings($settings, $current_section) { 
    if ($current_section == 'some_settings') { 
     echo "settings here"; 
    } else { 
     return $settings; 
    } 
} 
+0

創建的窗體但我想把我的設置放在這裏。 http://i.stack.imgur.com/bsjkc.png – pwnz22

+0

請添加此鉤子add_filter('woocommerce_get_sections_products','some_function_to_add_tab'); 函數some_function_to_add_tab($ sections){ $ sections ['some_settings'] = __('Some Settings','text-domain'); return $ sections; } –

+0

此過濾器將選項添加到產品選項卡。但我需要API部分中的設置選項卡。 – pwnz22

相關問題