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

标签 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;
    }
}

最佳答案

尝试以下重新访问的代码(在 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 文件中。它应该可以工作。

关于php - 在 WooCommerce 管理订单列表列中显示复合订单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66968469/

相关文章:

php - 如果我尝试在其中存储太多数据,我的数据属性将为空

php - WordPress - 如果将 get_option 用作变量,则为 get_option 添加默认值

javascript - 如何通过 FTP 客户端将单独的 HTML 文件上传到 wordpress 站点?

php - 根据 WooCommerce 结帐字段值显示/隐藏送货方式

php - 使用 wordpress woocommerce 创建自定义 'Add to cart'

php - 隐藏产品价格并禁用 Woocommerce 中特定产品类别的添加到购物车

php - FFMPEG 无法取帧并另存为图像

php - 如何在 symfony 2 中最好地构建应用程序

php - AES CryptoJS 加密和 phpseclib 解密

php - wp_localize_script $data 数组中的参数