在Woocommerce的管理订单编辑页面上获取嵌套的自定义字段元数据(Get nested custom fields meta data on admin order edit pages in

系统教程 行业动态 更新时间:2024-06-14 16:57:18
在Woocommerce的管理订单编辑页面上获取嵌套的自定义字段元数据(Get nested custom fields meta data on admin order edit pages in Woocommerce)

在woocommerce中,我基于购物车数量在结帐页面上添加了自定义字段。

// Adding Custom Fields based on Cart Count add_action( 'woocommerce_before_checkout_billing_form', 'srd_custom_einstiegswahl'); function srd_custom_einstiegswahl($checkout){ global $woocommerce; $items = $woocommerce->cart->get_cart(); $i = 1; foreach($items as $item => $values) { $_product = $values['data']->post; $quantity = $values['quantity']; $x = 1; while ($x <= $quantity) { // Add fields here echo '<h6>Reiseteilnehmer '.$x . '</h6>'; woocommerce_form_field( 'attendee_surname_'.$x, array( 'type' => 'text', 'class' => array('checkout_vorname'), 'label' => __('Vorame'), 'placeholder' => __(''), 'required' => true ), $checkout->get_value( 'attendee_surname_'.$x )); woocommerce_form_field( 'attendee_name_'.$x, array( 'type' => 'text', 'class' => array('checkout_nachname'), 'label' => __('Nachname'), 'placeholder' => __(''), 'required' => true ), $checkout->get_value( 'attendee_name_'.$x )); echo '<select name=einstiegswahl'.$x .'>'; // Select field populated by Custom Product Meta echo '<option value="" style="display:none"> Bitte wählen Sie Ihren Einstiegsort </option>'; // Loop through cart items foreach ( WC()->cart->get_cart() as $cart_item ) { // Get the custom field data $einstiegsorte = get_post_meta( $cart_item[ 'product_id' ], '_einstiegsorte', true ); if( ! empty($einstiegsorte) ){ // if it's multiline we split it in an array $select_field_items = explode( "\n", $einstiegsorte ); // If the array has more than one item if( sizeof( $select_field_items ) > 1 ){ foreach( $select_field_items as $value ) echo '<option value="'. $value .'">' . $value . '</option>'; } // If there is only one line else { // we clean it $value = str_replace('\n', '', $einstiegsorte); echo '<option value="'. $value .'">' . $value . '</option>'; } } } echo '</select>'; $checkout->get_value( 'einstiegswahl'.$x ); $x++; } $i++; } } // Process the checkout add_action('woocommerce_checkout_process', 'srd_teilnehmer_fields_process'); function srd_teilnehmer_fields_process() { global $woocommerce; $items = $woocommerce->cart->get_cart(); $i = 1; foreach($items as $item => $values) { $_product = $values['data']->post; $quantity = $values['quantity']; $x = 1; for($x = 1; $x <= $quantity; $x++ ) { //while ($x <= $quantity) { if (!$_POST['attendee_surname_'.$x] || !$_POST['attendee_name_'.$x] || !$_POST['einstiegswahl'.$x]) wc_add_notice( __( 'Bitte wählen Sie Ihren Einstiegsort', 'woocommerce' ), 'error' ); } } } // Update the order meta with field value add_action('woocommerce_checkout_update_order_meta', 'srd_teilnehmer_update_order_meta'); function srd_teilnehmer_update_order_meta( $order_id ) { global $woocommerce; $items = $woocommerce->cart->get_cart(); $i = 1; foreach($items as $item => $values) { $_product = $values['data']->post; $quantity = $values['quantity']; $x = 1; for($x = 1; $x <= $quantity; $x++ ) { if ( $_POST['attendee_name_'.$x] ) update_post_meta( $order_id, 'attendee_surname_'.$x , sanitize_text_field($_POST['attendee_name_'.$x]) ); if ($_POST['attendee_surname_'.$x]) update_post_meta( $order_id, 'attendee_name_'.$x, esc_attr($_POST['attendee_surname_'.$x])); if ($_POST['einstiegswahl'.$x]) update_post_meta( $order_id, ' Teilnehmer Einstiegsort' .$x, esc_attr($_POST['einstiegswahl'.$x])); }}}

所有的作品都很好,领域得到验证和保存。

问题:我无法获取并显示管理员订单编辑页面中的值。

由于此时我不知道根据订购的数量创建了多少个字段,因此首先需要获取一些订购商品。

这是相关的代码:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'srd_teilnehmer_checkout_field_display_admin_order_meta', 10, 1 ); //add_action( 'woocommerce_email_order_meta', 'srd_teilnehmer_checkout_field_display_admin_order_meta', 10, 3 ); function srd_teilnehmer_checkout_field_display_admin_order_meta($order) { $order = wc_get_order( $order_id ); $items = $item_data->get_quantity(); foreach($items as $item => $values) { $order_quantity = $values['quantity']; $x = 1; while ($x <= $order_quantity) { echo '<p><strong>'.__('Nachname'.$x).':</strong> ' . get_post_meta( $order->id, 'attendee_name_'.$x, true ) . '</p>'; } }}

