Home » Php » Get item meta from a WooCommerce order to display it

Get item meta from a WooCommerce order to display it

Posted by: admin September 16, 2018 Leave a comment

Questions:

Within WooCommerce backend admin Order Table screen, I have created a new column to display information. The data I would like to display is the meta data of each item within the order.

During the order process, additional meta data is captured for a product, which is saved within the order (by going to WooCommerce > Order > Edit a specific order ID).
This is the data I would like to grab and return: https://d.pr/free/i/HZzEam

When I inspect that area, it is the display_meta table content that I would like to replicate and add to the column: https://d.pr/free/i/oGsngJ

Digging through the WooCommerce files, I see this is the area where that table is being created:

$hidden_order_itemmeta = apply_filters(
    'woocommerce_hidden_order_itemmeta', array(
        '_qty',
        '_tax_class',
        '_product_id',
        '_variation_id',
        '_line_subtotal',
        '_line_subtotal_tax',
        '_line_total',
        '_line_tax',
        'method_id',
        'cost',
    )
);
?><div class="view">
    <?php if ( $meta_data = $item->get_formatted_meta_data( '' ) ) : ?>
        <table cellspacing="0" class="display_meta">
            <?php
            foreach ( $meta_data as $meta_id => $meta ) :
                if ( in_array( $meta->key, $hidden_order_itemmeta, true ) ) {
                    continue;
                }
                ?>
                <tr>
                    <th><?php echo wp_kses_post( $meta->display_key ); ?>:</th>
                    <td><?php echo wp_kses_post( force_balance_tags( $meta->display_value ) ); ?></td>
                </tr>
            <?php endforeach; ?>
        </table>
    <?php endif; ?>
</div>

I realize it’s a lot of information to add to a single column, but that’s currently what I need to do.

Similar to how WooCommerce is displaying that information in the Order Edit area, how would I also display that order item meta into my column?

I am using some PHP snippets (hook) to create the new column, by utilizing an available action from Admin Columns Pro plugin. Essentially, it allowed me to add a column of an existing custom field, and then override it with any data that I want. This is the current code I have:

<?php

/**
 * Display custom Event Registration details (item meta data from the order) (which are captured via WooCommerce Add-ons form/fields) on the ORDER LIST/TABLE SCREEN via overriding Admin Columns Pro field column
 *
 * Filter the display value for a column
 *
 * @param mixed $value Custom field value
 * @param int $id Object ID
 * @param AC_Column $column Column instance
 */
function fs_custom_event_registration_details_column_value( $value, $id, $column ) {
    if ( $column instanceof AC_Column_CustomField ) {

        $order = wc_get_order( $id );
        //$order_items = $order->get_items();

        // get the meta key of this column
        $meta_key = $column->get_meta_key();

        // we've added the WooCommerceEventsTicketsPurchased as a custom column via Admin Columns plugin, and now are targetting that one to override it with different data
        if ( 'WooCommerceEventsTicketsPurchased' == $meta_key ) {

            $value = 'testing<br>';

            // example of replacing column field value with a value from a different field
            // $billingphone = get_post_meta( $id, '_billing_phone', true );
            // $value .= sprintf( '<span>'. $billingphone .'</span><br>', $value );

            foreach ( $order->get_items() as $item_id => $item ) {

                $product_name = $item->get_name();
                //$product_id = $item->get_product_id();
                //$product_variation_id = $item->get_variation_id();

                $product_id = $item['product_id'];

                $value .= $product_name;

            }

        }
    }

    // show me the results
    return $value;
}

add_filter( 'ac/column/value', 'fs_custom_event_registration_details_column_value', 10, 3 );

But obviously that’s only grabbing the title of the products within the order. I need to grab the order meta for each item within the order.

It doesn’t have to be in a table necessarily. A bulleted list, or even comma separated should be OK.

Also, if possible, I only want to output the item meta of a product within the Events category.

Answers: