php WooCommerce自定义设置选项卡验证

cgyqldqp  于 2023-10-15  发布在  PHP
关注(0)|答案(3)|浏览(127)

在WooCommerce中使用自定义设置选项卡时,例如:

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'
        ),
        'section_end' => array(
            'type' => 'sectionend',
            'id' => 'wc_section_end'
        )
    );
    return apply_filters('wc_my_custom_tab_settings', $settings);
}

如何在允许example_input字段保存到数据库之前对它执行自定义验证,并在需要时抛出一个错误来告诉用户输入有什么问题?

xv8emn3q

xv8emn3q1#

您可以通过添加html5 required属性到字段中来使字段成为必填字段。

'example_input' => array(
    'name' => __('My Input', 'woocommerce-my-custom-tab'),
    'type' => 'text',
    'desc' => '',
    'custom_attributes' => array( 'required' => 'required' )
    'id' => 'wc_my_input'
),

但是在你的情况下,如果你想在字段更新时显示错误消息,你可以这样做。添加过滤器woocommerce_admin_settings_sanitize_option_<option_name>

// define the woocommerce_admin_settings_sanitize_option_<option_name> callback 
function filter_woocommerce_admin_settings_sanitize_option_wc_my_input( $value, $option, $raw_value ) { 
    add_action( 'admin_notices', function() use($value) {
        if($value == ""){
            echo '<div id="message" class="notice notice-error is-dismissible"><p>Option is required</p></div>';    
        }
    });
    return $value; 
}; 
// add the filter 
add_filter( "woocommerce_admin_settings_sanitize_option_wc_my_input", 'filter_woocommerce_admin_settings_sanitize_option_wc_my_input', 10, 3 );

编辑

你可以像这样遍历所有的选项

$options = ['wc_my_input' => 'My Input', 'other_field' => 'Other Field']; // get all the options here
foreach($options as $option_name => $option_val){
    // define the woocommerce_admin_settings_sanitize_option_$option_name callback 
    add_filter( "woocommerce_admin_settings_sanitize_option_$option_name", function($value, $option, $raw_value) use($option_val) {
        add_action( 'admin_notices', function() use($value) {
            if($value == ""){
                echo "<div id=\"message\" class=\"notice notice-error is-dismissible\"><p>$option_val is required</p></div>";
            }
        });
        return $value;
    }, 10, 3 ); 
}
xbp102n0

xbp102n02#

我试图满足您的要求与简单的过滤器选项可在吴商务。
我只添加了一个过滤器和基本的登录过滤器。过滤器名称为woocommerce_admin_settings_sanitize_option
你需要在这个过滤器上应用你的逻辑,它会根据你的需要做一切事情。
你可以防止保存的选项,如果有一个错误,通过设置空值,然后显示该字段的通知太。
我已经aded一个自定义属性做验证和属性的名称是'自定义_验证'如果有一个空字段,然后下面的属性值防止存储数据。

'custom_attributes' => array( 
    'required' => 'required' 
)

将整个代码片段放在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);

}

2ekbmq32

2ekbmq323#

不确定在woocommerce_admin_settings_sanitize_option_内部打印带有admin_notices钩子的东西是不是一个好主意。
我很确定我们可以使用WC_Admin_Settingsadd_error()静态方法来代替。
同样的例子,但与我的方法:

$options = array( 'wc_my_input' => 'My Input', 'other_field' => 'Other Field' );
foreach( $options as $option_name => $option_val ){
    add_filter( "woocommerce_admin_settings_sanitize_option_$option_name", function( $value ) {
       
        if( ! $value ){
            WC_Admin_Settings::add_error( 'Required field!' );
            return null;
        }

        return $value;
    } ); 
}

关于WooCommerce自定义设置验证的更多信息:https://rudrastyh.com/woocommerce/custom-settings-validation.html

相关问题