0

How can I add Product Name on Admin Panel Order list and User's My-Account Order List?

enter image description here

1 Answer 1

3

Add this code to your functions.php this will add an extra column to the listing table with Product name. Like 1 x Netflix Premium. If multiple that will be visible too.

add_filter('manage_edit-shop_order_columns', '_order_items_column' );
function _order_items_column( $order_columns ) {
    $order_columns['order_products'] = "Product Name";
    return $order_columns;
}
 

add_action( 'manage_shop_order_posts_custom_column' , '_order_items_column_cnt' );
function _order_items_column_cnt( $colname ) {
    global $the_order; // the global order object
 
    if( $colname == 'order_products' ) {
 
        // get items from the order global object
        $order_items = $the_order->get_items();
 
        if ( !is_wp_error( $order_items ) ) {
            foreach( $order_items as $order_item ) {
 
                echo $order_item['quantity'] .' × <a href="' . admin_url('post.php?post=' . $order_item['product_id'] . '&action=edit' ) . '">'. $order_item['name'] .'</a><br />';
                // you can also use $order_item->variation_id parameter
                // by the way, $order_item['name'] will display variation name too
 
            }
        }
 
    }
 
}

Now normally the column will be added as last column. We have to rearrange the column order. Let's modify _order_items_column function like bellow to make it move at the desired position.

function _order_items_column( $order_columns ) {

    $new_columns = array();
    foreach ( $order_columns as $key => $name ) {

        $new_columns[ $key ] = $name;

        // add ship-to after order status column
        if ( 'order_number' === $key ) {
            $new_columns['order_products'] = __( 'Product Name', 'textdomain' );
        }
    }

    return $new_columns;
}

Now it should appear right after Order ID.

To add this data in My Orders list use following code.

add_filter( 'woocommerce_my_account_my_orders_columns', 'additional_my_account_orders_column', 10, 1 );
function additional_my_account_orders_column( $columns ) {
    $new_columns = [];

    foreach ( $columns as $key => $name ) {
        $new_columns[ $key ] = $name;

        if ( 'order-number' === $key ) {
            $new_columns['order-items'] = __( 'Product Name', 'woocommerce' );
        }
    }
    return $new_columns;
}

add_action( 'woocommerce_my_account_my_orders_column_order-items', 'additional_my_account_orders_column_content', 10, 1 );
function additional_my_account_orders_column_content( $order ) {
    $details = array();

    foreach( $order->get_items() as $item )
        $details[] = $item->get_quantity() . '&nbsp;&times;&nbsp;' . $item->get_name();

    echo count( $details ) > 0 ? implode( '<br>', $details ) : '&ndash;';
}
2
  • bhai. Thanks a lot. It's working. <3
    – ashik4u
    Commented Jun 28, 2020 at 11:43
  • You are welcome! Commented Jun 28, 2020 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.