为 WooCommerce 创建插件:向 API 设置添加选项卡

Creating a plugin for WooCommerce: Adding an option tab to API settings

我正在为 Worpdress/WooCommerce 创建一个插件。我已经完成了所有的工作,现在我想在 woocommerce API 设置中添加一个选项卡,就像这样 instruction tutorial.

这是我的代码:

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;
    }
}

但是我遇到了一些错误:

Warning: Missing argument 2 for some_tab_settings() in C:\OpenServer\domains\wp-dev\wp-content\plugins\some-plugin\some_plugin.php on line 30

Notice: Undefined variable: current_section in C:\OpenServer\domains\wp-dev\wp-content\plugins\some-plugin\some_plugin.php on line 31

相关:

add_filter( 'woocommerce_get_settings_api', 'some_tab_settings', 10, 2 ); ==> Line: 30

function some_tab_settings( $settings, $current_section ) { ==> Line: 31

我怎样才能做到这一点?

请尝试使用 "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;
    }
}

先看WC for WooCommerce API Settings and to 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 );

参考: