• I have added a checkbox (not mandatory) in the checkout page by the following code:

    add_action(‘woocommerce_before_order_notes’,

    ‘my_custom_checkout_field’);

    function my_custom_checkout_field( $checkout ) {

    echo ‘<div id=”my-new-field”><h3>’.__(”).'</h3>’;

    woocommerce_form_field( ‘my_checkbox’, array(
    ‘type’ => ‘checkbox’,
    ‘class’ => array(‘input-checkbox’),
    ‘label’ => __(‘I care about the environment! Do not send me any cutlery and paper napkin with this order’),
    ‘required’ => false,
    ), $checkout->get_value( ‘my_checkbox’ ));

    echo ‘</div>’;
    }

    /**
    * Process the checkout
    **/

    add_action(‘woocommerce_checkout_process’,

    ‘my_custom_checkout_field_process’);

    function my_custom_checkout_field_process() {

    global $woocommerce;

    // Check if set, if its not set add an error.

    }

    /**
    * 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 ($_POST[‘my_checkbox’]) update_post_meta( $order_id, ‘My

    Checkbox’, esc_attr($_POST[‘my_checkbox’]));

    }

    But whether the user is clicking on the checkbox or not I am not able to find. Is there anyway I can find that?

    Thank you for the help.

    https://wordpress.org/plugins/woocommerce/

Viewing 1 replies (of 1 total)
  • Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    That last bit should be:

    function my_custom_checkout_field_update_order_meta( $order_id ) {
    
    update_post_meta( $order_id, 'My Checkbox', ! empty( $_POST['my_checkbox'] ) ? 'Yes' : 'No' );
    
    }

    Then check the ‘custom fields’ box when editing the order for the value.

Viewing 1 replies (of 1 total)
  • The topic ‘Woocommerce checkout issue’ is closed to new replies.