【问题标题】:Show custom fields in WooCommerce quick order preview在 WooCommerce 快速订单预览中显示自定义字段
【发布时间】:2019-09-08 23:41:07
【问题描述】:

在 WooCommerce 管理订单列表中,单击“眼睛图标”可以快速预览订单信息。

我添加了自定义结算结帐字段,但它们未显示在此快速预览中,而是在结算详细信息下显示“N/A”:

但是在选择编辑订单页面时,我可以看到它们。

如何在订单快速预览中显示该计费自定义字段?

【问题讨论】:

    标签: php wordpress woocommerce metadata orders


    【解决方案1】:

    在下面的代码中,您必须为每个结算自定义字段设置正确的元键。它将在“计费”部分下的快速订单预览中显示您的计费自定义字段:

    add_filter( 'woocommerce_admin_order_preview_get_order_details', 'admin_order_preview_add_custom_billing_data', 10, 2 );
    function admin_order_preview_add_custom_billing_data( $data, $order ) {
        $custom_billing_data = []; // initializing
    
        // Custom field 1: Replace '_custom_meta_key1' by the correct custom field metakey
        if( $custom_value1 = $order->get_meta('_custom_meta_key1') ) {
            $custom_billing_data[] = $custom_value1;
        }
    
        // Custom field 2: Replace '_custom_meta_key1' by the correct custom field metakey
        if( $custom_value2 = $order->get_meta('_custom_meta_key1') ) {
            $custom_billing_data[] = $custom_value2;
        }
    
        ## ……… And so on (for each additional custom field).
    
        // Check that our custom fields array is not empty
        if( count($custom_billing_data) > 0 ) {
            // Converting the array in a formatted string
            $formatted_custom_billing_data = implode( '<br>', $custom_billing_data );
    
            if( $data['formatted_billing_address'] === __( 'N/A', 'woocommerce' ) ) {
                $data['formatted_billing_address'] = $formatted_custom_billing_data;
            } else {
                $data['formatted_billing_address'] .= '<br>' . $formatted_custom_billing_data;
            }
        }
    
        return $data;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。它应该可以工作。

    相关:Display custom data on Woocommerce admin order preview

    【讨论】:

    • 你是我的英雄,非常感谢!! ..但这也可以应用于计费列
    • @Ibrahim 您的意思是运输(因为它实际上是计费)...为此,您需要复制函数内的所有代码,除了您在末尾保留的return $data;...然后将“计费”一词更改为仅在此副本中到处运送。您必须为自定义运输字段设置正确的元键。
    猜你喜欢
    • 2017-05-10
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 2021-04-28
    • 2018-03-18
    • 2020-09-29
    • 1970-01-01
    • 2019-01-31
    相关资源
    最近更新 更多