• Resolved rt32kd

    (@rt32kd)


    I’m trying to add additional columns to the Woocommerce Orders summary page. The columns need to contain custom metadata that have been added to product pages using the Product Add Ons plugin.

    I followed this tutorial and it does not return the fields. The tutorial uses get_post_meta from the wp_post_meta table but the plugin seems to store the fields in the order_item_meta table rather than the wp_post_meta table.

    How do I extract the additional purchase fields from order_item_meta rather than wp_post_meta?

    Here is the code I am working with

    
    // ADDING 2 NEW COLUMNS WITH THEIR TITLES (keeping "Total" and "Actions" columns at the end)
    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'] = __( 'Child First','theme_domain');
                $reordered_columns['my-column2'] = __( 'Child Last','theme_domain');
                $reordered_columns['my-column3'] = __( 'DOB','theme_domain');
                $reordered_columns['my-column4'] = __( 'Needs','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 )
    {
        switch ( $column )
        {
            case 'my-column1' :
                // Get custom post meta data
                $my_var_one = get_post_meta( $post_id, "Child's First Name", 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;
    
            case 'my-column2' :
                // Get custom post meta data
                $my_var_two = get_post_meta( $post_id, "Child's Last Name", true );
                if(!empty($my_var_two))
                    echo $my_var_two;
    
                // Testing (to be removed) - Empty value case
                else
                    echo '<small>(<em>no value</em>)</small>';
    
                break;
                
            case 'my-column3' :
                // Get custom post meta data
                $my_var_three = get_post_meta( $post_id, "Date of Birth", true );
                if(!empty($my_var_three))
                    echo $my_var_three;
    
                // Testing (to be removed) - Empty value case
                else
                    echo '<small>(<em>no value</em>)</small>';
    
                break;
            case 'my-column4' :
                // Get custom post meta data
                $my_var_four = get_post_meta( $post_id, "class", true );
                if(!empty($my_var_four))
                    echo $my_var_four;
    
                // Testing (to be removed) - Empty value case
                else
                    echo '<small>(<em>no value</em>)</small>';
    
                break;
        }
    }
    

    Thanks

    • This topic was modified 2 years, 5 months ago by rt32kd.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Add column to admin Orders summary page with custom field data’ is closed to new replies.