PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 8, 2022

[FIXED] Determine the sequence of custom column in WooCommerce admin orders list

 January 08, 2022     orders, woocommerce, wordpress     No comments   

Issue

I've added a new custom column in the WooCommerce admin order list:

add_filter( 'manage_edit-shop_order_columns', 'custom_woo_columns_function' );
function custom_woo_columns_function( $columns ) {
    $new_columns = ( is_array( $columns ) ) ? $columns : array();
    

    // all of your columns will be added before the actions column
    $new_columns['storecode'] = '門市代碼';

    //stop editing
    
    return $new_columns;
}
// Change order of columns (working)
add_action( 'manage_shop_order_posts_custom_column', 'custom_woo_admin_value', 2 );
function custom_woo_admin_value( $column ) {
     
     $order_id = get_post($order_id)->ID;
     $CVSStoreID = get_post_meta($order_id, '_shipping_CVSStoreID', true);
    
    if ( $column == 'storecode' ) {
        echo ( isset( $CVSStoreID ) ? $CVSStoreID : '' );
    }
}

How should I move this column to the front?

enter image description here


Solution

Your current code will add the custom column at the end, this is because you add this column to the end of the existing $columns array.

To change this you can use array_slice, then it is just a matter of adjusting the $number variable to the desired position.

So you get:

// Display new column on WooCommerce admin orders list (header)
function filter_manage_edit_shop_order_columns( $columns ) {
    // Number (adapt to your needs)
    $number = 4;
    
    // Add new column after $number column
    return array_slice( $columns, 0, $number, true )
    + array( 'storecode' => __( '門市代碼', 'woocommerce' ) )
    + array_slice( $columns, $number, NULL, true );
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );

// Display details in the new column on WooCommerce admin orders list (populate the column)
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {    
    // Compare
    if ( $column == 'storecode' ) {
        // Get order
        $order = wc_get_order( $post_id );
    
        // Get meta
        $value = $order->get_meta( '_shipping_CVSStoreID' );
        
        // NOT empty
        if ( ! empty ( $value ) ) {
            echo ucfirst( $value );
        } else {
            echo __( 'N/A', 'woocommerce' );
        }
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );


Answered By - 7uc1f3r
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing