Show stock remaining per product in a custom column in the admin orders list in WooCommerce

Show stock remaining per product in a custom column in the admin orders list in WooCommerce on child sites.

Snippet Type

Execute on Child Sites

Snippet

// Add a Header
function filter_manage_edit_shop_order_columns( $columns ) {
    // Add new column
    $columns['order_products'] = __( 'Products', 'woocommerce' );

    return $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 an instance of the WC_Order object from an Order ID
        $order = wc_get_order( $post_id );
        
        // Is a WC_Order
        if ( is_a( $order, 'WC_Order' ) ) {
            foreach( $order->get_items() as $item ) {
                // Product ID
                $product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
                
                // Get product
                $product = wc_get_product( $product_id );
                
                // Get stock quantity
                $get_stock_quantity = $product->get_stock_quantity();
                
                // NOT empty
                if ( ! empty ( $get_stock_quantity ) ) {
                    $stock_output = ' (' . $get_stock_quantity . ')';
                } else {
                    $stock_output = '';
                }
                
                // Output
                echo '▪ <a href="' . admin_url( 'post.php?post=' . $item->get_product_id() . '&action=edit' ) . '">'.  $item->get_name() . '</a> × ' . $item->get_quantity() . $stock_output . '<br />';
           }
        }
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.