0

Continuation of "Additional field on checkout for specific payment gateway in Woocommerce"

What i have now is showing a select field when BACS payment is selected.

Now i want to add conditional logic if 'option 1' is selected, a next question appears.

You pay (with):

Option 1, Exact amount: €21.10 (total order amount)

Option 2, Text field to enter amount.

I just can't figure out how to do this, and get the extra field in the backend of wordpress.

What i have so far

add_filter( 'woocommerce_gateway_description', 'gateway_bacs_custom_fields', 20, 2 );
function gateway_bacs_custom_fields( $description, $payment_id ){
    //
    if( 'bacs' === $payment_id ){
        ob_start(); // Start buffering

        echo '<div  class="bacs-options" style="padding:10px 0;">';

        woocommerce_form_field( 'bacs_option', array(
            'type'          => 'select',
            'label'         => __("Hoe wil je betalen?", "woocommerce"),
            'class'         => array('form-row-wide'),
            'required'      => true,
            'options'       => array(
                ''          => __("Maak een keuze", "woocommerce"),
                'Pinnen'  => __("Pinnen", "woocommerce"),
                'Contant'  => __("Contant", "woocommerce"),
            ),
        ), '');

        echo '<div>';

        $description .= ob_get_clean(); // Append buffered content
    }
    return $description;
}

// Checkout custom field validation
add_action('woocommerce_checkout_process', 'bacs_option_validation' );
function bacs_option_validation() {
    if ( isset($_POST['payment_method']) && $_POST['payment_method'] === 'bacs'
    && isset($_POST['bacs_option']) && empty($_POST['bacs_option']) ) {
        wc_add_notice( __( 'Please Select an option for "Direct Bank Transfer" payment, please.' ), 'error' );
    }
}

// Checkout custom field save to order meta
add_action('woocommerce_checkout_create_order', 'save_bacs_option_order_meta', 10, 2 );
function save_bacs_option_order_meta( $order, $data ) {
    if ( isset($_POST['bacs_option']) && ! empty($_POST['bacs_option']) ) {
        $order->update_meta_data( '_bacs_option' , esc_attr($_POST['bacs_option']) );
    }
}


// Display custom field in Admin orders, below billing address block
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_bacs_option_near_admin_order_billing_address', 10, 1 );
function display_bacs_option_near_admin_order_billing_address( $order ){
    if( $bacs_option = $order->get_meta('_bacs_option') ) {
        echo '<div class="bacs-option">
        <p><strong>'.__('Betaalmethode').':</strong> ' . $bacs_option . '</p>
        </div>';
    }
}
Mike F
  • 47
  • 6
  • 1
    See: [Show/hide custom field based on a select field with validation in WooCommerce checkout](https://stackoverflow.com/a/67430892/11987538) – 7uc1f3r Jun 17 '21 at 09:43
  • Did you come up with a solution? if not, adjust your question with what you've tried so far. – 7uc1f3r Jun 19 '21 at 18:12
  • Dear 7uc1f3r, I tried to make a combination of your post and mine for 2 days it keeps running into a critical error. I just updated my original post, its without the extra conditions. Also safe to order meta is not working. Your basically my only hope haha :D thank you so far – Mike F Jun 20 '21 at 13:44

0 Answers0