在 WooCommerce 管理订单列表列中显示复合订单数据

时间:2021-04-06 12:15:04

标签: php wordpress woocommerce crud orders

我的订单包含我想在订单页面上查看的信息。 但是这些信息并没有存储为元数据,有没有办法获取这些信息?

我想获取的信息: Information that I want to get

信息在代码中的存储位置: Where the information is stored in the code

我尝试了一些东西,但总是出现错误,这就是我尝试过的:

    // ADDING 2 NEW COLUMNS WITH THEIR TITLES (keeping "Total" and "Actions" columns at the end)
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column_another', 20 );
function custom_shop_order_column_another($columns)
{
    $reordered_columns = array();

    // Inserting columns to a specific location
    foreach( $columns as $key => $column){
        $reordered_columns[$key] = $column;
        if( $key ==  'order_status' ){
            // Inserting after "Status" column
            $reordered_columns['my-column3'] = __( 'Wefact email','theme_domain');
            $reordered_columns['my-column4'] = __( 'Wefact status','theme_domain');
        }
    }
    return $reordered_columns;
}


// Adding custom fields meta data for each new column (example)
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_Another_content', 20, 2 );
function custom_orders_list_column_Another_content( $column, $post_id )
{
    switch ( $column )
    {
        case 'my-column3' :
            // Get custom post meta data
            $my_var_one = get_post_meta( $post_id, 'WeFact_email', true );
            if(!empty($my_var_one))
                echo $my_var_one;

            // Testing (to be removed) - Empty value case
            else
                echo '<small>(<em>no value</em>)</small>';

            break;

        case 'my-column4' :
            // Get custom post meta data
        $wfInvoiceID = (int) get_post_meta( $post_id, '_wefact_invoice_id', true);
        
        if (isset($wfInvoiceID)) :
        $invoice = new WeFactInvoice();
        $wfInvoice = $invoice->showByID($wfInvoiceID);
        
        if ($wfInvoice['status'] == 'success') :
        
        $status = [
            "0" => "Concept factuur",
            "1" => "Wachtrij factuur",
            "2" => "Verzonden",
            "3" => "Deels betaald",
            "4" => "Betaald",
            "8" => "Creditfactuur",
            "9" => "Vervallen",
        ];

            if(!empty($wfInvoiceID))
                echo $status[$wfInvoice['invoice']['Status']];

            // Testing (to be removed) - Empty value case
            else
                echo '<small>(<em>no value</em>)</small>';

            break;  
        endif;
    }
}

1 个答案:

答案 0 :(得分:2)

尝试以下重新访问的代码(在 WC_Data 对象上使用 get_meta() 方法 WC_Order

// ADDING 2 NEW COLUMNS WITH THEIR TITLES (keeping "Total" and "Actions" columns at the end)
add_filter( 'manage_edit-shop_order_columns', 'add_custom_wefact_shop_order_columns', 20 );
function add_custom_wefact_shop_order_columns( $columns ) {
    $reordered_columns = array();

    // Inserting columns to a specific location
    foreach( $columns as $key => $column ){
        $reordered_columns[$key] = $column;
        if( $key ==  'order_status' ){
            // Inserting after "Status" column
            $reordered_columns['wefact-email'] = __( 'Wefact email', 'theme_domain');
            $reordered_columns['wefact-status'] = __( 'Wefact status', 'theme_domain');
        }
    }
    return $reordered_columns;
}


// Adding custom fields meta data for each new column
add_action( 'manage_shop_order_posts_custom_column' , 'custom_wefact_shop_order_columns_content', 20, 2 );
function custom_wefact_shop_order_columns_content( $column, $post_id ) {
    global $post, $the_order;

    $order = is_a($the_order, 'WC_Order') ? $the_order : wc_get_order($post_id);

    if ( 'wefact-email' === $column ) {
        $wefact_email = $order->get_meta('WeFact_email'); // Get order custom field

        echo empty($wefact_email) ? '<small>(<em>no value</em>)</small>' : $wefact_email;
    }

    elseif( 'wefact-status' === $column ) {
        $wfInvoiceID  = $order->get_meta('_wefact_invoice_id'); // Get order custom field
        $wfInvoiceID  = empty($wfInvoiceID) ? $order->get_meta('_order_wefact_invoice_id') : $wfInvoiceID; // Get order custom field
        $value_output = '';

        if ( ! empty($wfInvoiceID) && class_exists('WeFactInvoice') ) {
            $invoice = new WeFactInvoice();
            $wfInvoice = $invoice->showByID($wfInvoiceID);

            if ($wfInvoice['status'] == 'success') {
                $status = [
                    "0" => "Concept factuur",
                    "1" => "Wachtrij factuur",
                    "2" => "Verzonden",
                    "3" => "Deels betaald",
                    "4" => "Betaald",
                    "8" => "Creditfactuur",
                    "9" => "Vervallen",
                ];

                if( isset($wfInvoice['invoice']['Status']) && isset($status[$wfInvoice['invoice']['Status']]) ) {
                    $value_output = $status[$wfInvoice['invoice']['Status']];
                }
            }
        }
        echo empty($value_output) ? '<small>(<em>no value</em>)</small>' : $value_output;
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。