![](/img/trans.png)
[英]Add a custom field in Woocommerce admin order list "Order" existing column
[英]WooCommerce: add custom field to order meta and admin order
使用以下代碼,我在 WooCommerce 產品上獲得了一個自定義字段,但如何在訂單、結賬和管理訂單(后端)上顯示它?
這是我在每個產品的 woocommerce 上的自定義字段
這是我的管理訂單(后端)詳細信息。 我嘗試顯示我在每個產品帖子中輸入的正確元數據。
如何在管理訂單中顯示我在自定義字段中輸入的值? 我也希望有人可以幫助我在訂單和結帳頁面中顯示元數據……或者指導我走向正確的方向。 我意識到為了在管理訂單中顯示它,我必須確保首先注冊元以訂購或結帳。
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields()
{
global $woocommerce, $post;
echo '<div class="product_custom_field">';
// Custom Product Text Field
woocommerce_wp_text_input(array(
'id' => '_custom_product_text_field',
'placeholder' => 'Custom Product Text Field',
'label' => __('Custom Product Text Field', 'woocommerce') ,
'desc_tip' => 'true'
));
// Custom Product Number Field
woocommerce_wp_text_input(array(
'id' => '_custom_product_number_field',
'placeholder' => 'Custom Product Number Field',
'label' => __('Custom Product Number Field', 'woocommerce') ,
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
));
// Custom Product Textarea
woocommerce_wp_textarea_input(array(
'id' => '_custom_product_textarea',
'placeholder' => 'Custom Product Textarea',
'label' => __('Custom Product Textarea', 'woocommerce')
));
echo '</div>';
}
function woocommerce_product_custom_fields_save($post_id)
{
// Custom Product Text Field
$woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
if (!empty($woocommerce_custom_product_text_field)) update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));
// Custom Product Number Field
$woocommerce_custom_product_number_field = $_POST['_custom_product_number_field'];
if (!empty($woocommerce_custom_product_number_field)) update_post_meta($post_id, '_custom_product_number_field', esc_attr($woocommerce_custom_product_number_field));
// Custom Product Textarea Field
$woocommerce_custom_procut_textarea = $_POST['_custom_product_textarea'];
if (!empty($woocommerce_custom_procut_textarea)) update_post_meta($post_id, '_custom_product_textarea', esc_html($woocommerce_custom_procut_textarea));
}
// Display in order edit
add_action('woocommerce_admin_order_data_after_billing_address', 'display_verification_id_in_admin_order_meta', 10, 1);
function display_verification_id_in_admin_order_meta($order)
{
// compatibility with WC +3
$order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id;
$total_incart = number_format((float)$order->get_total() - $order->get_total_tax() - $order->get_total_shipping() - $order->get_shipping_tax() , wc_get_price_decimals() , '.', '');
$post_meta_value = get_post_meta($post->ID, '_custom_product_text_field', true);
echo '<p><strong>' . __('TEXTFIELD', 'woocommerce') . ':</strong> ' . get_post_meta($order_id, '_custom_product_text_field', true) . '</p>';
echo '<p><strong>' . __('NUMBERFIELD', 'woocommerce') . ':</strong> ' . get_post_meta($order_id, '_custom_product_number_field', true) . '</p>';
echo '<p><strong>' . __('TEXTAREAFIELD', 'woocommerce') . ':</strong> ' . get_post_meta($order_id, '_custom_product_textarea', true) . '</p>';
echo '<p><strong>Total price items: </strong>' . $total_incart . '</p>';
}
已經快一年了,但我認為它可以幫助某人......
這篇文章准確解釋了所質疑的內容。
我認為文章中最相關的部分可以解決問題
/**
* Display custom fields in to Admin order details
*/
add_action( 'woocommerce_admin_order_data_after_order_details', 'custom_woocommerce_admin_order_data_after_order_details' );
function custom_woocommerce_admin_order_data_after_order_details( $order ){
?>
<br class="clear" />
<h4>My Custom Checkout Field Name <a href="#" class="edit_custom_field">Edit</a></h4>
<?php
/*
* get all the meta data values we need
*/
$custom_field_value = get_post_meta( $order->id, 'my_custom_field_name', true );
?>
<div class="custom_field">
<p><strong>Custom Field Name:</strong> <?php echo wpautop( $custom_field_value ) ?></p>
</div>
<div class="edit_custom_field"> <!-- use same css class in h4 tag -->
<?php
woocommerce_wp_textarea_input( array(
'id' => 'custom_field_name',
'label' => 'Custom Field Name:',
'value' => $custom_field_value,
'wrapper_class' => 'form-field-wide'
) );
?>
</div>
<?php
}
/**
* Save the custom fields values
*/
add_action( 'woocommerce_process_shop_order_meta', 'custom_woocommerce_process_shop_order_meta' );
function custom_woocommerce_process_shop_order_meta( $order_id ){
update_post_meta( $order_id, 'custom_field_name', wc_sanitize_textarea( $_POST[ 'custom_field_name' ] ) );
}
文章內有一個鏈接,他解釋了如何在結帳頁面上添加字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.