Problem of refresh of Adding Columns to admin list - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Saturday, December 11, 2021

Problem of refresh of Adding Columns to admin list

I am new in here. I try to add a column in the woocommerce Admin order list page according to a previous question in here. I want to retrieve certain order status from it whether it is completed or processing with $wpdb (this part is still under development). The code works well when I inserted in theme functions.php (in the last part). But when I change some of the order status from processing to completed, the column I added didn't change correspondingly but only change if I save the theme functions.php one more time. Is this easily solved by making it independently as a plugin ?or other code like forcing it to execute everytime when a order status changed.

The code I used

add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
function custom_shop_order_column($columns)
{
    $reordered_columns = array();

    // Inserting columns to a specific location
    foreach( $columns as $key => $column){
        $reordered_columns[$key] = $column;
        if( $key ==  'order_status' ){
            // Inserting after "Status" column
            $reordered_columns['my-column1'] = __( 'Invoice','theme_domain');
           
        }
    }
    return $reordered_columns;
}

// Adding custom fields meta data for each new column (example)
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
    global $wpdb;

    switch ( $column )
    {
        case 'my-column1' :
    // Get custom post meta data
    $sql = 'SELECT * FROM wp_wc_order_stats WHERE order_id="'.$post_id.'"';
    $order_stats = $wpdb->get_results($sql);
            
    foreach ($order_stats as $status) {
    $reorder_stats = $status->status;
   
}
  if($reorder_stats == 'wc-cancelled')
        echo 'Cancelled';

             // Testing (to be removed) - Empty value case
    else
        echo $reorder_stats;
   

        break;
    }    
         
}


source https://stackoverflow.com/questions/70309586/problem-of-refresh-of-adding-columns-to-admin-list

No comments:

Post a Comment