[英]Display ACF order custom field in WooCommerce orders
Need to add a field to the WooCommerce order details page where I can add a tracking number in the order page to be displayed for the customer.需要在 WooCommerce 订单详细信息页面添加一个字段,我可以在订单页面中添加一个跟踪号以显示给客户。
I have added ACF to the order-details.php template as below, but it does not display.我已将 ACF 添加到 order-details.php 模板中,如下所示,但它不显示。 When I inspect page element all I can see is <h2></h2>
.当我检查页面元素时,我只能看到<h2></h2>
。 This is my current ACF code:这是我当前的 ACF 代码:
<h2><?php the_field('tracking_number'); ?></h2>
You can use the following to dis您可以使用以下方法来dis
// Display tracking information as a row on order totals everywhere
add_filter( 'woocommerce_get_order_item_totals', 'order_item_totals_tracking_row', 1000, 3 );
function order_item_totals_tracking_row( $total_rows, $order, $tax_display ){
if( $tracking_number = get_field('tracking_number', $order->get_id()) ) {
$new_total_rows = []; // Initializing
$has_tracking_row = false; // Initializing
$tracking_row = array( // tracking information array
'label' => __("Tracking number", "woocommerce"),
'value' => $tracking_number
);
// Loop through order total rows
foreach( $total_rows as $key => $values ) {
$new_total_rows[$key] = $values;
// Inserting tracking information array
if( $key === 'shipping' ) {
$new_total_rows['tracking'] = $tracking_row;
$has_tracking_row = true;
} elseif( $key === 'payment_method' && ! $has_tracking_row ) {
$new_total_rows['tracking'] = $tracking_row;
}
}
return $new_total_rows;
}
return $total_rows;
}
Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 It should works.它应该有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.