pwnz22
pwnz22

Reputation: 469

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

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?

Upvotes: 4

Views: 6085

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 254383

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:

Upvotes: 3

Md Kamruzzaman
Md Kamruzzaman

Reputation: 394

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

Upvotes: 0

Related Questions