4

I am creating a plugin for Worpdress/WooCommerce. I have done all the stuff and now I want to add an option tab to woocommerce API settings, like in this instruction tutorial.

This is my code:

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

But I am getting some errors:

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

Related to:

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

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

How can I achieve this?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
pwnz22
  • 469
  • 2
  • 9
  • 19
  • @LoicTheAztec I have one more question :) Can u explain how can i use the form in tutorial to insert data to dabase? like am writing something in input clicking "save" button and data inserting to my table. I now how to insert, but not how to use the form that created by array () – pwnz22 Jun 13 '16 at 05:58

2 Answers2

3

Looking first at the core source code of WC for WooCommerce API Settings and to this useful only tutorial (2016) I have found on the subject. Here is the code:

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

It's used to create a new tab and a sub tab in woocommerce settings tab API. the second function displays HTML fields where you will be able to edit and save the settings.

Thanks to Daniel Halmagean


Update: Using the New Settings:

You would now just use your newly created settings like you would any other WordPress / WooCommerce setting, through the get_option() function and the defined ID of the setting (see reference below).

For example if your settings array id is 'custom_settings' you will use get_option( 'custom_settings' ). For more specific information on adding settings to WooCommerce, check out the wooThemes: Settings API. May be you can use echo var_dump(); function to see the outputted data:

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

Reference:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Didnt really understand how to use this form :(. But i will continue to read documentaion :) thanx – pwnz22 Jun 13 '16 at 08:32
0

Please try this with "woocommerce_get_settings_products" hook. You can not get current section from this( woocommerce_get_sections_api ) hook.

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;
    }
}
Md Kamruzzaman
  • 346
  • 1
  • 8
  • Please add this hook 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' ); return $sections; } – Md Kamruzzaman Jun 12 '16 at 16:29
  • This filter adding option to products tab. But i need a settings tab in the API section. – pwnz22 Jun 12 '16 at 16:34
  • Sorry for mistake. please use this without current section. woocommerce manage this current section by default. add_filter( 'woocommerce_get_settings_api', 'some_tab_settings', 10, 2 ); function some_tab_settings( $settings ) { echo "settings here"; return $settings; } – Md Kamruzzaman Jun 12 '16 at 16:47
  • `function syncretailcrm_settings( $settings) { if ($_GET['section'] == 'some_settings') { adminMenu(); } else { return $settings; } }` I did the following and now getting this error when going to settings tab: `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` – pwnz22 Jun 12 '16 at 17:24
  • what do you want to use syncretailcrm_settings this function. – Md Kamruzzaman Jun 12 '16 at 18:09
  • Now im having this `add_filter( 'woocommerce_get_sections_api', 'sync_retail_crm' ); function sync_retail_crm( $sections ) { $sections['syncretailcrm'] = __( 'Sync RetailCRM', 'text-domain' ); return $sections; } add_filter( 'woocommerce_get_settings_api', 'syncretailcrm_settings', 10, 2 ); function syncretailcrm_settings( $settings ) { adminMenu(); return $settings; } ` and i want to go to my settings by clicking my section but not the settings button which going first – pwnz22 Jun 12 '16 at 18:35