Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

php - Additional field on checkout for specific payment gateway in Woocommerce

I have a custom Woocommerce payment gateway and I need to add additional field on the checkout when the payment is selected.

Basically, when the users click the custom payment gateway a "select" field should appear and they have to choose something from the select field.

I have attached a screenshot to represent better the idea of what I need to do. Unfortunately, I couldn't find any info about that in the Docs.

this is the image

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following code will append to the gateway description in checkout page, a custom text input field (here, in this example to BACS payment gateway):

// BACS payement gateway description: Append custom select field
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-fields" style="padding:10px 0;">';

        woocommerce_form_field( 'field_slug', array(
            'type'          => 'select',
            'label'         => __("Fill in this field", "woocommerce"),
            'class'         => array('form-row-wide'),
            'required'      => false,
            'options'       => array(
                ''          => __("Select something", "woocommerce"),
                'choice-1'  => __("Choice one", "woocommerce"),
                'choice-2'  => __("Choice two", "woocommerce"),
            ),
        ), '');

        echo '<div>';

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

Code goes in functions.php file of your active child theme (or active theme). tested and works.

enter image description here

Related: Validate and save additional checkout field for specific payment gateway in Woocommerce


The complete way, validating the field, saving it as order custom meta data and display it on orders and email notifications:

Save and display specific payment gateway additional field everywhere in Woocommerce


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...