0
votes

I have a free product in Woocommerce, and used this tutorial from Skyverge to simplify the checkout process: https://www.skyverge.com/blog/how-to-simplify-free-woocommerce-checkout/

The problem is, for all other products we have 3 required fields in the Additional Information tab:

cracha_empresa cracha_primeironome cracha_sobrenome

And we removed "order_notes" all together. To achieve this, we are using the Woocommerce Checkout Field Editor.

When we try to go through the checkout process for this free product, the filter add_filter( 'woocommerce_enable_order_notes_field', '__return_false' ); is removing the additional fields (as expected), but when you try to complete the purchase, I get an error saying that the above fields are REQUIERED to fill out, even though they aren't showing.

From what I guess, I need to filter these fields in an array?

/** * Remove os cupons, notas, e campos que não são necessários para palestrantes. * */ function sv_free_checkout_fields() {

// Se carrinho precisa de pagamento, não fazer nada
if ( WC()->cart && WC()->cart->needs_payment() ) {
    return;
}

// Continuar somente se estamos no checkout
// is_checkout() foi quebrado em WooCommerce 3.2 no ajax, checkar se is_ajax está ativo
if ( function_exists( 'is_checkout' ) && ( is_checkout() || is_ajax() ) ) {

    // Remove cupons para produtos gratuitos
    remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );

    // Remove o campo "Additional Info" nas notas dos pedidos
    add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );

    // Desativa os campos para produtos gratuitos
    function unset_unwanted_checkout_fields( $fields ) {

        // Adiciona aqui o que deseja remover
        // campos: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2
        $billing_keys = array(
            'billing_persontype',
            'billing_cpf',
            'billing_rg',
            'billing_cnpj',
            'billing_company',
            'billing_phone',
            'billing_cellphone',
            'billing_address_1',
            'billing_address_2',
            'billing_neighborhood',
            'billing_city',
            'billing_postcode',
            'billing_country',
            'billing_state',
            'billing_number',
        );


        // unset each of those unwanted fields
        foreach( $billing_keys as $key ) {
            unset( $fields['billing'][ $key ] );
        }


        return $fields;
    }
    add_filter( 'woocommerce_checkout_fields', 'unset_unwanted_checkout_fields' );
}

} add_action( 'wp', 'sv_free_checkout_fields' );

I tried this with no luck:

/** * Remove os cupons, notas, e campos que não são necessários para palestrantes. * */ function sv_free_checkout_fields() {

// Se carrinho precisa de pagamento, não fazer nada
if ( WC()->cart && WC()->cart->needs_payment() ) {
    return;
}

// Continuar somente se estamos no checkout
// is_checkout() foi quebrado em WooCommerce 3.2 no ajax, checkar se is_ajax está ativo
if ( function_exists( 'is_checkout' ) && ( is_checkout() || is_ajax() ) ) {

    // Remove cupons para produtos gratuitos
    remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );

    // Remove o campo "Additional Info" nas notas dos pedidos
    add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );

    // Desativa os campos para produtos gratuitos
    function unset_unwanted_checkout_fields( $fields ) {

        // Adiciona aqui o que deseja remover
        // campos: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2
        $billing_keys = array(
            'billing_persontype',
            'billing_cpf',
            'billing_rg',
            'billing_cnpj',
            'billing_company',
            'billing_phone',
            'billing_cellphone',
            'billing_address_1',
            'billing_address_2',
            'billing_neighborhood',
            'billing_city',
            'billing_postcode',
            'billing_country',
            'billing_state',
            'billing_number',
        );

        $order_keys = array(
            'cracha_empresa',
            'cracha_primeironome',
            'cracha_sobrenome',
        );       

        // unset each of those unwanted fields
        foreach( $billing_keys as $key ) {
            unset( $fields['billing'][ $key ] );
        }

        // unset each of those unwanted fields
        foreach( $order_keys as $key ) {
            unset( $fields['order'][ $key ] );
        }


        return $fields;


    }
    add_filter( 'woocommerce_checkout_fields', 'unset_unwanted_checkout_fields' );
}

} add_action( 'wp', 'sv_free_checkout_fields' );

1

1 Answers

0
votes

Just disable Woocommerce Checkout Field Editor and try this.

You can remove the Additional Information and Order Notes fields in WooCommerce checkout page with 2 filters that you add to your themes functions.php file

// Removes Order Notes Title - Additional Information & Notes Field
add_filter( 'woocommerce_enable_order_notes_field', '__return_false', 9999 );

// Remove Order Notes Field
add_filter( 'woocommerce_checkout_fields' , 'remove_order_notes' );

    enter code here

function remove_order_notes( $fields ) {
     unset($fields['order']['order_comments']);
     return $fields;
}

The first filter woocommerce_enable_order_notes_field is returning false and will not display the ‘Additional Information’ heading and also the order notes field, I have found it needs to be run with a high priority sometimes, that’s why I have added in the ‘9999’.

The second filter woocommerce_checkout_fields is removing the order notes field.