WooCommerce woocommerce_admin_settings_sanitize_option use

Multi tool use
WooCommerce woocommerce_admin_settings_sanitize_option use
I'm currently using the action woocommerce_update_option_X to save the settings of my plugin settings page. However, the following error occurs:
woocommerce_update_option_X action is deprecated, use woocommerce_admin_settings_sanitize_option filter instead
But when I try to find information on that filter, I can't figure out how to use it in my case. In my settings I have a table with fields that has to be saved. The table looks like this:
...
<tbody>
<?php
$max_settings = get_option('max_settings',array());
foreach ( $max_settings as $data ) {
?>
<tr>
<td><input type="text" value="<?php echo esc_attr( $data['var'] ) ?>" name="max_settings[var]" /></td>
<td><input type="text" value="<?php echo esc_attr( $data['productid'] ) ?>" name="max_settings[productid]" /></td>
</tr>
<?php
}
?>
</tbody>
...
The code I'm currently using:
add_action('woocommerce_update_option_max_field_table','update_option_max_field_table');
function update_option_max_field_table($value){
$max_settings_new = $_POST['max_settings'];
$max_settings = array();
// Check if settings table is filled with information
if(!empty($max_settings_new))
{
foreach($max_settings_new as $fields => $max_settings_extra ){
foreach( $max_settings_extra as $key => $settings ){
$max_settings[$key][$fields] = $settings;
}
}
update_option('max_settings',$max_settings);
}
}
Anyone who knows how to correctly use the new filter instead of the old action?
I'm sorry, the table wasn't added in the first post. I've added the table, which is left to reproduce the question. The reason that I'm reacting now is that I didn't had time on my hands before, also the notice was noticed very late by me.
– Max
Jun 28 at 19:11
Sorry Max, but this is not enough for me to be able to test that and to give you the correct working answer code.
– LoicTheAztec
Jun 29 at 7:36
1 Answer
1
If (now) I understand the question properly, you have a WooCommerce setting named woocommerce_max_field_table
, which once the option is saved, you want the max_settings
field/table to be saved as well.
woocommerce_max_field_table
max_settings
So if that's correct, then I believe you don't need to use the woocommerce_admin_settings_sanitize_option
filter. (or you shouldn't use it for the above purpose)
woocommerce_admin_settings_sanitize_option
This is the code I used and tested to work (with the latest WooCommerce release):
add_action( 'woocommerce_settings_saved', 'update_option_max_field_table' );
function update_option_max_field_table(){
if ( ! isset( $_POST['woocommerce_max_field_table'], $_POST['max_settings'] ) ) {
return;
}
// ... the rest of your code here ...
}
So as you can see, the filter I used is woocommerce_settings_saved
. But you could also use woocommerce_update_options
or one of the other filters in WC_Admin_Settings::save()
.
woocommerce_settings_saved
woocommerce_update_options
WC_Admin_Settings::save()
The filter woocommerce_update_option
is also available; however, although it works (as of now), which means it doesn't throw any WooCommerce/WordPress/PHP errors/notices, it's actually also been deprecated and I'd suggest not to using it.
woocommerce_update_option
UPDATE
Fixed the typo — it's max_field_table
and not max_table
.
max_field_table
max_table
And just for reference, this would be how you could use the suggested filter — woocommerce_admin_settings_sanitize_option_$option_name
:
woocommerce_admin_settings_sanitize_option_$option_name
add_action( 'woocommerce_admin_settings_sanitize_option_max_field_table', 'update_option_max_field_table' );
function update_option_max_field_table( $value ){
if ( ! isset( $_POST['woocommerce_max_field_table'], $_POST['max_settings'] ) ) {
return $value;
}
// ... the rest of your code here ...
return $value;
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Your code is not enough complete to see how you should handle that in the filter hook instead… You should include in your question all the related setting fields code. Remember that "questions seeking debugging help must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. I am very surprise that you are reacting only now, as this hook is deprecated since the release of Woocommerce 2.4 (few years ago)…
– LoicTheAztec
Jun 28 at 17:37