【问题标题】:Creating a plugin for WooCommerce: Adding an option tab to API settings为 WooCommerce 创建插件:向 API 设置添加选项选项卡
【发布时间】:2016-10-13 01:35:43
【问题描述】:

我正在为 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;
    }
}

但我遇到了一些错误:

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

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

相关:

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

function some_tab_settings( $settings, $current_section ) { ==> 第 31 行

我怎样才能做到这一点?

【问题讨论】:

  • @LoicTheAztec 我还有一个问题:) 你能解释一下如何使用教程中的表格将数据插入数据库吗?就像我在输入中写一些东西,点击“保存”按钮并将数据插入到我的表中。我现在如何插入,而不是如何使用由array()创建的表单

标签: php wordpress woocommerce hook-woocommerce wordpress-hook


【解决方案1】:

首先查看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 );

参考:

【讨论】:

  • 真的不明白如何使用这个表格:(。但我会继续阅读文档:)thanx
【解决方案2】:

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

【讨论】:

  • 请添加这个钩子 add_filter( 'woocommerce_get_sections_products', 'some_function_to_add_tab' ); function some_function_to_add_tab( $sections ) { $sections['some_settings'] = __( 'Some Settings', 'text-domain' );返回 $sections; }
  • 此过滤器将选项添加到产品选项卡。但我需要 API 部分中的设置选项卡。
  • 抱歉有错。请在没有当前部分的情况下使用它。 woocommerce 默认管理此当前部分。 add_filter('woocommerce_get_settings_api', 'some_tab_settings', 10, 2); function some_tab_settings( $settings ) { echo "settings here";返回$设置; }
  • function syncretailcrm_settings( $settings) { if ($_GET['section'] == 'some_settings') { adminMenu(); } else { return $settings; } } 我做了以下操作,现在进入设置选项卡时出现此错误:Warning: Invalid argument supplied for foreach() in C:\OpenServer\domains\wp-dev\wp-content\plugins\woocommerce\includes\admin\class-wc-admin-settings.php on line 222
  • 你想用syncretailcrm_settings这个函数做什么。
猜你喜欢
  • 1970-01-01
  • 2016-08-15
  • 1970-01-01
  • 2022-12-11
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多