从WooCommerce订单中获取item meta以显示它

时间:2018-05-22 15:35:08

标签: php wordpress woocommerce

在WooCommerce后端管理员订单表屏幕中,我创建了一个新列来显示信息。我想要显示的数据是订单中每个项目的元数据。

在订单处理过程中,会为产品捕获其他元数据,该数据会保存在订单中(通过转到WooCommerce>订单>编辑特定订单ID)。 这是我想抓取并返回的数据: https://d.pr/free/i/HZzEam

当我检查该区域时,我希望复制并添加到列中的display_meta表格内容:https://d.pr/free/i/oGsngJ

通过WooCommerce文件,我看到这是创建该表的区域

$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>

我意识到要添加到单个列中的信息很多,但这是我目前需要做的事情。

类似于WooCommerce在订单编辑区域中显示该信息的方式,我如何将该订单商品元素显示在我的列中?

我正在使用一些PHP代码段(hook)来创建新列,方法是使用Admin Columns Pro插件中的可用操作。从本质上讲,它允许我添加现有自定义字段的列,然后使用我想要的任何数据覆盖它。这是我目前的代码:

<?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 );

但很明显,这只是获取订单中产品的标题。我需要获取订单中每个项目的订单元数据。

它不一定必须在桌子上。项目符号列表,甚至逗号分隔应该没问题。

此外,如果可能,我只想在“事件”类别中输出产品的元元素。

1 个答案:

答案 0 :(得分:0)

能够通过以下方式执行此操作:

/**
 * 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 is equal to the original value, so we use $value .= to add (append) additional data to it */
            if ( $value == 1) {
                $value .= ' event item<br>';
            }
            elseif ( $value > 1) {
                $value .= ' event items<br>';
            }

            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'];

                // only for items within the Events category
                if ( has_term( 'events', 'product_cat', $product_id ) ) {

                    // wrap HTML around the value output
                    $value .= '<table cellspacing="0" cellpadding="0" style="width: 100%; border: 1px solid rgba(0,0,0,0.66); margin: 0.5em 0;">';
                    $value .= '<thead style="background: rgba(0,0,0,0.1);"><tr><th style="padding: 0.25em; line-height: 1.25em; font-size: 0.875em;">
                        <h5 style="line-height: 1.2em; font-size: 1em; margin: 0; padding: 0; font-weight: 700;">'. $product_name .'</h5>
                        </th></tr></thead>';
                    $value .= '<tbody>';

                    // grab the meta from the item
                    $meta_data = $item->get_formatted_meta_data( '' );

                    // Loop though the data array to set the fields
                    foreach ( $meta_data as $meta_id => $meta ) :

                        // grab the key and strip anything that may be junky
                        $field_display_label = wp_kses_post( $meta->display_key );
                        // grab the key value and stip anything that may be junky, as well as ensuring things are properly wrapped
                        $field_display_value = wp_kses_post( force_balance_tags( $meta->display_value ) );
                        // by default, the key value has p tags wrapping it, so lets remove those for now
                        $field_display_value = str_replace('<p>','',$field_display_value);
                        $field_display_value = str_replace('</p>','',$field_display_value);

                        $value .= '<tr><td style="padding: 0.25em; line-height: 1.25em; font-size: 0.875em; border-top: 1px dotted rgba(0,0,0,0.25);">
                            <strong style="font-weight: 700;">'. $field_display_label .'</strong>: <br>&nbsp;&bull;&nbsp;'. $field_display_value .'</td></tr>';

                    endforeach;

                    // close the HTML wrapper around the value output
                    $value .= '</tbody></table>';

                }
            }
        }
    }

    // show me the results
    return $value;
}

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

导致:https://d.pr/free/i/ou1ORr

Order Screen