如何在管理订单编辑页面中获取并显示这些自定义字段值? 如何在电子邮件通知中显示该自定义字段值?

任何帮助将不胜感激。

In woocommerce I have added custom fields on checkout page based on cart count.

// Adding Custom Fields based on Cart Count add_action( 'woocommerce_before_checkout_billing_form', 'srd_custom_einstiegswahl'); function srd_custom_einstiegswahl($checkout){ global $woocommerce; $items = $woocommerce->cart->get_cart(); $i = 1; foreach($items as $item => $values) { $_product = $values['data']->post; $quantity = $values['quantity']; $x = 1; while ($x <= $quantity) { // Add fields here echo '<h6>Reiseteilnehmer '.$x . '</h6>'; woocommerce_form_field( 'attendee_surname_'.$x, array( 'type' => 'text', 'class' => array('checkout_vorname'), 'label' => __('Vorame'), 'placeholder' => __(''), 'required' => true ), $checkout->get_value( 'attendee_surname_'.$x )); woocommerce_form_field( 'attendee_name_'.$x, array( 'type' => 'text', 'class' => array('checkout_nachname'), 'label' => __('Nachname'), 'placeholder' => __(''), 'required' => true ), $checkout->get_value( 'attendee_name_'.$x )); echo '<select name=einstiegswahl'.$x .'>'; // Select field populated by Custom Product Meta echo '<option value="" style="display:none"> Bitte wählen Sie Ihren Einstiegsort </option>'; // Loop through cart items foreach ( WC()->cart->get_cart() as $cart_item ) { // Get the custom field data $einstiegsorte = get_post_meta( $cart_item[ 'product_id' ], '_einstiegsorte', true ); if( ! empty($einstiegsorte) ){ // if it's multiline we split it in an array $select_field_items = explode( "\n", $einstiegsorte ); // If the array has more than one item if( sizeof( $select_field_items ) > 1 ){ foreach( $select_field_items as $value ) echo '<option value="'. $value .'">' . $value . '</option>'; } // If there is only one line else { // we clean it $value = str_replace('\n', '', $einstiegsorte); echo '<option value="'. $value .'">' . $value . '</option>'; } } } echo '</select>'; $checkout->get_value( 'einstiegswahl'.$x ); $x++; } $i++; } } // Process the checkout add_action('woocommerce_checkout_process', 'srd_teilnehmer_fields_process'); function srd_teilnehmer_fields_process() { global $woocommerce; $items = $woocommerce->cart->get_cart(); $i = 1; foreach($items as $item => $values) { $_product = $values['data']->post; $quantity = $values['quantity']; $x = 1; for($x = 1; $x <= $quantity; $x++ ) { //while ($x <= $quantity) { if (!$_POST['attendee_surname_'.$x] || !$_POST['attendee_name_'.$x] || !$_POST['einstiegswahl'.$x]) wc_add_notice( __( 'Bitte wählen Sie Ihren Einstiegsort', 'woocommerce' ), 'error' ); } } } // Update the order meta with field value add_action('woocommerce_checkout_update_order_meta', 'srd_teilnehmer_update_order_meta'); function srd_teilnehmer_update_order_meta( $order_id ) { global $woocommerce; $items = $woocommerce->cart->get_cart(); $i = 1; foreach($items as $item => $values) { $_product = $values['data']->post; $quantity = $values['quantity']; $x = 1; for($x = 1; $x <= $quantity; $x++ ) { if ( $_POST['attendee_name_'.$x] ) update_post_meta( $order_id, 'attendee_surname_'.$x , sanitize_text_field($_POST['attendee_name_'.$x]) ); if ($_POST['attendee_surname_'.$x]) update_post_meta( $order_id, 'attendee_name_'.$x, esc_attr($_POST['attendee_surname_'.$x])); if ($_POST['einstiegswahl'.$x]) update_post_meta( $order_id, ' Teilnehmer Einstiegsort' .$x, esc_attr($_POST['einstiegswahl'.$x])); }}}

