如何保存和显示来自 Woocommerce 订单管理员的用户元数据
How to save and display user meta from Woocommerce order admin
客户有一个名为 billing_ean
的额外字段作为其帐单地址的一部分。
我正在使用以下代码从管理面板的 Woocommerce 单一订单页面加载、编辑和保存他们的 EAN 编号。该代码在加载和编辑字段时完美运行,但在保存时(更新订单),它根本不会更新他们的客户信息。
function customer_ean_edit( $customer_id ){
$eannr = get_user_meta( $customer_id->get_customer_id(), 'billing_eannr', true );
?>
<div class="address">
<p<?php if( !$eannr ) echo ' class="none_set"' ?>>
<strong>EAN nr.:</strong>
<?php echo ( $eannr ) ? $eannr : 'Der er ikke angivet et EAN nr.' ?>
</p>
</div>
<div class="edit_address"><?php
woocommerce_wp_text_input( array(
'id' => 'billing_eannr',
'label' => 'EAN nr.:',
'value' => $eannr,
'wrapper_class' => 'form-field-wide'
) );
?></div>
<?php }
add_action( 'woocommerce_admin_order_data_after_billing_address', 'customer_ean_edit' );
function customer_ean_save( $customer_id ){
update_user_meta( $customer_id->get_customer_id(), 'billing_eannr', sanitize_text_field( $_POST['billing_eannr'] ) );
}
add_action( 'personal_options_update', 'customer_ean_save' ); // edit own account admin
add_action( 'edit_user_profile_update', 'customer_ean_save' ); // edit other account admin
add_action( 'woocommerce_save_account_details', 'customer_ean_save' ); // edit WC account
改为尝试以下方法,其中显示在订单编辑页面中的字段和值被保存到订单元数据和用户元数据中……
您重访的代码:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'customer_ean_edit' );
function customer_ean_edit( $order ){
$value = get_user_meta( $order->get_customer_id(), 'billing_eannr', true );
?>
<div class="edit-address"><?php
woocommerce_wp_text_input( array(
'id' => 'billing_eannr',
'label' => __('EAN nr.:', 'woocommerce'),
'placeholder' => '',
'value' => $value,
'wrapper_class' => 'form-field-wide'
) );
?></div><?php
}
add_action('save_post_shop_order', 'customer_ean_save', 50, 3 );
function customer_ean_save( $post_id, $post, $update ) {
// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
if ( ! current_user_can( 'edit_shop_order', $post_id ) )
return $post_id;
if( isset($_POST['billing_eannr']) ) {
$order = wc_get_order( $post_id );
// Update order post meta data
update_post_meta( $post_id, 'billing_eannr', sanitize_text_field( $_POST['billing_eannr'] ) );
// Update user meta data
update_user_meta( $order->get_customer_id(), 'billing_eannr', sanitize_text_field( $_POST['billing_eannr'] ) );
}
}
代码进入活动子主题(或活动主题)的 function.php 文件。已测试并有效。
客户有一个名为 billing_ean
的额外字段作为其帐单地址的一部分。
我正在使用以下代码从管理面板的 Woocommerce 单一订单页面加载、编辑和保存他们的 EAN 编号。该代码在加载和编辑字段时完美运行,但在保存时(更新订单),它根本不会更新他们的客户信息。
function customer_ean_edit( $customer_id ){
$eannr = get_user_meta( $customer_id->get_customer_id(), 'billing_eannr', true );
?>
<div class="address">
<p<?php if( !$eannr ) echo ' class="none_set"' ?>>
<strong>EAN nr.:</strong>
<?php echo ( $eannr ) ? $eannr : 'Der er ikke angivet et EAN nr.' ?>
</p>
</div>
<div class="edit_address"><?php
woocommerce_wp_text_input( array(
'id' => 'billing_eannr',
'label' => 'EAN nr.:',
'value' => $eannr,
'wrapper_class' => 'form-field-wide'
) );
?></div>
<?php }
add_action( 'woocommerce_admin_order_data_after_billing_address', 'customer_ean_edit' );
function customer_ean_save( $customer_id ){
update_user_meta( $customer_id->get_customer_id(), 'billing_eannr', sanitize_text_field( $_POST['billing_eannr'] ) );
}
add_action( 'personal_options_update', 'customer_ean_save' ); // edit own account admin
add_action( 'edit_user_profile_update', 'customer_ean_save' ); // edit other account admin
add_action( 'woocommerce_save_account_details', 'customer_ean_save' ); // edit WC account
改为尝试以下方法,其中显示在订单编辑页面中的字段和值被保存到订单元数据和用户元数据中……
您重访的代码:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'customer_ean_edit' );
function customer_ean_edit( $order ){
$value = get_user_meta( $order->get_customer_id(), 'billing_eannr', true );
?>
<div class="edit-address"><?php
woocommerce_wp_text_input( array(
'id' => 'billing_eannr',
'label' => __('EAN nr.:', 'woocommerce'),
'placeholder' => '',
'value' => $value,
'wrapper_class' => 'form-field-wide'
) );
?></div><?php
}
add_action('save_post_shop_order', 'customer_ean_save', 50, 3 );
function customer_ean_save( $post_id, $post, $update ) {
// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
if ( ! current_user_can( 'edit_shop_order', $post_id ) )
return $post_id;
if( isset($_POST['billing_eannr']) ) {
$order = wc_get_order( $post_id );
// Update order post meta data
update_post_meta( $post_id, 'billing_eannr', sanitize_text_field( $_POST['billing_eannr'] ) );
// Update user meta data
update_user_meta( $order->get_customer_id(), 'billing_eannr', sanitize_text_field( $_POST['billing_eannr'] ) );
}
}
代码进入活动子主题(或活动主题)的 function.php 文件。已测试并有效。