在 Woocommerce 管理员、订单和电子邮件中显示自定义支付字段
Display custom payment field in Woocommerce Admin, Orders and emails
我需要在管理订单页面、thank_you 和电子邮件通知中显示我的自定义结帐字段。
我正在使用 答案代码来显示、验证和保存我的自定义字段。
来自 我正在尝试显示我的自定义字段保存的输入值。
这是我目前的代码:
// Display field value on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address',
'show_Ean_nummer_in_admin', 10, 1 );
function show_Ean_nummer_in_admin ( $order ){
// Get "ean" custom field value
$udfyld_ean = get_post_meta( $order_id, '_udfyld_ean', true );
// Display "ean" custom field value
echo '<p>'.__('EAN', 'woocommerce') . $udfyld_ean . '</p>';
}
// Display field value on the order emails
add_action( 'woocommerce_email_order_details', 'ean_in_emails' 50, 1 );
function ean_in_emails( $order, $sent_to_admin, $plain_text, $email ){
// Get "ean" custom field value
$udfyld_ean = get_post_meta( $order_id, '_udfyld_ean', true );
// Display "ean" custom field value
echo '<p>'.__('EAN', 'woocommerce') . $udfyld_ean . '</p>';
}
// Display field value in thank you
add_action( 'woocommerce_thankyou', 'ean_in_thankyou' );
function ean_in_thankyou() {
// Get "ean" custom field value
$udfyld_ean = get_post_meta( $order_id, '_udfyld_ean', true );
// Display "ean" custom field value
echo '<p>'.__('EAN', 'woocommerce') . $udfyld_ean . '</p>';
}
但是它不起作用。该字段确实附加到数据库,但不显示在任何地方:
如何正确显示字段?
您正在使用以下代码获取 _udfyld_ean
的值:
get_post_meta( $order_id, '_udfyld_ean', true );
但问题是,你没有在任何地方定义$order_id
。您需要传递订单 ID 的有效值才能获得预期的输出。
以下代码将在订单和电子邮件通知中显示您的 "Udfyld EAN" 自定义字段值:
1) 在 Woocommerce 订单管理单页中显示:
// Display field value on the admin order edit page
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'custom_field_admin_display_order_meta', 10, 1 );
function custom_field_admin_display_order_meta( $order ){
$business_address = get_post_meta( $order->get_id(), 'Business Address?', true );
if( $udfyld_ean = $order->get_meta('_udfyld_ean') )
echo '<p><strong>'.__('Udfyld EAN', 'woocommerce').': </strong> ' . $udfyld_ean . '</p>';
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
2) 要在收到的订单、订单视图和电子邮件通知中显示,您将使用:
add_filter( 'woocommerce_get_order_item_totals', 'add_udfyld_ean_row_to_order_totals', 10, 3 );
function add_udfyld_ean_row_to_order_totals( $total_rows, $order, $tax_display ) {;
$new_total_rows = [];
foreach($total_rows as $key => $total ){
$new_total_rows[$key] = $total;
if( $order->get_meta('_udfyld_ean') && 'payment_method' === $key ){
$new_total_rows['udfyld_ean'] = array(
'label' => __('Udfyld EAN', 'woocommerce'),
'value' => esc_html( $order->get_meta('_udfyld_ean') ),
);
}
}
return $new_total_rows;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
我需要在管理订单页面、thank_you 和电子邮件通知中显示我的自定义结帐字段。
我正在使用
来自
这是我目前的代码:
// Display field value on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address',
'show_Ean_nummer_in_admin', 10, 1 );
function show_Ean_nummer_in_admin ( $order ){
// Get "ean" custom field value
$udfyld_ean = get_post_meta( $order_id, '_udfyld_ean', true );
// Display "ean" custom field value
echo '<p>'.__('EAN', 'woocommerce') . $udfyld_ean . '</p>';
}
// Display field value on the order emails
add_action( 'woocommerce_email_order_details', 'ean_in_emails' 50, 1 );
function ean_in_emails( $order, $sent_to_admin, $plain_text, $email ){
// Get "ean" custom field value
$udfyld_ean = get_post_meta( $order_id, '_udfyld_ean', true );
// Display "ean" custom field value
echo '<p>'.__('EAN', 'woocommerce') . $udfyld_ean . '</p>';
}
// Display field value in thank you
add_action( 'woocommerce_thankyou', 'ean_in_thankyou' );
function ean_in_thankyou() {
// Get "ean" custom field value
$udfyld_ean = get_post_meta( $order_id, '_udfyld_ean', true );
// Display "ean" custom field value
echo '<p>'.__('EAN', 'woocommerce') . $udfyld_ean . '</p>';
}
但是它不起作用。该字段确实附加到数据库,但不显示在任何地方:
如何正确显示字段?
您正在使用以下代码获取 _udfyld_ean
的值:
get_post_meta( $order_id, '_udfyld_ean', true );
但问题是,你没有在任何地方定义$order_id
。您需要传递订单 ID 的有效值才能获得预期的输出。
以下代码将在订单和电子邮件通知中显示您的 "Udfyld EAN" 自定义字段值:
1) 在 Woocommerce 订单管理单页中显示:
// Display field value on the admin order edit page
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'custom_field_admin_display_order_meta', 10, 1 );
function custom_field_admin_display_order_meta( $order ){
$business_address = get_post_meta( $order->get_id(), 'Business Address?', true );
if( $udfyld_ean = $order->get_meta('_udfyld_ean') )
echo '<p><strong>'.__('Udfyld EAN', 'woocommerce').': </strong> ' . $udfyld_ean . '</p>';
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
2) 要在收到的订单、订单视图和电子邮件通知中显示,您将使用:
add_filter( 'woocommerce_get_order_item_totals', 'add_udfyld_ean_row_to_order_totals', 10, 3 );
function add_udfyld_ean_row_to_order_totals( $total_rows, $order, $tax_display ) {;
$new_total_rows = [];
foreach($total_rows as $key => $total ){
$new_total_rows[$key] = $total;
if( $order->get_meta('_udfyld_ean') && 'payment_method' === $key ){
$new_total_rows['udfyld_ean'] = array(
'label' => __('Udfyld EAN', 'woocommerce'),
'value' => esc_html( $order->get_meta('_udfyld_ean') ),
);
}
}
return $new_total_rows;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。