All works fine and fields get validated and saved.

The problem: I am not able to get and display the values in admin Order edit pages.

Since I do not know at this point how many fields were created based on the amount ordered, I first need to get the a number of ordered items.

Here is that related code:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'srd_teilnehmer_checkout_field_display_admin_order_meta', 10, 1 ); //add_action( 'woocommerce_email_order_meta', 'srd_teilnehmer_checkout_field_display_admin_order_meta', 10, 3 ); function srd_teilnehmer_checkout_field_display_admin_order_meta($order) { $order = wc_get_order( $order_id ); $items = $item_data->get_quantity(); foreach($items as $item => $values) { $order_quantity = $values['quantity']; $x = 1; while ($x <= $order_quantity) { echo '<p><strong>'.__('Nachname'.$x).':</strong> ' . get_post_meta( $order->id, 'attendee_name_'.$x, true ) . '</p>'; } }}

How can I get and display those custom field values in admin Order edit pages? How to display that custom field values in email notifications?

Any help will be appreciated.

最满意答案

你正在复杂化一些事情,并且你的代码有一些错误。

在管理员订单编辑页面(这里是右栏)中显示与会者预订数据作为自定义元框应该更好。

所以我完全重新访问了你的代码:

// Adding Custom Fields based on Cart Count add_action( 'woocommerce_before_checkout_billing_form', 'srd_custom_einstiegswahl'); function srd_custom_einstiegswahl( $checkout ){ $count = 1; // Loop through cart items foreach( WC()->cart->get_cart() as $cart_item ) { $options = array( '' => __("Bitte wählen Sie Ihren Einstiegsort") ); $einstiegsorte = get_post_meta( $cart_item[ 'product_id' ], '_einstiegsorte', true ); if( ! empty($einstiegsorte) ){ $option_items = explode( "\n", $einstiegsorte ); if( sizeof( $option_items ) > 1 ){ foreach( $option_items as $value ) $options[$value] = $value; } else { $value = str_replace('\n', '', $einstiegsorte); $options[$value] = $value; } } // Loop through cart item quantity for($i = 1; $i <= $cart_item['quantity']; $i++ ) { $j = $count.'_'.$i; echo '<h6>Reiseteilnehmer '.$i . '</h6>'; woocommerce_form_field( '_teilnehmer_vorame_'.$j, array( 'type' => 'text', 'class' => array('checkout_vorname'), 'label' => __('Vorame'), 'required' => true, ), $checkout->get_value( '_teilnehmer_vorame_'.$j )); woocommerce_form_field( '_teilnehmer_nachname_'.$j, array( 'type' => 'text', 'class' => array('checkout_nachname'), 'label' => __('Nachname'), 'required' => true, ), $checkout->get_value( '_teilnehmer_nachname_'.$j )); woocommerce_form_field( '_teilnehmer_einstiegswahl_'.$j, array( 'type' => 'select', 'class' => array('checkout_einstiegswahl'), 'label' => __('Einstiegswahl'), 'required' => true, 'options' => $options, ), $checkout->get_value( '_teilnehmer_einstiegswahl_'.$j )); } $count++; } } // Custom checkout fields validation add_action('woocommerce_checkout_process', 'srd_teilnehmer_fields_process'); function srd_teilnehmer_fields_process() { $count = 1; // Loop through cart items foreach( WC()->cart->get_cart() as $cart_item ) { // Loop through cart item quantity for($i = 1; $i <= $cart_item['quantity']; $i++ ) { $j = $count.'_'.$i; if (!$_POST['_teilnehmer_vorame_'.$j] || !$_POST['_teilnehmer_nachname_'.$j] || !$_POST['_teilnehmer_einstiegswahl_'.$j]) wc_add_notice( __( 'Bitte wählen Sie Ihren Einstiegsort', 'woocommerce' ), 'error' ); } $count++; } } // Update the order meta data with checkout custom fields values add_action('woocommerce_checkout_create_order', 'srd_teilnehmer_checkout_create_order', 20, 2 ); function srd_teilnehmer_checkout_create_order( $order, $data ) { $count = 1; // Loop through cart item quantity foreach( WC()->cart->get_cart() as $cart_item ) { // Loop through item quantity for($i = 1; $i <= $cart_item['quantity']; $i++ ) { $j = $count.'_'.$i; if ( isset($_POST['_teilnehmer_vorame_'.$j]) ) $order->update_meta_data( '_teilnehmer_vorame_'.$j , sanitize_text_field($_POST['_teilnehmer_vorame_'.$j]) ); if ( isset($_POST['_teilnehmer_nachname_'.$j]) ) $order->update_meta_data( '_teilnehmer_nachname_'.$j, sanitize_text_field($_POST['_teilnehmer_nachname_'.$j]) ); if ( isset($_POST['_teilnehmer_einstiegswahl_'.$j]) ) $order->update_meta_data( '_teilnehmer_einstiegswahl_'.$j, sanitize_text_field($_POST['_teilnehmer_einstiegswahl_'.$j]) ); } $count++; } } // Adding Custom metabox in admin orders edit pages (on the right column) add_action( 'add_meta_boxes', 'add_reiseteilnehmer_metabox' ); function add_reiseteilnehmer_metabox(){ add_meta_box( 'attendees', __('Reiseteilnehmer'), 'reiseteilnehmer_inhalt', 'shop_order', 'side', // or 'normal' 'default' // or 'high' ); } // Adding the content for the custom metabox function reiseteilnehmer_inhalt() { $order = wc_get_order(get_the_id()); $count = 1; $key_labels = array( 'vorame', 'nachname', 'einstiegswahl' ); // Loop through order items foreach ( $order->get_items() as $item ){ echo '<h4 style="margin:1em 0 .5em;">Order item '.$count.'</h4>'; // Loop through item quantity for($i = 1; $i <= $item->get_quantity(); $i++ ) { echo '<div style="background-color:#eee;padding:2px;margin-bottom:.4em;"> <h4 style="margin:.5em 0;padding:2px;">Reiseteilnehmer '.$i.'</h4> <table style="text-align:left;margin-bottom:.7em;" cellpadding="2"><tbody>'; $style = ' style="padding:2px 0;"'; // Loop through attendee fields foreach( $key_labels as $key ){ $value = get_post_meta( $order->get_id(), '_teilnehmer_'.$key.'_'.$count.'_'.$i, true ); echo '<tr><th>'.ucfirst($key).':</th><td>'.$value.'</td></tr>'; } echo '</tbody></table></div>'; } $count++; } }

代码在你的活动子主题(或活动主题)的function.php文件中。 经过测试和工作。

在这里输入图像描述

要在电子邮件和前端“已收到订单”和我的账户“订单视图”页面中显示该信息,您必须提出一个新问题,因为它过于宽泛,无法在一个答案中得到解答。

You are complicating a little bit things and there is some mistakes in your code.

It should be much better to display the attendees booking data as a custom meta box in admin shop orders edit pages (Here on the right column).

So I have completely revisited your code:

// Adding Custom Fields based on Cart Count add_action( 'woocommerce_before_checkout_billing_form', 'srd_custom_einstiegswahl'); function srd_custom_einstiegswahl( $checkout ){ $count = 1; // Loop through cart items foreach( WC()->cart->get_cart() as $cart_item ) { $options = array( '' => __("Bitte wählen Sie Ihren Einstiegsort") ); $einstiegsorte = get_post_meta( $cart_item[ 'product_id' ], '_einstiegsorte', true ); if( ! empty($einstiegsorte) ){ $option_items = explode( "\n", $einstiegsorte ); if( sizeof( $option_items ) > 1 ){ foreach( $option_items as $value ) $options[$value] = $value; } else { $value = str_replace('\n', '', $einstiegsorte); $options[$value] = $value; } } // Loop through cart item quantity for($i = 1; $i <= $cart_item['quantity']; $i++ ) { $j = $count.'_'.$i; echo '<h6>Reiseteilnehmer '.$i . '</h6>'; woocommerce_form_field( '_teilnehmer_vorame_'.$j, array( 'type' => 'text', 'class' => array('checkout_vorname'), 'label' => __('Vorame'), 'required' => true, ), $checkout->get_value( '_teilnehmer_vorame_'.$j )); woocommerce_form_field( '_teilnehmer_nachname_'.$j, array( 'type' => 'text', 'class' => array('checkout_nachname'), 'label' => __('Nachname'), 'required' => true, ), $checkout->get_value( '_teilnehmer_nachname_'.$j )); woocommerce_form_field( '_teilnehmer_einstiegswahl_'.$j, array( 'type' => 'select', 'class' => array('checkout_einstiegswahl'), 'label' => __('Einstiegswahl'), 'required' => true, 'options' => $options, ), $checkout->get_value( '_teilnehmer_einstiegswahl_'.$j )); } $count++; } } // Custom checkout fields validation add_action('woocommerce_checkout_process', 'srd_teilnehmer_fields_process'); function srd_teilnehmer_fields_process() { $count = 1; // Loop through cart items foreach( WC()->cart->get_cart() as $cart_item ) { // Loop through cart item quantity for($i = 1; $i <= $cart_item['quantity']; $i++ ) { $j = $count.'_'.$i; if (!$_POST['_teilnehmer_vorame_'.$j] || !$_POST['_teilnehmer_nachname_'.$j] || !$_POST['_teilnehmer_einstiegswahl_'.$j]) wc_add_notice( __( 'Bitte wählen Sie Ihren Einstiegsort', 'woocommerce' ), 'error' ); } $count++; } } // Update the order meta data with checkout custom fields values add_action('woocommerce_checkout_create_order', 'srd_teilnehmer_checkout_create_order', 20, 2 ); function srd_teilnehmer_checkout_create_order( $order, $data ) { $count = 1; // Loop through cart item quantity foreach( WC()->cart->get_cart() as $cart_item ) { // Loop through item quantity for($i = 1; $i <= $cart_item['quantity']; $i++ ) { $j = $count.'_'.$i; if ( isset($_POST['_teilnehmer_vorame_'.$j]) ) $order->update_meta_data( '_teilnehmer_vorame_'.$j , sanitize_text_field($_POST['_teilnehmer_vorame_'.$j]) ); if ( isset($_POST['_teilnehmer_nachname_'.$j]) ) $order->update_meta_data( '_teilnehmer_nachname_'.$j, sanitize_text_field($_POST['_teilnehmer_nachname_'.$j]) ); if ( isset($_POST['_teilnehmer_einstiegswahl_'.$j]) ) $order->update_meta_data( '_teilnehmer_einstiegswahl_'.$j, sanitize_text_field($_POST['_teilnehmer_einstiegswahl_'.$j]) ); } $count++; } } // Adding Custom metabox in admin orders edit pages (on the right column) add_action( 'add_meta_boxes', 'add_reiseteilnehmer_metabox' ); function add_reiseteilnehmer_metabox(){ add_meta_box( 'attendees', __('Reiseteilnehmer'), 'reiseteilnehmer_inhalt', 'shop_order', 'side', // or 'normal' 'default' // or 'high' ); } // Adding the content for the custom metabox function reiseteilnehmer_inhalt() { $order = wc_get_order(get_the_id()); $count = 1; $key_labels = array( 'vorame', 'nachname', 'einstiegswahl' ); // Loop through order items foreach ( $order->get_items() as $item ){ echo '<h4 style="margin:1em 0 .5em;">Order item '.$count.'</h4>'; // Loop through item quantity for($i = 1; $i <= $item->get_quantity(); $i++ ) { echo '<div style="background-color:#eee;padding:2px;margin-bottom:.4em;"> <h4 style="margin:.5em 0;padding:2px;">Reiseteilnehmer '.$i.'</h4> <table style="text-align:left;margin-bottom:.7em;" cellpadding="2"><tbody>'; $style = ' style="padding:2px 0;"'; // Loop through attendee fields foreach( $key_labels as $key ){ $value = get_post_meta( $order->get_id(), '_teilnehmer_'.$key.'_'.$count.'_'.$i, true ); echo '<tr><th>'.ucfirst($key).':</th><td>'.$value.'</td></tr>'; } echo '</tbody></table></div>'; } $count++; } }

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

enter image description here

To display that in emails and in front end "Order Received" and My account "Order view" pages, you will have to ask a new question as it is just too broad to be answered in one answer.

更多推荐

本文发布于:2023-04-12 20:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/d0b41d3e781468376c1b4b0b2082c2b8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   自定义   字段   订单   编辑

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!