Woocommerce adds sections inside tabs
I am making a small plugin in woocommerce . In this plugin, I want to show a section in the admin area just like shipping Options, Flat Rate, Free Shipping etc
the Shipping tab of the woocommerce customization toolbar. First I created a tab from a plugin and inside this tab I need sections. To create the tab I took the code from this link So my code for the plugin is like this
<?php
/**
* Plugin Name: WooCommerce Settings Tab
* Plugin URI: http://www.wordpress.org
* Description: Woocmmerce settings tab.
* Author: Author Name
* Author URI: http://www.wordpress.org
* Version: 1.0
*
*/
class WC_Settings_Tab {
public static function init() {
add_filter( 'woocommerce_settings_tabs_array', __CLASS__ . '::add_settings_tab', 50 );
add_action( 'woocommerce_settings_tabs_settings_tab_demo', __CLASS__ . '::settings_tab' );
add_action( 'woocommerce_update_options_settings_tab_demo', __CLASS__ . '::update_settings' );
add_action( 'woocommerce_sections',__CLASS__ . '::get_sections');
}
public static function add_settings_tab( $settings_tabs ) {
$settings_tabs['settings_tab_demo'] = __( 'Settings Demo Tab', 'woocommerce-settings-tab-demo' );
return $settings_tabs;
}
public static function settings_tab() {
woocommerce_admin_fields( self::get_settings() );
}
public static function update_settings() {
woocommerce_update_options( self::get_settings() );
}
public function get_sections() {
$sections = array(
'' => __( 'Test Link 1', 'woocommerce' ),
'testlink2' => __( 'Test Link 2', 'woocommerce' ),
);
return apply_filters( 'woocommerce_sections', $sections );
}
public static function get_settings() {
$settings = array(
'section_title' => array(
'name' => __( 'Section Title', 'woocommerce-settings-tab-demo' ),
'type' => 'title',
'desc' => '',
'id' => 'wc_settings_tab_demo_section_title'
),
'title' => array(
'name' => __( 'Title', 'woocommerce-settings-tab-demo' ),
'type' => 'text',
'desc' => __( 'This is some helper text', 'woocommerce-settings-tab-demo' ),
'id' => 'wc_settings_tab_demo_title'
),
'description' => array(
'name' => __( 'Description', 'woocommerce-settings-tab-demo' ),
'type' => 'textarea',
'desc' => __( 'This is a paragraph describing the setting. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda.', 'woocommerce-settings-tab-demo' ),
'id' => 'wc_settings_tab_demo_description'
),
'section_end' => array(
'type' => 'sectionend',
'id' => 'wc_settings_tab_demo_section_end'
)
);
return apply_filters( 'wc_settings_tab_demo_settings', $settings );
}
}
WC_Settings_Tab::init();
But it doesn't show sections. So can someone kindly tell me how to add sections in the woocommerce settings tab? Any help and suggestions would be really noticeable.
source to share
I already tried to answer this 2x question. I have sections working in my plugin but as you find it was a beast to figure out. The best example is adding shipping / payment gateways ... if you have access to any of them. And that depends on whether you want to do something that you want to expand in a way that is.
Anyway, because I could barely understand the code I was using, I tried to condense it into 1 file that controlled all sections.
First download the settings file.
add_filter( 'woocommerce_get_settings_pages', 'so_26355697_add_settings_page' );
function so_26355697_add_settings_page( $settings ) {
$settings[] = include( 'wc-plugin-settings.php' );
return $settings;
}
And then in the file wc-plugin-settings.php
(which should return a class object) ...
class WC_Settings_My_Plugin extends WC_Settings_Page {
/**
* Constructor
*/
public function __construct() {
$this->id = 'demo_plugin';
add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_tab' ), 50 );
add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) );
add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );
}
/**
* Add plugin options tab
*
* @return array
*/
public function add_settings_tab( $settings_tabs ) {
$settings_tabs[$this->id] = __( 'Settings Demo Tab', 'woocommerce-settings-tab-demo' );
return $settings_tabs;
}
/**
* Get sections
*
* @return array
*/
public function get_sections() {
$sections = array(
'section-0' => __( 'Plugin Options', 'woocommerce-settings-tab-demo' ),
'section-1' => __( 'Section 1', 'woocommerce-settings-tab-demo' ),
'section 2' => __( 'Section 2', 'woocommerce-settings-tab-demo' ),
);
return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections );
}
/**
* Get sections
*
* @return array
*/
public function get_settings( $section = null ) {
switch( $section ){
case 'section-0' :
$settings = array(
'section_title' => array(
'name' => __( 'Main Section Title', 'woocommerce-settings-tab-demo' ),
'type' => 'title',
'desc' => '',
'id' => 'wc_settings_tab_demo_title_section-1'
),
'title' => array(
'name' => __( 'Main Title', 'woocommerce-settings-tab-demo' ),
'type' => 'text',
'desc' => __( 'This is some helper text', 'woocommerce-settings-tab-demo' ),
'id' => 'wc_settings_tab_demo_title_section-1'
),
'description' => array(
'name' => __( 'Main Description', 'woocommerce-settings-tab-demo' ),
'type' => 'textarea',
'desc' => __( 'This is a paragraph describing the setting. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda.', 'woocommerce-settings-tab-demo' ),
'id' => 'wc_settings_tab_demo_description_section-1'
),
'section_end' => array(
'type' => 'sectionend',
'id' => 'wc_settings_tab_demo_end-section-1'
)
);
break;
case 'section-1':
$settings = array(
'section_title' => array(
'name' => __( 'Section One Title', 'woocommerce-settings-tab-demo' ),
'type' => 'title',
'desc' => '',
'id' => 'wc_settings_tab_demo_section_title_section-2'
),
'title' => array(
'name' => __( 'Section One Title', 'woocommerce-settings-tab-demo' ),
'type' => 'text',
'desc' => __( 'This is some helper text', 'woocommerce-settings-tab-demo' ),
'id' => 'wc_settings_tab_demo_title_section-2'
),
'description' => array(
'name' => __( 'Section One Description', 'woocommerce-settings-tab-demo' ),
'type' => 'textarea',
'desc' => __( 'This is a paragraph describing the setting. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda.', 'woocommerce-settings-tab-demo' ),
'id' => 'wc_settings_tab_demo_description_section-2'
),
'section_end' => array(
'type' => 'sectionend',
'id' => 'wc_settings_tab_demo_section_end_section-2'
)
);
break;
case 'section-2':
$settings = array(
'section_title' => array(
'name' => __( 'Section Two Title', 'woocommerce-settings-tab-demo' ),
'type' => 'title',
'desc' => '',
'id' => 'wc_settings_tab_demo_section_title'
),
'title' => array(
'name' => __( 'Section Two Title', 'woocommerce-settings-tab-demo' ),
'type' => 'text',
'desc' => __( 'This is some helper text', 'woocommerce-settings-tab-demo' ),
'id' => 'wc_settings_tab_demo_title'
),
'description' => array(
'name' => __( 'Section Two Description', 'woocommerce-settings-tab-demo' ),
'type' => 'textarea',
'desc' => __( 'This is a paragraph describing the setting. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda.', 'woocommerce-settings-tab-demo' ),
'id' => 'wc_settings_tab_demo_description'
),
'section_end' => array(
'type' => 'sectionend',
'id' => 'wc_settings_tab_demo_section_end'
)
);
break;
}
return apply_filters( 'wc_settings_tab_demo_settings', $settings, $section );
}
/**
* Output the settings
*/
public function output() {
global $current_section;
$settings = $this->get_settings( $current_section );
WC_Admin_Settings::output_fields( $settings );
}
/**
* Save settings
*/
public function save() {
global $current_section;
$settings = $this->get_settings( $current_section );
WC_Admin_Settings::save_fields( $settings );
}
}
return new WC_Settings_My_Plugin();
As I said, this is not exactly how Wu does it, and even how I do it in my own code. BUT, I think this is the simplest approach. If you need something that can be extended, it won't be appropriate, but it's also much more difficult to write.
source to share
You have to activate the init function also on the hook for playing with WordPress for it to happen at the right time. This trigger is "admin_init".
add_action( 'admin_init', '<your func name>');
So, for your use, replace:
WC_Settings_Tab::init();
from
add_action( 'admin_init', 'WC_Settings_Tab::init');
Another way to use the admin_init trigger is to add the function you provided to the woocommerce-admin-init.php file, which you should place in your theme folder.
The difference is that the first solution works where you want it so you can use it in plugins versus a theme.
source to share