Add a Purchased Products Column to the WooCommerce Admin Orders List

WooCommerce is the most popular ecommerce plugin in the WordPress ecosystem. One reason is its rich API and its convenient, flexible admin interface, which makes WooCommerce custom development much easier. In the WooCommerce admin area, we can already view detailed order information very conveniently, including the quantity of purchased items, the order total, and other important details. But one very important piece of information is not shown by default: the full product list in the order. That list is extremely useful, and it can be displayed with a very small amount of code.

The code below adds a special column to the order list page. The new column is called “Purchased Products”, and each row in that column shows a product name together with the purchased quantity. Copy the code below into your theme’s functions.php file or into the relevant file in your plugin.

add_filter( 'manage_edit-shop_order_columns', 'show_product_order',15 );
function show_product_order($columns){

//添加一列信息
$columns['product-display'] = __( '购买的商品');
  return $columns;
}

Add the products purchased by the user to that column.

add_action( 'manage_shop_order_posts_custom_column' , 'snv_custom_shop_order_column', 10, 2 );
function snv_custom_shop_order_column( $column ) {
 global $post, $woocommerce, $the_order;

    switch ( $column ) {
        case 'product-display' :
          $terms = $the_order->get_items();

          if ( is_array( $terms ) ) {
                foreach($terms as $term)
        {
        echo $term['item_meta']['_qty'][0] .' x ' . $term['name'] .'';
        }
              } else {
                _e( '获取商品信息失败。', 'woocommerce' );
        }
            break;

    }
}

Here is how the code works:

  • The show_product_order filter adds a new column to the WooCommerce order management interface. The column appears after the other information columns.
  • The snv_custom_shop_order_column action hooks into manage_shop_order_posts_custom_column to query the products purchased by the user and display the product names and quantities in the “Purchased Products” column we added above.

In addition to the products purchased by the user, we can add other information to the order list page as needed, such as contact details or a shipping address. With WordPress and WooCommerce providing rich APIs, the only real limit is what we think to build.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *