Reputation: 823
I'm trying to add two different sections to the WooCommerce Admin Products tab.
The following code works only when I don't use the elseif()
, if I add this, I get the following error:
Warning: Invalid argument supplied for foreach() in /wp-content/plugins/woocommerce/includes/admin/class-wc-admin-settings.php on line 197
The code:
// Create a section on the Products tab
add_filter('woocommerce_get_sections_products', 'myplugin_add_section');
function myplugin_add_section( $sections ) {
$sections['myplugin'] = __('My Plugin page', 'myplugin');
$sections['mypluginlog'] = __('My plugin Log', 'myplugin');
return $sections;
}
// Add Settings
add_filter('woocommerce_get_settings_products', 'myplugin_all_settings', 10, 2);
function myplugin_all_settings( $settings, $current_section ) {
if ($current_section == 'myplugin') {
// do something
}
elseif($current_section == 'mypluginlog') {
// do something else
}
else {
return $settings;
}
Upvotes: 0
Views: 299
Reputation: 3562
you need to return an array if the if statement
is true as woocommerce_get_settings_
expecting an array as return.
so your code should be like this:
// Create a section on the Products tab
add_filter('woocommerce_get_sections_products', 'myplugin_add_section');
function myplugin_add_section($sections)
{
$sections['myplugin'] = __('My Plugin page', 'myplugin');
$sections['mypluginlog'] = __('My plugin Log', 'myplugin');
return $sections;
}
add_filter('woocommerce_get_settings_products', 'myplugin_all_settings', 10, 2);
function myplugin_all_settings($settings, $current_section)
{
if ($current_section == 'myplugin') {
$my_settings = array();
return $my_settings;
} elseif ($current_section == 'mypluginlog') {
$my_settings = array();
return $my_settings;
} else {
return $settings;
}
}
Upvotes: 2