• Hey guys,

    I am trying to add 3 check boxes for the checkout page in woocommerce.

    I ve added the following code to my functions.php.

    I can successfully create 2 checkboxes but I am getting an error after

    I create the 3rd one (just copied & pasted the code and modified the nummbers).

    Maybe u guys can help me out ?

    Thanks in advance!

    /**
     * Add checkbox field to the checkout - 1ST BOX
     **/
    add_action('woocommerce_after_checkout_billing_form', 'my_custom_checkout_field');
    
    function my_custom_checkout_field( $checkout ) {
    
        echo '<div id="my-new-field" style="clear:both;"><h3>'.__('AGBs').'</h3>';
    
        woocommerce_form_field( 'my_checkbox', array(
            'type'          => 'checkbox',
            'class'         => array('input-checkbox'),
            'label'         => __('some text'),
            'required'  => true,
            ), $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.
        if (!$_POST['my_checkbox'])
             wc_add_notice('some error message', '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']));
    }
    
    /**
     * Add checkbox field to the checkout - 2ND BOX
     **/
    add_action('woocommerce_after_checkout_billing_form', 'my_custom_checkout_field2');
    
    function my_custom_checkout_field2( $checkout ) {
    
        echo '<div id="my-new-field2">';
    
        woocommerce_form_field( 'my_checkbox2', array(
            'type'          => 'checkbox',
            'class'         => array('input-checkbox'),
            'label'         => __('some text'),
            'required'  => true,
            ), $checkout->get_value( 'my_checkbox2' ));
    
        echo '</div>';
    }
    
    /**
     * Process the checkout
     **/
    add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process2');
    
    function my_custom_checkout_field_process2() {
        global $woocommerce;
    
        // Check if set, if its not set add an error.
        if (!$_POST['my_checkbox2'])
             wc_add_notice('some error message', 'error' );
    }
    
    /**
     * Update the order meta with field value
     **/
    add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta2');
    
    function my_custom_checkout_field_update_order_meta2( $order_id ) {
        if ($_POST['my_checkbox2']) update_post_meta( $order_id, 'My Checkbox2', esc_attr($_POST['my_checkbox2']));
    }
    
    /**
     * Add checkbox field to the checkout - 3rd checkbox
     **/
    add_action('woocommerce_after_checkout_billing_form', 'my_custom_checkout_field3');
    
    function my_custom_checkout_field3( $checkout ) {
    
        echo '<div id="my-new-field3">';
    
        woocommerce_form_field( 'my_checkbox3', array(
            'type'          => 'checkbox',
            'class'         => array('input-checkbox'),
            'label'         => __('some text'),
            'required'  => true,
            ), $checkout->get_value( 'my_checkbox3' ));
    
        echo '</div>';
    }
    
    /**
     * Process the checkout
     **/
    add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process3');
    
    function my_custom_checkout_field_process2() {
        global $woocommerce;
    
        // Check if set, if its not set add an error.
        if (!$_POST['my_checkbox3'])
             wc_add_notice('some error message', 'error' );
    }
    
    /**
     * Update the order meta with field value
     **/
    add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta3');
    
    function my_custom_checkout_field_update_order_meta2( $order_id ) {
        if ($_POST['my_checkbox3']) update_post_meta( $order_id, 'My Checkbox3', esc_attr($_POST['my_checkbox3']));
    }
  • The topic ‘[Plugin: Woocommerce]’ is closed to new replies.