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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器