• Resolved pmoignard

    (@pmoignard)


    Hi everyone, thanks in advance for your help. I’m attempting to add the value of a custom product field that I’ve created to a custom column in the WooCommerce “Orders” admin table. I’ve got the column itself setup, and am able to get the value via the individual product/post ID, but haven’t been able to figure out how to get the product ID from each product dynamically to pass into my “get_post_meta()” function to output each relevant value into the “Orders” table. Here’s my code so far:

    //Add 'Course Date' column header to 'Orders' page after 'Total' column.
         
        function add_order_coursedates_column_header( $columns ) {
            $new_columns = array();
                foreach ( $columns as $column_name => $column_info ) {
                $new_columns[ $column_name ] = $column_info;
                if ( 'order_total' === $column_name ) {
                    $new_columns['order_course_dates'] = __( 'Course Dates', 'my-textdomain' );
                }
            }
            return $new_columns;
        }
        add_filter( 'manage_edit-shop_order_columns', 'add_order_coursedates_column_header', 20 );
        
        // Add Course Dates to new column
        function add_order_coursedates($column_info) {
            if($column_info == 'order_course_dates') {
                $the_product_id = '5316';
                $date_field = 'wccaf_gLUNQXKdWIcO';
                $course_dates = get_post_meta($the_product_id, $date_field, true);
                echo $course_dates;
            }
        }
        add_action('manage_shop_order_posts_custom_column', 'add_order_coursedates', 99);
Viewing 11 replies - 1 through 11 (of 11 total)
  • You use the hook manage_{$post->post_type}_posts_custom_column with only one parameter. Check the manual to see what the 2nd parameter provides ??
    https://developer.www.ads-software.com/reference/hooks/manage_post-post_type_posts_custom_column/

    Thread Starter pmoignard

    (@pmoignard)

    @threadi Thanks so much for your response. A bit confused at how to implement that. Do you mind giving me a quick example? I tried:

    function add_order_coursedates($column_info, $the_product_id) {
            if($column_info == 'order_course_dates') {
                $date_field = 'wccaf_gLUNQXKdWIcO';
                $course_dates = get_post_meta($the_product_id, $date_field, true);
                echo $course_dates;
            }
        }
        add_action('manage_shop_order_posts_custom_column', 'add_order_coursedates', 99);

    And it just gave me a fatal error?

    Pay attention to the specification of the parameters at the end of add_filter. In this case, there must be a 2 for 2 parameters:

    add_action('manage_shop_order_posts_custom_column', 'add_order_coursedates', 99, 2);

    Thread Starter pmoignard

    (@pmoignard)

    Ah. Ok… I did that, but I still don’t get any values returned in my column?

    This is what I’ve got now:

    // Add Course Dates to new column
        function add_order_coursedates($column_info, $the_product_id) {
            if($column_info == 'order_course_dates') {
                $date_field = 'wccaf_gLUNQXKdWIcO';
                $course_dates = get_post_meta($the_product_id, $date_field, true);
                echo $course_dates;
            }
        }
        add_action('manage_shop_order_posts_custom_column', 'add_order_coursedates', 99, 2);
    • This reply was modified 2 years, 3 months ago by pmoignard.

    Check with var_dump() which values come out where. Don’t worry, this will look strange in the backend when you call it, but it will get you there faster. Then you should be able to find the cause. My guess is a wrong name in the key for the post_meta field. But I don’t know the rest of your programming, so I’m just guessing.

    Thread Starter pmoignard

    (@pmoignard)

    Ok, when I var_dump() on $the_product_id, I get (for example) “int(5798)” which is the ID of the order, not the ID of the associated product. I need the post ID of the associated WooCommerce product.

    Then you have to load the order data via the ID and then the product information from there. Since these are now more WooCommerce questions, the question would be better off in the WooCommerce forum. But I took a quick look:
    wc_get_order( $order_id ); to get the folders.
    $order->get_items() returns a list of associated products.
    See: https://stackoverflow.com/questions/39401393/how-to-get-woocommerce-order-details

    Thread Starter pmoignard

    (@pmoignard)

    AH. That works. Thanks so much for your guidance @threadi!

    Thread Starter pmoignard

    (@pmoignard)

    The final code:

    // Add Course Dates to new column
        
        function add_order_coursedates($column_info, $order_id) {
            if($column_info == 'order_course_dates') {
                $order_info = wc_get_order( $order_id );
                $order_items = $order_info->get_items();
                foreach ( $order_items as $order_item ) {
                    $the_product_id = $order_item->get_product_id();
                }
                $date_field = 'wccaf_gLUNQXKdWIcO';
                $course_dates = get_post_meta($the_product_id, $date_field, true);
                //var_dump($folders);
                echo $course_dates;     
            }
        }
        add_action('manage_shop_order_posts_custom_column', 'add_order_coursedates', 99, 2);

    Nice if I could help. You are welcome to mark the topic as solved.

    Thread Starter pmoignard

    (@pmoignard)

    Working!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘WooCommerce: Displaying Custom Feild value in “Orders” Admin List’ is closed to new replies.