dongyou2635 2018-04-19 11:18
浏览 49
已采纳

使用其他表单数据更新Woo商务结帐页面

I am collecting an additional User Meta Data for my Woo commerce check out page.

woocommerce_form_field('myName', array(
    'type' =>'text',
    'class'=>array('my-field-class form-row-wide'),
    'label'=>__('First Name'),
    'placeholder'=>__('Please enter your name'),
), $checkout->get_value('myName'));

And I am updating to the database with this code:

/*Update the info with the checkout*/
add_action('woocommerce_checkout_field_update_order_meta','my_custom_checkout_field_update_meta');
function my_custom_checkout_field_update_meta($order_id){
    if($_POST['MyName'])
        update_post_meta($order_id, 'First Name',esc_attr($POST['MyName']));
}

Each time I submit, I get an internal server Error, even though I am working on a local machine. I need to collect that data and persist it into the order Database. Anybody help?

  • 写回答

1条回答 默认 最新

  • duanmu2941 2018-04-19 11:37
    关注

    Update: Normally the postmeta meta_key should not use white spaces and capitals…

    Also your additional field should need to be inside the checkout form, if not nothing will be submitted and nothing can be saved.

    To display an custom text field and save it in the database once submitted, the best way is to use the following:

    // Add checkout custom text field
    add_action( 'woocommerce_before_order_notes', 'add_checkout_custom_field', 20, 1 );
    function add_checkout_custom_field( $checkout) {
    
        // Text field
        woocommerce_form_field('my_name', array(
            'type'        => 'text',
            'class'       => array('my-field-class form-row-wide'),
            'label'       => __('First Name'),
            'placeholder' =>__('Please enter your name'),
        ), $checkout->get_value('my_name') );
    }
    
    // Save the data to the order
    add_action('woocommerce_checkout_create_order','my_custom_checkout_field_update_meta');
    function my_custom_checkout_field_update_meta( $order ){
        if( isset($_POST['my_name']) && ! empty($_POST['my_name']) )
            $order->update_meta_data( '_my_name', sanitize_text_field($POST['my_name']) );
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works…

    To get this data once saved in the order, use get_post_meta( $order_id, '_my_name', true ); where $order_id is the dynamic order ID…

    Your additional text field will be located just before "Order notes" field:

    enter image description here

    Now your additional field is confusing as Billing and Shipping first name fields already exist in checkout page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应