duanfen9090 2017-10-13 10:13 采纳率: 100%
浏览 85
已采纳

在woocommerce中添加带有默认值的自定义隐藏结帐字段

How to add custom field in checkout Woocommerce with hidden type and default value?

please check below my code:

function pord_checkout_fields( $fields ) {
    $fields['billing']['quickbook'] = array(
        'type' => 'hidden',
        'label'     => __('Purchase Order Number', 'woocommerce'),
        'placeholder'  => _x('Purchase Order Number', 'placeholder', 'woocommerce'),
        'required'  => false,
        'class'     => array('form-row-wide'),
        'clear'     => true
     );

    return $fields;
}
add_filter('woocommerce_checkout_fields','pord_checkout_fields');
  • 写回答

2条回答 默认 最新

  • duandingqi9442 2017-10-13 11:45
    关注

    Hidden type doesn't exist buy default for woocommerce form fields… But you can create it.

    • The first function will create that hidden field type.
    • The 2nd function will add this hidden custom field (where you will set the value)
    • The third function will display this value in order edit page

    Here is the code:

    // Create hidden checkout field type
    add_filter( 'woocommerce_form_field_hidden', 'create_checkout_hidden_field_type', 5, 4 );
    function create_checkout_hidden_field_type( $field, $key, $args, $value ){
        return '<input type="hidden" name="'.esc_attr($key).'" id="'.esc_attr($args['id']).'" value="'.esc_attr($args['default']).'" />';
    }
    
    // Add custom hidden billing checkout field
    add_filter( 'woocommerce_checkout_fields', 'custom_billing_fields' );
    function custom_billing_fields( $fields ){
    
        ## HERE set the value (for this hidden checkout field)
        $value = "The value";
    
        $fields['billing']['billing_quickbook'] = array(
            'type' => 'hidden',
            'label'     => __('Purchase Order Number', 'woocommerce'),
            'placeholder'  => _x('Purchase Order Number', 'placeholder', 'woocommerce'),
            'required'  => false,
            'class'     => array('form-row-wide'),
            'clear'     => true,
            'default'   => $value, // The custom field value
        );
        return $fields;
    }
    
    // Display the field value on the admin order edit page (after billing address)
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_field_in_admin_order_meta', 10, 1 );
    function display_custom_field_in_admin_order_meta($order){
        echo '<p><strong>'.__('Quickbook').':</strong> ' . get_post_meta( $order->get_id(), '_billing_quickbook', true ) . '</p>';
    }
    

    The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    USAGE (RETRIEVING THE VALUE):

    To get the value from the Order ID $order_id, you will use (if needed):

    $value = get_post_meta( $order_id, '_billing_quickbook', true );
    

    This code is tested and works in WooCommerce 3+.


    Official developer documentation: Customizing checkout fields using actions and filters

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 准备学习小程序搭建,谁能手把手的教我啊?
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥100 AT89C52单片机C语言调试之后再回答
  • ¥15 AT89C52单片机C语言串口助手发送数据包返回值
  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)