简体   繁体   English

在 WooCommerce 订单中显示 ACF 订单自定义字段

[英]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.

相关问题 在 WooCommerce 订单管理页面 (ACF) 显示产品自定义字段 - Display product custom field in WooCommerce Order admin Page (ACF) 按状态显示订单数量,并按Woocommerce中的自定义ACF字段显示 - Show number of orders by statuses and by custom ACF field in Woocommerce 在 WooCommerce 产品变体中显示 ACF 自定义字段 - Display ACF custom field in WooCommerce product variations 更新所有 WooCommerce 处理订单中的 ACF 字段 - Update an ACF field in all WooCommerce processing orders 在Woocommerce订单电子邮件中的ACF字段中显示值 - Display value from ACF field in Woocommerce order email 如何将 woocommerce 订单号更新为自定义 ACF 字段? (wordpress) - How to update woocommerce order number to a custom ACF field? (wordpress) Output WooCommerce 订单管理页面 (ACF) 中的产品自定义字段 - Output a product custom field in WooCommerce Order Admin Page (ACF) WooCommerce 在订单通知中显示高级自定义字段 (ACF) - WooCommerce Display avanced custom fields (ACF) inside order notification 仅在 WooCommerce 管理订单上显示自定义订单项元数据 - Display custom order item metadata only on WooCommerce admin orders 在Woocommerce订单编辑视图中显示自定义字段值 - Display a custom-field value in Woocommerce orders edit view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM