4

我正在为 Worpdress/WooCommerce 创建一个插件。我已经完成了所有工作,现在我想在 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;
    }
}

但是我遇到了一些错误:

警告:缺少第 30 行 C:\OpenServer\domains\wp-dev\wp-content\plugins\some-plugin\some_plugin.php 中 some_tab_settings() 的参数 2

注意:未定义变量:第 31 行 C:\OpenServer\domains\wp-dev\wp-content\plugins\some-plugin\some_plugin.php 中的 current_section

相关:

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

功能 some_tab_settings( $settings, $current_section ) { ==>行:31

我怎样才能做到这一点?

4

2 回答 2

3

首先查看 WooCommerce API 设置的 WC 的核心源代码,以及我在该主题上找到的唯一有用的教程(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(参见下面的参考)。

例如,如果您的设置arrayid 是'custom_settings'您将使用get_option( 'custom_settings' ). 有关向 WooCommerce 添加设置的更多具体信息,请查看wooThemes:设置 API。可能您可以使用echo var_dump();函数查看输出的数据:

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

参考:

于 2016-06-13T02:58:14.220 回答
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;
    }
}
于 2016-06-12T16:02:30.827 回答