簡體   English   中英

在 WooCommerce 管理訂單列表中添加新列時顯示產品名稱的問題

[英]Issue when adding a new column in WooCommerce admin order list displaying product names

我正在嘗試在“訂單”頁面(管理員)上顯示一列。 在該列中,按順序顯示每個產品。

這個想法是能夠快速查看已訂購的產品。

添加列工作正常,但獲取產品不起作用。 我得到的只是Fatal error: Uncaught ArgumentCountError

這是代碼:

add_filter( 'manage_edit-shop_order_columns', 'products_column_create_products', 20 );
function products_column_create_products( $columns ) {

    $reordered_columns = array();

        foreach( $columns as $key => $column ) {

            $reordered_columns[$key] = $column;

            if ( $key == 'order_number' ) {

                $reordered_columns['products'] = __( 'Products','woocommerce' );

            }
        }

    return $reordered_columns;
}

add_action( 'manage_shop_order_posts_custom_column' , 'products_column_show_products', 20, 3 );
function products_column_show_products($column, $post_id, $order){

    if ('products' != $column) return;

    global $order;

        $details = array();

            foreach($order->get_items() as $item)

            $details[] = '<a href="' . $item->get_product()->get_permalink() . '">' . $item->get_name() . '</a>  × ' . $item->get_quantity() .'<br>';

        echo count($details) > 0 ? implode('<br>', $details) : '–';
}

我不知道如何解決它,所以如果有人可以提供建議,請告訴我

您的代碼有一些小錯誤,例如:

  • 您指出products_column_show_products()回調 function 需要 3 個 arguments,而manage_{$post->post_type}_posts_custom_column ZFDE31686817FE5029021B586 鈎子包含 2 個參數。 $column_name$post_id
  • 無需使用global $order; ,通過$post_id ,您可以使用wc_get_order( $post_id );

所以這應該足夠了:

// Add a Header
function filter_manage_edit_shop_order_columns( $columns ) {
    // Loop trough existing columns
    foreach ( $columns as $key => $name ) {

        $new_columns[ $key ] = $name;

        // Add after order status column
        if ( $key === 'order_number' ) {
            $new_columns['order_products'] = __( 'Products', 'woocommerce' );
        }
    }

    return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );

// Populate the Column
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
    // Compare
    if ( $column == 'order_products' ) {
        // Get order
        $order = wc_get_order( $post_id );
        
        echo '<ul>';
    
        // Going through order items
        foreach ( $order->get_items() as $item ) {
            // Get product object
            $product = $item->get_product();
            
            echo '<li><a href="' . $product->get_permalink() . '">' . $item->get_name() . '</a> &times; ' . $item->get_quantity() . '</li>';
        }
        
        echo '</ul>';
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM