Code Element Ws

Add Custom Fields to Existing Woocommerce Settings Page

Use this code snippet to add custom fields to an existing Woocommerce Settings page. You can read more about customizing the Woocommerce Settings Tabs here.

Replace:

woocommerce_get_settings_{tab_name}: with the settings tab you are targeting. ex For the products tab, you would replace with woocommerce_get_settings_products

section_name: with the settings tab section you are targeting. ex For the inventory section under the Products tab, you would simply replace with inventory

You can find your target tab and section names in the URL parameters.

You can grab values from your new custom fields using get_option('wc_my_custom_field'), where wc_my_custom_field is the id you’ve designated for your field.

/*
 * Add Custom Fields to Woocommerce Settings page
 */
add_filter('woocommerce_get_settings_{tab_name}', function ($settings, $current_section) {

	if ($current_section == 'section_name') {

        // Section Start
        $settings[] =  array(
            'name'     => __('My Section Title', 'text-domain'),
            'id'        => 'wc_my_custom_section',
            'type'      => 'title',
        );

        // My Custom Field
        $settings[] = array(
			'name'      => __('My Field', 'text-domain'),
			'desc_tip'  => __('This will add a title to your slider', 'text-domain'),
			'id'        => 'wc_my_custom_field',
			'type'      => 'text',
			'desc'      => __('You can add a field description here.', 'text-domain'),
		);

        // Section End
        $settings[] =  array(
            'type'      => 'sectionend',
            'id'        => 'wc_my_custom_section',
        );
	}
	return $settings;
}, 10, 2 );