【问题标题】:WooCommerce custom settings tab validationWooCommerce 自定义设置选项卡验证
【发布时间】:2019-12-17 09:14:30
【问题描述】:

在 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 字段保存到数据库之前对其执行自定义验证,并在需要时抛出错误以告诉用户输入有什么问题?

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您可以通过将 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 ); 
    }
    

    【讨论】:

    • 感谢您的回答!但是,如果我回显错误消息,如何防止设置保存?
    • @OhMad 您可以通过替换 return $value 来执行类似 return $value != "" ? $value: ""; 的操作,这将阻止更新值。
    • 谢谢,其他选项怎么样?我不仅要检查单个字段,还要检查所有选项(不是一个一个,但最后一次检查就足够了,以了解是否允许用户保存设置) .
    • 谢谢。回到我之前的问题,我仍然看到“您的设置已保存”消息。有没有办法防止这种情况发生?
    【解决方案2】:

    我已尝试使用 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);
    

    }

    【讨论】:

      猜你喜欢
      • 2018-09-22
      • 1970-01-01
      • 2016-08-15
      • 2019-06-27
      • 2021-12-12
      • 2018-03-04
      • 2015-10-08
      • 2021-07-24
      • 2018-06-23
      相关资源
      最近更新 更多