Wednesday, July 6, 2022
HomeWordPress Developmentphp - Add a {custom} WooCommerce settings tab with sections

php – Add a {custom} WooCommerce settings tab with sections


I am making an attempt so as to add a {custom} settings tab to the WooCommerce settings display screen. Mainly I need to obtain an identical factor to the Merchandise settings tab, with the subsections/subtabs:

WooCommerce Products settings tab

I have never been capable of finding any respectable documentation on how to do that however I have been in a position so as to add a {custom} tab utilizing this snippet:

class WC_Settings_Tab_Demo {

    public static operate init() {
        add_filter( 'woocommerce_settings_tabs_array', __CLASS__ . '::add_settings_tab', 50 );
    }

    public static operate add_settings_tab( $settings_tabs ) {
        $settings_tabs['test'] = __( 'Settings Demo Tab', 'woocommerce-settings-tab-demo' );
        return $settings_tabs;
    }
}
WC_Settings_Tab_Demo::init();

Custom WooCommerce settings tab

Based mostly on what I’ve dug up from varied threads/tutorials, I have been making an attempt so as to add the sections/subtabs to the brand new settings tab one thing like this:

// creating a brand new sub tab in API settings
add_filter( 'woocommerce_get_sections_test','add_subtab' );
operate add_subtab( $sections ) {
    $sections['custom_settings'] = __( 'Customized Settings', 'woocommerce-custom-settings-tab' );
    $sections['more_settings'] = __( 'Extra Settings', 'woocommerce-custom-settings-tab' );
    return $sections;
}


// including settings (HTML Type)
add_filter( 'woocommerce_get_settings_test', 'add_subtab_settings', 10, 2 );
operate add_subtab_settings( $settings, $current_section ) {
    // $current_section = (isset($_GET['section']) && !empty($_GET['section']))? $_GET['section']:'';
    if ( $current_section == 'custom_settings' ) {
        $custom_settings = array();
        $custom_settings[] = array( 'title' => __( 'Customized Settings', 'text-domain' ),
                                   'sort' => 'title',
                                   'desc' => __( 'The next choices are used to ...', 'text-domain' ),
                                   'id' => 'custom_settings'
                                  );

        $custom_settings[] = array(
                                    'title'     => __( 'Area 1', 'text-domain' ),
                                    'id'       => 'field_one',
                                    'sort'     => 'textual content',
                                    'default'  => get_option('field_one'),

                                );

        $custom_settings[] = array( 'sort' => 'sectionend', 'id' => 'test-options' );
        return $custom_settings;
    } else {
        // If not, return the usual settings
        return $settings;
    }
}

I have been in a position so as to add new subsections to the Merchandise tab utilizing related code to the above, but it surely is not working for my new {custom} tab. The place am I going incorrect right here?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments