WooCommerce conditional custom checkout fields

前端 未结 1 1732
感动是毒
感动是毒 2020-12-12 03:46

In WooCommerce, I\'m currently trying to add a conditional custom field in the Checkout that shows a checkbox which if is checked displays an input field to insert an italia

1条回答
  •  暖寄归人
    2020-12-12 04:00

    In this answer I can't treat PDF invoice, so you will get here:

    • Solving the problem of conditional "required" field (point 1)
    • Solving the problem of the json error (point 2)
    • Displayed only for italian language (point 3)

    Additionally I have:

    • Revisited all your code and corrected many little errors.
    • Added code to display and edit the backend user profile custom field value "codice fiscale":

    Here is the code:

    add_filter( 'woocommerce_checkout_fields' , 'cbi_cf_chkbox' );
    function cbi_cf_chkbox ( $fields ) {
        if ( ICL_LANGUAGE_CODE !='it' ) return $fields; // Only for Italy
    
        $fields['billing']['checkbox_cf'] = array(
            'type'      => 'checkbox',
            'label'     => __('Voui la fattura? (solo per privati)', 'cbi-custom-parts'),
            'class'     => array('form-row-wide'),
            'clear'     => true
         );
    
        $fields['billing']['cf_in'] = array(
            'label'     => __('Inserisci il codice fiscale', 'cbi-custom-parts'),
            'placeholder'   => _x('RSSMRA85T10A562S', 'placeholder', 'cbi-custom-parts'),
            'class'     => array('form-row-wide'),
            'clear'     => true
         );
    
        return $fields;
    }
    
    add_action( 'woocommerce_after_checkout_form', 'cbi_cf_conditionally_hide_show', 6);
    function cbi_cf_conditionally_hide_show() {
        if ( ICL_LANGUAGE_CODE !='it' ) return; // Only for Italy
        $required = esc_attr__( 'required', 'woocommerce' );
        ?>
        
        user_id, 'codice_fiscale', sanitize_text_field( $_POST['cf_in'] ) );
    
        update_post_meta( $order_id, '_codice_fiscale', sanitize_text_field( $_POST['cf_in'] ) );
    }
    
    // Backend : Display in Order edit pages, after billing address, the custom field value "codice fiscale"
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'cbi_cf_admin_order_data_after_billing_address', 10, 1 );
    function cbi_cf_admin_order_data_after_billing_address( $order ){
        $codice_fiscale = get_post_meta( $order->get_id(), '_codice_fiscale', true );
        if( ! empty( $codice_fiscale ) )
            echo '

    '.__('Codice Fiscale', 'cbi-cf-invoice').': ' . $codice_fiscale . '

    '; } // Backend: Display and edit user profile custom field value "codice fiscale" Only for Italy add_action( 'show_user_profile', 'add_extra_user_codice_fiscale', 1, 1 ); add_action( 'edit_user_profile', 'add_extra_user_codice_fiscale', 1, 1 ); function add_extra_user_codice_fiscale( $user ) { //if( get_user_meta( $user->ID, 'billing_country', true ) != 'IT' ) return; // Only for Italy $codice_fiscale = get_user_meta( $user->ID, 'codice_fiscale', true ); if( empty( $codice_fiscale ) ) $codice_fiscale = ''; ?>


    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    All code is tested on Woocommerce 3+ and works.

    0 讨论(0)
提交回复
热议问题