[英]Editing metadata in the order admin page WooCommerce
我有一個自定義的 WooCommerce 支付網關,它使用以下方法保存元數據:
$order->update_meta_data('_example_data', esc_attr($_POST['_example_data']));
.
然后顯示在管理訂單頁面中,並使用以下內容進行編輯:
// In constructor
add_action('woocommerce_admin_order_data_after_order_details', array($this, 'display_payment_type_order_edit_pages'), 10, 1);
add_action('woocommerce_process_shop_order_meta', array($this, 'save_order_custom_field_meta'));
public function display_payment_type_order_edit_pages($order) {
$example_data = $order->get_meta('_example_data');
echo '<br class="clear" />';
echo '<h4>Example Details <a href="#" class="edit_address">Edit</a></h4>';
// Non-edit display.
echo '<div class="address">';
echo '<p><strong>' . __('Example Data') . ':</strong> ' . $example_data . '</p>';
echo '</div>';
echo '<div class="edit_address">';
woocommerce_wp_text_input(array(
'id' => '_example_data',
'label' => 'Example Data:',
'value' => $example_data,
'wrapper_class' => 'form-field-wide'
));
echo '</div>';
}
function save_order_custom_field_meta($post_id) {
if (isset($_POST['_example_data']))
update_post_meta($post_id, '_example_data', sanitize_text_field($_POST['_example_data']));
}
元數據從結帳中正確保存,它正確顯示並且在您開始編輯時出現字段。 但是,編輯訂單后元數據似乎沒有保存。
任何幫助,將不勝感激!
編輯:我在 WP 和 WC 的全新安裝上試用其他一些功能時注意到了一些奇怪的交互,我將范圍縮小(有點)到沒有以傳統方式實例化的類。
即我這樣做:
add_action('plugins_loaded', 'init_my_payment_gateway', 11);
function init_my_payment_gateway()
{
class My_Payment_Gateway extends WC_Payment_Gateway
{
// Rest of plugin here.
與常規方式有關:
$my_class = new My_Payment_Gateway();
此外,我還發現像這樣添加該操作
add_action('woocommerce_process_shop_order_meta', array('my_payment_gateway', 'save_order_custom_field_meta'));
當鈎子被觸發時執行函數; 當然有錯誤(以靜態方式運行非靜態)。 所以似乎在插件文件中添加操作會導致它不起作用?
最后,如果我使用$wp_filter['woocommerce_process_shop_order_meta'];
查看掛鈎附加了哪些功能$wp_filter['woocommerce_process_shop_order_meta'];
我實際上可以看到附加了save_order_custom_field_meta
。 然而,當鈎子運行時它不會執行(用日志記錄、斷點等確認)。
您的代碼優先功能不會顯示您的可編輯字段,並且您使事情變得比應有的更復雜。 請嘗試以下操作:
add_action( 'woocommerce_admin_order_data_after_order_details', 'display_custom_editable_field_on_admin_orders' );
function display_custom_editable_field_on_admin_orders( $order ) {
echo '<br class="clear" />
<div class="example_data_wrapper">';
woocommerce_wp_text_input( array(
'id' => '_example_data',
'label' => __('Example Data:'),
'wrapper_class' => 'form-field-wide'
) );
echo '</div>';
}
add_action( 'woocommerce_process_shop_order_meta', 'save_order_custom_field_meta' );
function save_order_custom_field_meta( $post_id ) {
if ( isset($_POST['_example_data']) )
update_post_meta( $post_id, '_example_data', sanitize_text_field($_POST['_example_data']) );
}
代碼位於活動子主題(或活動主題)的 functions.php 文件中。 測試和工作。
如果要在 Class 內的插件中使用代碼,可以在插件文件中使用以下代碼:
/*
Plugin Name: Admin Order Editable field
Plugin URI:
Description: Your shipping method plugin
Version: 1.0.0
Author: Me
Author URI:
*/
if ( ! class_exists( 'WC_Some_Class' ) ) {
class WC_Some_Class {
// In the constructor
public function __construct() {
# Your settings below to be defined
$this->text_domain = 'my_text_domain'; // The domain name
$this->meta_key = '_example_data'; // The field Id or field slug
$this->label_name = __('Example Data', $this->text_domain ); // Field label
# The hooks to be triggered
add_action( 'woocommerce_admin_order_data_after_order_details', array( $this, 'display_custom_editable_field_on_admin_orders' ) );
add_action( 'woocommerce_process_shop_order_meta', array( $this, 'save_order_custom_field_meta' ) );
}
// Outside the constructor
public function display_custom_editable_field_on_admin_orders( $order ) {
echo '<br class="clear" />
<div class="example_data_wrapper">';
woocommerce_wp_text_input( array(
'id' => $this->meta_key,
'label' => $this->label_name . ':',
'wrapper_class' => 'form-field-wide'
) );
echo '</div>';
}
public function save_order_custom_field_meta( $post_id ) {
if ( isset($_POST[$this->meta_key]) )
update_post_meta( $post_id, '_example_data', sanitize_text_field($_POST[$this->meta_key]) );
}
}
$some_class = new WC_Some_Class();
}
測試和工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.