使用附加表单数据更新 Woo commerce 结帐页面
Updating Woo commerce checkout Page with Additional Form Data
我正在为我的 Woo 商务结帐页面收集额外的用户元数据。
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'));
我正在使用以下代码更新到数据库:
/*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']));
}
每次提交时,我都会收到内部服务器错误,即使我是在本地机器上工作也是如此。我需要收集该数据并将其保存到订单数据库中。有人帮忙吗?
更新: 通常 postmeta meta_key
不应该使用空格和大写...
此外,您的附加字段应位于结帐表单内,否则将不会提交任何内容,也不会保存任何内容。
要显示自定义文本字段并在提交后将其保存在数据库中,最好的方法是使用以下方法:
// 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']) );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。经过测试并有效……
要在订单中保存后获取此数据,请使用 get_post_meta( $order_id, '_my_name', true );
,其中 $order_id
是动态订单 ID…
您的附加文本字段将位于 "Order notes" 字段之前:
Now your additional field is confusing as Billing and Shipping first name fields already exist in checkout page.
我正在为我的 Woo 商务结帐页面收集额外的用户元数据。
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'));
我正在使用以下代码更新到数据库:
/*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']));
}
每次提交时,我都会收到内部服务器错误,即使我是在本地机器上工作也是如此。我需要收集该数据并将其保存到订单数据库中。有人帮忙吗?
更新: 通常 postmeta meta_key
不应该使用空格和大写...
此外,您的附加字段应位于结帐表单内,否则将不会提交任何内容,也不会保存任何内容。
要显示自定义文本字段并在提交后将其保存在数据库中,最好的方法是使用以下方法:
// 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']) );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。经过测试并有效……
要在订单中保存后获取此数据,请使用 get_post_meta( $order_id, '_my_name', true );
,其中 $order_id
是动态订单 ID…
您的附加文本字段将位于 "Order notes" 字段之前:
Now your additional field is confusing as Billing and Shipping first name fields already exist in checkout page.