首页 文章

在WooCommerce中清理自定义结帐字段数据

提问于
浏览
4

在WooCommerce结帐字段定制文档之后:
Customizing checkout fields using actions and filters

我已经通过 functions.php 向woocommerce结帐页面添加了自定义字段 .

我担心是否必须清理该自定义字段的用户输入?

我认为它不需要清理,因为它已经传入计费字段,如:$ fields ['billing'],这是正确的吗?

如果不是,我如何清理此自定义字段?

创建此自定义字段意味着接受文本字符串(拉丁语)和长度不超过50的整数 .

// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {

//Adding custom text field  
 $fields['billing']['billing_username'] = array(
'type' => 'text',
'label'     => __('Your Username', 'woocommerce'),
'placeholder'   => _x('', 'placeholder', 'woocommerce'),
'required'  => true,
'class'     => array('form-row-first'),
'clear'     => true
 );

 return $fields;
}

1 回答

  • 5

    如果您查看问题中链接的相关官方文档,您就会得到以下代码段:

    /**
     * Update the order meta with field value
     */
    add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
    
    function my_custom_checkout_field_update_order_meta( $order_id ) {
        if ( ! empty( $_POST['my_field_name'] ) ) {
            update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field_name'] ) );
        }
    }
    

    在您的情况下,您不需要它,因为Woocommerce已经处理了地址字段 . 对于自定义特殊字段:答案是肯定的(这不是你的情况)正如你在这段代码中看到的那样,当使用update_post_meta()函数将提交的数据保存到数据库时,他们使用sanitize_text_field()WordPress函数...这仅用于自定义结账字段而不是现有的结帐字段,已经有了自己的流程......

相关问题