我已尝试使用 woo-commerce 中提供的简单过滤选项来满足您的要求。
我只在该过滤器上添加了一个过滤器和基本登录。过滤器名称为woocommerce_admin_settings_sanitize_option
你需要在这个过滤器上应用你的逻辑,它会根据你的需要做所有事情。
如果出现错误,您可以通过设置空值来阻止保存选项,然后也显示该字段的通知。
我添加了一个自定义属性来进行验证,属性的名称是“custom_validate”
如果有一个空字段,那么下面的属性值会阻止存储数据。
'custom_attributes' => array(
'required' => 'required'
)
将整个代码 sn-p 放入您的 functions.php 并开始跟踪流程。希望你能得到它
// define the woocommerce_admin_settings_sanitize_option callback
function filter_woocommerce_admin_settings_sanitize_option( $value, $option, $raw_value ) {
/*Try to print data as given below to study and decide logic to implement*/
/*
echo "<pre style='margin-left:500px'>";
echo "Value";
print_r($value);
echo "</pre>";
echo "<pre style='margin-left:500px'>";
echo "Option";
print_r($option);
echo "</pre>";
echo "<pre style='margin-left:500px'>";
echo "Raw";
print_r($raw_value);
echo "</pre>";
*/
$error = false;
$message = "";
$custom_validate = $option['custom_validate'];
$required = $option['custom_attributes']['required'];
$name = $option['name'];
if($custom_validate == 'yes'){
if($required == 'required' ){
//Do your validation here and set error notice for the same
$message = "<div id=\"message\" class=\"notice notice-error is-dismissible\"><p>$name is required</p></div>";
$error = true;
}
}
/*If there is an error then empty the value and show the notice for that input*/
add_action( 'admin_notices', function() use($message) {
if(!empty($message))
echo $message;
});
if($error)
return "";
/*If no error then it will return the default value*/
return $value;
};
// add the filter
add_filter( 'woocommerce_admin_settings_sanitize_option', 'filter_woocommerce_admin_settings_sanitize_option', 10, 3 );
add_filter('woocommerce_settings_tabs_array', 'add_my_custom_tab', 50);
function add_my_custom_tab($settings_tabs) {
$settings_tabs['my_custom_tab'] = __('My Custom Tab', 'my-custom-tab');
return $settings_tabs;
}
add_action('woocommerce_settings_tabs_my_custom_tab', 'my_custom_tab');
function my_custom_tab() {
woocommerce_admin_fields(get_custom_settings());
}
add_action('woocommerce_update_options_my_custom_tab', 'update_my_custom_tab_settings');
function update_my_custom_tab_settings() {
woocommerce_update_options(get_custom_settings());
}
function get_custom_settings() {
$settings = array(
'section_title' => array(
'name' => __('Custom Options', 'woocommerce-my-custom-tab'),
'type' => 'title',
'desc' => '',
'id' => 'wc_custom_tab'
),
'example_input' => array(
'name' => __('My Input', 'woocommerce-my-custom-tab'),
'type' => 'text',
'desc' => '',
'id' => 'wc_my_input_1',
'custom_attributes' => array(
'required' => 'required'
)
),
/*You can pass any number of custom attributes and based on those attribures you can develop your logic for validation*/
/*'custom_validate' is a custom attribute that is used to identify whether i need to do custom validation or not*/
'example_input_2' => array(
'name' => __('My Input 2', 'woocommerce-my-custom-tab'),
'type' => 'text',
'desc' => '',
'custom_validate' => 'yes',
'id' => 'wc_my_input_2',
'custom_attributes' => array(
'required' => 'required'
)
),
'section_end' => array(
'type' => 'sectionend',
'id' => 'wc_section_end'
)
);
return apply_filters('wc_my_custom_tab_settings', $settings);
}