代码之家  ›  专栏  ›  技术社区  ›  Garconis

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

  •  -1
  • Garconis  · 技术社区  · 8 年前

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

    在订单处理过程中,会捕获产品的其他元数据,并将其保存在订单中(通过转到WoomCommerce>订单>编辑特定订单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 );
    

    但很明显,这只是抢占了订单中产品的名称。我需要获取订单中每个项目的订单元数据。

    不一定要放在桌子上。项目符号列表,甚至逗号分隔都可以。

    另外,如果可能的话,我只想输出事件类别中产品的item meta。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Garconis    8 年前

    可以通过以下方式进行此操作:

    /**
     * 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