[英]Display custom dynamic fields stored data woocommerce order admin and thankyou page from meta.?
I am stuck from past few days with to store data of dynamically generated fields on my woocommerce checkout page.过去几天我一直在我的 woocommerce 结帐页面上存储动态生成的字段的数据。 I am using woocommerce booking plugin in which i have to get names of person type.我正在使用 woocommerce 预订插件,我必须在其中获取人员类型的姓名。 for that i have did this.为此,我已经这样做了。
// Add checkout custom text fields
add_action( 'woocommerce_before_order_notes', 'add_checkout_custom_text_fields', 20, 1 );
function add_checkout_custom_text_fields( $checkout) {
$index = 0;
// 1st Loop through cart items
foreach(WC()->cart->get_cart() as $cart_item){
// 2nd Loop through each unit related to item quantity
for($i = 1; $i <= $cart_item['booking']['Adults']; $i++){
$index++;
woocommerce_form_field("my_field$index", array(
'type' =>'text',
'class'=>array('my-field-class form-row-wide'),
'label'=>__('Adults')." ($i)",
'placeholder'=>__('Enter adult name'),
'required' => true,
), $checkout->get_value("my_field$index"));
}
for($i = 1; $i <= $cart_item['booking']['Childs']; $i++){
$index++;
woocommerce_form_field("my_field$index", array(
'type' =>'text',
'class'=>array('my-field-class form-row-wide'),
'label'=>__('Childs')." ($i)",
'placeholder'=>__('Enter child name'),
'required' => true,
), $checkout->get_value("my_field$index"));
}
}
}
I am getting fields on my checkout page those working perfectly but now i want them to stored on database and display on admin order page and thankyou page of order, which is become deperession to me how can i get to resolve this, Stackoverflow is only platform i believe can get solution.我在我的结帐页面上找到了那些工作正常的字段,但现在我希望它们存储在数据库中并显示在管理订单页面和感谢订单页面上,这对我来说已经成为 deperession 我该如何解决这个问题,Stackoverflow 只是平台我相信可以得到解决。
Edit After spending alot of hours i have been successfull to store custom data into database, now i want it to be display on admin and thankyou page.编辑花费大量时间后,我已成功将自定义数据存储到数据库中,现在我希望它显示在管理和感谢页面上。
Here is my code which saving fields to post meta.这是我保存字段以发布元数据的代码。
// Save fields in order meta data
add_action('woocommerce_checkout_create_order', 'save_custom_fields_to_order_meta_data', 20, 2 );
function save_custom_fields_to_order_meta_data( $order, $data ) {
$index = 0;
// 1st Loop through order items
foreach(WC()->cart->get_cart() as $cart_item){
// 2nd Loop through each unit related to item quantity
for($i = 1; $i <= $cart_item['booking']['Adults']; $i++){
$index++;
if (isset( $_POST['my_field'.$index.'']) && ! empty($_POST['my_field'.$index.'']) ){
$order->update_meta_data( '_my_field_'.$index.'_'.$i, esc_attr( $_POST['my_field'.$index.''] ) );
}
}
for($i = 1; $i <= $cart_item['booking']['Childs']; $i++){
$index++;
if (isset( $_POST['my_field'.$index.'']) && ! empty($_POST['my_field'.$index.'']) ){
$order->update_meta_data( '_my_field_'.$index.'_'.$i, esc_attr( $_POST['my_field'.$index.''] ) );
}
}
}
}
// Display fields in order edit
add_action('woocommerce_admin_order_data_after_billing_address','display_custom_fields_in_admin_order', 20, 1);
function display_custom_fields_in_admin_order( $order ){
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE '_adult%' and post_id = ".$order->get_id()."" );
$i= 1;
foreach($results as $result) {
echo '<p><strong>Adult Name '.$i.':</strong>' . $result->meta_value . '</p>';
$i++;
}
$results = $wpdb->get_results( "SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE '_child%' and post_id = ".$order->get_id()."" );
$i= 1;
foreach($results as $result) {
echo '<p><strong>Child Name '.$i.':</strong>' . $result->meta_value . '</p>';
$i++;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.