user14906105
user14906105

Reputation:

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

I am trying to display a column on the Orders page (admin). In that column, show each product in the order.

The idea is to be able to quickly see the products that have been ordered.

Adding the column works fine, but getting the products is not working. All I get is Fatal error: Uncaught ArgumentCountError

Here's is the code:

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) : '–';
}

I don't know how to fix it, so if anyone can give advice please let me know

Upvotes: 1

Views: 563

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29624

Your code has some minor bugs, for example:

  • You indicate that the products_column_show_products() callback function expects 3 arguments, while the manage_{$post->post_type}_posts_custom_column WordPress hook contains 2 parameters. The $column_name and the $post_id
  • There is no need to use global $order;, via the $post_id, you can access the order with wc_get_order( $post_id );

So this should suffice:

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

Upvotes: 1

Related Questions