保存自定义结帐字段并在管理员 Woocommerce 订单中显示它们
Save custom checkout fields and display them in admin Woocommerce Orders
我根据我的订单数量创建了一些额外的表单字段。因此,如果我在购物车中订购了 2 件商品,该字段应该出现两次。
一切正常,除了我的订单中只有一个字段的输入。我想为每个相似的表单字段设置单独的值。
foreach(WC()->cart->get_cart() as $cart_item){
//2nd Loop go through each unit related to item quantity
for($i = 1; $i <= $cart_item['quantity']; $i++){
$index++;
woocommerce_form_field('myname', array(
'type' =>'text',
'class'=>array('my-field-class form-row-wide'),
'label'=>__('My Name'),
'placeholder'=>__('Please enter your name'),
), $checkout->get_value('myname'));
我用这个更新:
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta($order_id){
if (! empty( $_POST['myname'])){
update_post_meta($order_id,'Aspirant Name', sanitize_text_field($_POST['myname']));
}
}
并显示为:
add_action('woocommerce_admin_order_data_after_billing_address','my_custom_checkout_field_display_admin_order_meta', 10, 1);
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('My Name').':</strong> ' . get_post_meta($order->get_id(),'My Name', true).'</p>';
}
感谢任何帮助。
以下是保存订单中所有相关自定义结帐值并在订单编辑页面中显示帐单详细信息下方的正确方法:
// 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){
$index++;
// 2nd Loop through each unit related to item quantity
for($i = 1; $i <= $cart_item['quantity']; $i++){
woocommerce_form_field("my_field[$index][$i]", array(
'type' =>'text',
'class'=>array('my-field-class form-row-wide'),
'label'=>__('My Field')." (item $index - $i)",
'placeholder'=>__('Please enter your data'),
), $checkout->get_value("my_field[$index][$i]"));
}
}
}
// 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( $order->get_items() as $item ){
$index++;
// 2nd Loop through each unit related to item quantity
for($i = 1; $i <= $item->get_quantity(); $i++){
if (isset( $_POST['my_field'][$index][$i]) && ! empty($_POST['my_field'][$index][$i]) ){
$order->update_meta_data( '_my_field_'.$index.'_'.$i, esc_attr( $_POST['my_field'][$index][$i] ) );
}
}
}
}
// Display fields in order edit pages
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 ){
$index = 0;
// 1st Loop through order items
foreach( $order->get_items() as $item ){
$index++;
// 2nd Loop through each unit related to item quantity
for($i = 1; $i <= $item->get_quantity(); $i++){
$my_field = get_post_meta($order->get_id(),'_my_field_'.$index.'_'.$i, true );
if (! empty($my_field) ){
echo '<p><strong>'.__('My Field')." <em>(item $index - $i)</em>".':</strong> ' . $my_field . '</p>';
}
}
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效
我根据我的订单数量创建了一些额外的表单字段。因此,如果我在购物车中订购了 2 件商品,该字段应该出现两次。
一切正常,除了我的订单中只有一个字段的输入。我想为每个相似的表单字段设置单独的值。
foreach(WC()->cart->get_cart() as $cart_item){
//2nd Loop go through each unit related to item quantity
for($i = 1; $i <= $cart_item['quantity']; $i++){
$index++;
woocommerce_form_field('myname', array(
'type' =>'text',
'class'=>array('my-field-class form-row-wide'),
'label'=>__('My Name'),
'placeholder'=>__('Please enter your name'),
), $checkout->get_value('myname'));
我用这个更新:
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta($order_id){
if (! empty( $_POST['myname'])){
update_post_meta($order_id,'Aspirant Name', sanitize_text_field($_POST['myname']));
}
}
并显示为:
add_action('woocommerce_admin_order_data_after_billing_address','my_custom_checkout_field_display_admin_order_meta', 10, 1);
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('My Name').':</strong> ' . get_post_meta($order->get_id(),'My Name', true).'</p>';
}
感谢任何帮助。
以下是保存订单中所有相关自定义结帐值并在订单编辑页面中显示帐单详细信息下方的正确方法:
// 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){
$index++;
// 2nd Loop through each unit related to item quantity
for($i = 1; $i <= $cart_item['quantity']; $i++){
woocommerce_form_field("my_field[$index][$i]", array(
'type' =>'text',
'class'=>array('my-field-class form-row-wide'),
'label'=>__('My Field')." (item $index - $i)",
'placeholder'=>__('Please enter your data'),
), $checkout->get_value("my_field[$index][$i]"));
}
}
}
// 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( $order->get_items() as $item ){
$index++;
// 2nd Loop through each unit related to item quantity
for($i = 1; $i <= $item->get_quantity(); $i++){
if (isset( $_POST['my_field'][$index][$i]) && ! empty($_POST['my_field'][$index][$i]) ){
$order->update_meta_data( '_my_field_'.$index.'_'.$i, esc_attr( $_POST['my_field'][$index][$i] ) );
}
}
}
}
// Display fields in order edit pages
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 ){
$index = 0;
// 1st Loop through order items
foreach( $order->get_items() as $item ){
$index++;
// 2nd Loop through each unit related to item quantity
for($i = 1; $i <= $item->get_quantity(); $i++){
$my_field = get_post_meta($order->get_id(),'_my_field_'.$index.'_'.$i, true );
if (! empty($my_field) ){
echo '<p><strong>'.__('My Field')." <em>(item $index - $i)</em>".':</strong> ' . $my_field . '</p>';
}
}
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效