Required custom WooCommerce checkout fields don't validate entered value

后端 未结 3 856
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-27 03:08

I\'m adding WooCommerce custom checkout fields in a Storefront child theme functions.php file.
They have a \"required\" attribute.
The ai

3条回答
  •  时光说笑
    2021-01-27 04:06

    You try this code. Tested Ok

    add_action( 'woocommerce_billing_fields', 'my_custom_checkout_fields' );
    
    function my_custom_checkout_fields( $fields ) {
    
       $fields['billing_developer_name'] = array(
        'label'       => __('Developer name', 'woocommerce'),            
        'placeholder' => _x('Developer name', 'placeholder', 'woocommerce'),  
        'required'    => TRUE,             
        'clear'       => false,             
        'type'        => 'text',              
        'class'       => array('my-css')    
        );
    
     return $fields;
    
    }
    

    You can arrange it with this snippet

    add_filter("woocommerce_checkout_fields", "order_fields");
    
    function order_fields($fields) {
    
        $order = array(
            "billing_developer_name",
            "billing_first_name", 
            "billing_last_name", 
            "billing_company", 
            "billing_address_1", 
            "billing_address_2", 
            "billing_postcode", 
            "billing_country", 
            "billing_email", 
            "billing_phone"
    
        );
        foreach($order as $field)
        {
            $ordered_fields[$field] = $fields["billing"][$field];
        }
    
        $fields["billing"] = $ordered_fields;
        return $fields;
    
    }
    

    See screenshot

提交回复
热议问题