Additional field on checkout for specific payment gateway in Woocommerce

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 06:04:34

问题


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.


回答1:


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.

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



来源:https://stackoverflow.com/questions/52164954/additional-field-on-checkout-for-specific-payment-gateway-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!