Home > database >  Add multiple custom columns (notes and VAT) on WooCommerce admin orders list
Add multiple custom columns (notes and VAT) on WooCommerce admin orders list

Time:10-22

I have already inserted this snippet to show an additional column with an icon if there are customer notes in the order. it works.

add_action( 'wp_enqueue_scripts', 'mini_enqueue_scripts' );

add_filter( 'manage_shop_order_posts_columns', 'woocommerce_add_order_notes_column', 99 );
function woocommerce_add_order_notes_column( $columns ) {
    $columns['order_notes'] = __('Customer note', 'woocommerce');
    return $columns;
}

add_action( 'manage_shop_order_posts_custom_column' , 'woocommerce_show_order_notes_column', 10, 2 );
function woocommerce_show_order_notes_column( $column_name, $order_id ) {
    switch ( $column_name ) {
        case 'order_notes':
            $order = wc_get_order( $order_id );
            $note = $order->get_customer_note();
            if ( !empty($note) ) {
                echo '<span  data-tip="' . wc_sanitize_tooltip( $note ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
            } else {
                echo '<span >&ndash;</span>';
            }
            break;
    }
}

How can I create another column (if the VAT number has been entered in the order) on the side that does the same thing?

For that I have two additional fields on the checkout page: "vat number" (billing_piva) and "CID number" (billing_cid). I would like to have them together on the same column with two headers first, like:

  • "Vat Number: xxx" and "CID Number: xxx"

Any advice?

CodePudding user response:

To add 2 columns versus 1 you can actually apply the same as you already did for adding 1 column

Adding the VAT number to the order can be done in different ways, from your question I understand that this is a custom checkout field with the meta key: billing_piva

Note: the use of add_action( 'wp_enqueue_scripts', 'mini_enqueue_scripts' ); is not necessary

So you get:

// Display on order admin list (header)
function filter_manage_edit_shop_order_columns( $columns ) {
    // Add columns
    $columns['order_notes'] = __( 'Customer note', 'woocommerce' );
    $columns['order_vat'] = __( 'VAT number', 'woocommerce' );

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

// Display on order admin list (populate the column)
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
    // Get order
    $order = wc_get_order( $post_id );
    
    // Is a WC_Order
    if ( is_a( $order, 'WC_Order' ) ) {
        // Compare
        switch ( $column ) {
            case 'order_notes':
                // Get customer note
                $note = $order->get_customer_note();
                
                // NOT empty
                if ( ! empty( $note ) ) {
                    echo '<span  data-tip="' . wc_sanitize_tooltip( $note ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
                } else {
                    echo '<span >&ndash;</span>';
                }
                
                break;
            case 'order_vat':
                // Get VAT (if necessary, adjust to the correct meta key)
                $vat_number = $order->get_meta( 'billing_piva' );
                
                // NOT empty
                if ( ! empty( $vat_number ) ) {
                    // Output
                    $output = '<span>' . sprintf( __( 'VAT Number: %s', 'woocommerce' ), $vat_number ) . '</span>';
                    
                    // Get CID number
                    $cid_number = $order->get_meta( 'billing_cid' );
                    
                    // NOT empty 
                    if ( ! empty ( $cid_number ) ) {
                        // Concatenation 
                        $output .= '<br><span>' . sprintf( __( 'CID Number: %s', 'woocommerce' ), $cid_number ) . '</span>';
                    }
                    
                    // Print
                    echo $output;
                } else {
                    echo '<span >&ndash;</span>';
                }               
        }
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );
  • Related