Add custom columns to admin products list in WooCommerce 3

前端 未结 1 1027
囚心锁ツ
囚心锁ツ 2021-01-03 12:05

Is it possible to add a column \"delivery time\" to the Woocommerce admin product list?

I know there are some additional columns (thumb, price, product_cat etc) to c

相关标签:
1条回答
  • 2021-01-03 13:08

    Here is the way to do it with that 2 custom functions hooked. The first one create the column with the title, the second one populate the column with the products data. But you will need to set in that second function, the correct corresponding meta_key to get the data.

    Here is that code:

    // ADDING A CUSTOM COLUMN TITLE TO ADMIN PRODUCTS LIST
    add_filter( 'manage_edit-product_columns', 'custom_product_column',11);
    function custom_product_column($columns)
    {
       //add columns
       $columns['delivery'] = __( 'Delivery time','woocommerce'); // title
       return $columns;
    }
    
    // ADDING THE DATA FOR EACH PRODUCTS BY COLUMN (EXAMPLE)
    add_action( 'manage_product_posts_custom_column' , 'custom_product_list_column_content', 10, 2 );
    function custom_product_list_column_content( $column, $product_id )
    {
        global $post;
    
        // HERE get the data from your custom field (set the correct meta key below)
        $delivery_time = get_post_meta( $product_id, '_delivery_time', true );
    
        switch ( $column )
        {
            case 'delivery' :
                echo $delivery_time; // display the data
                break;
        }
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested and works.


    How to get the correct meta_key slug:

    To find the correct meta_key slug corresponding to the "delivery time", you should need to make search in your database using PhpMyAdmin. You will have to search for delivery term in wp_postmeta table this way:

    Then you will get this kind of results (here there is just 1 line with a fake slug):

    So now you should be able to get the correct slug name (like this fake "_delivery_date" one)


    Related answer (for orders): Add custom columns to admin orders list in WooCommerce backend

    0 讨论(0)
提交回复
热议问题