Add a WooCommerce Orders List Column and value

本小妞迷上赌 提交于 2019-12-23 02:42:44

问题


In woo commerce order page(Admin Side) I want to add Drop Shipping column in order list

which I done through

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_total' ){

            $reordered_columns['drop_shipping'] = __( 'Drop Shipping','twentyseventeen');

        }
    }
    return $reordered_columns;                                       
}

It works

Now I want to show populate data in that field

I found the solution from here

I follow same step as mention but I cant get the value

add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
    //echo $column;
    switch ( $column )
    {
        case 'drop_shipping' :
            // Get custom post meta data

            $my_var_one = get_post_meta( $post_id, 'drop_shipping', true );

            if(!empty($my_var_one))
                echo $my_var_one;

            // Testing (to be removed) - Empty value case
            else
                echo '<small>(<em>no value</em>)</small>';

          break;                        
    } 

}

Also I check wp_postmeta table but there is no result found..

Can you please tell me where is I done mistake and how to add value in drop_shipping

Thank You.


回答1:


As per Suggestion I got my mistake, I add _drop_shipping in wp_postmeta

add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
    switch ( $column )
    {
        case 'drop_shipping' :


              if(get_post_meta( $post_id, '_drop_shipping', true )){
                   $my_var_one = get_post_meta( $post_id, '_drop_shipping', true );
                   echo $my_var_one;
              }
              else{         

                   add_post_meta($post_id, '_drop_shipping', $post_id); 
                   $my_var_one = get_post_meta( $post_id, '_drop_shipping', true );
                   echo $my_var_one;
              }

        break;
    }

}


来源:https://stackoverflow.com/questions/49919915/add-a-woocommerce-orders-list-column-and-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!