skip to Main Content

I’ve got this custom code working to add a couple of radio buttons into the checkout:

add_action( 'woocommerce_review_order_before_payment', 'display_extra_fields_after_billing_address' , 10, 1 );
function display_extra_fields_after_billing_address () { ?>
    <h3 class="delivery-options-heading">Delivery Options <sup>*</sup></h3>
    <div class="delivery-options">
        <p><input type="radio" name="delivery_option" value="Have the courier leave a card." required /> Have the courier leave a card.</p>
        <p><input type="radio" name="delivery_option" value="Leave the consignment without a signature." required /> Leave the consignment without a signature.</p>
    </div>
<?php 
}

add_action( 'woocommerce_checkout_update_order_meta', 'add_delivery_option_to_order' , 10, 1);
function add_delivery_option_to_order ( $order_id ) {

    if ( isset( $_POST ['delivery_option'] ) &&  '' != $_POST ['delivery_option'] ) {
        add_post_meta( $order_id, '_delivery_option',  sanitize_text_field( $_POST ['delivery_option'] ) );
    }
}


add_filter( 'woocommerce_email_order_meta_fields', 'add_delivery_option_to_emails' , 10, 3 );

function add_delivery_option_to_emails ( $fields, $sent_to_admin, $order ) {
    if( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">=" ) ) {            
        $order_id = $order->get_id();
    } else {
        $order_id = $order->id;
    }

    $delivery_option = get_post_meta( $order_id, '_delivery_option', true );

    if ( '' != $delivery_option ) {
    $fields[ 'Delivery Date' ] = array(
        'label' => __( 'Delivery Option', 'delivery_option' ),
        'value' => $delivery_option,
    );
    }
    return $fields;
}

Every part of this works fine except for the required attributes on the radio buttons, letting the user checkout without making a choice, which is not ideal. For some reason the required attributes are being ignored.

How can I force the user to select one of the radio buttons before checking out?

2

Answers


  1. Chosen as BEST ANSWER

    This was the solution, slightly different to what Krunal Prajapati suggested. The only difference is the second line:

    function action_woocommerce_after_checkout_validation( $data, $errors ) { 
        if ( empty( $_POST['delivery_option'] ) ) :
            $errors->add( 'required-field', __( 'No delivery option has been selected. Please choose delivery option.', 'woocommerce' ) );
        endif; //Endif
    }
    // add the action 
    add_action( 'woocommerce_after_checkout_validation', 'action_woocommerce_after_checkout_validation', 10, 2 );
    

  2. Please add this validation hook along with your code that will validate your option on checkout

    function action_woocommerce_after_checkout_validation( $data, $errors ) { 
        if( !isset( $data['delivery_option'] ) || empty( $data['delivery_option'] ) ) :
            $errors->add( 'required-field', __( 'No delivery option has been selected. Please choose delivery option.', 'woocommerce' ) );
        endif; //Endif
    }
    // add the action 
    add_action( 'woocommerce_after_checkout_validation', 'action_woocommerce_after_checkout_validation', 10, 2 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search