【问题标题】:Display composite order data in WooCommerce admin Orders List column在 WooCommerce 管理员订单列表列中显示复合订单数据
【发布时间】:2021-04-06 12:15:04
【问题描述】:

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

我想获取的信息:

信息在代码中的存储位置:

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

    // 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;
    }
}

【问题讨论】:

    标签: php wordpress woocommerce crud orders


    【解决方案1】:

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

    // 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 文件中。它应该可以工作。

    【讨论】:

    • 这行得通!现在只有订单概览的加载速度是原来的两倍。但从本质上讲,代码是有效的。谢谢!
    猜你喜欢
    • 2018-08-08
    • 2020-10-12
    • 2020-11-11
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多