循环遍历所有已完成的Woocommerce订单的自定义字段数据

时间:2016-07-15 01:40:52

标签: php wordpress woocommerce

我正在尝试创建一个woocommerce最近的订单表,其中包含一个包含来自自定义字段的数据的列,但我很难让它工作。我能够使用以下代码在单个订单详细信息页面中显示数据:

<?php
    $custom_fields = get_post_custom($order_id);
    $my_custom_field = $custom_fields["property-street-address"];
    foreach ( $my_custom_field as $key => $value ) {
    echo "$value";
  }
?>

但是当我尝试循环所有已发布的订单并为每个订单显示此字段(进入我的帐户信息中心页面)时,我遇到了麻烦。这就是我现在所拥有的:

<?php                   
global $woocommerce;                    
$args = array(          
'post_type'         => 'shop_order',            
'post_status'       => 'publish',                 
'posts_per_page' => -1      
);          
$loop = new WP_Query( $args );          
while ( $loop->have_posts() ) : $loop->the_post();          
$order_id = $loop->post->ID;                
$order = new WC_Order($order_id);               
?>

<p class="test"><?php

 $custom_fields = get_post_custom($order);
 $my_custom_field = $custom_fields["property-street-address"];
 foreach ( $my_custom_field as $key => $value ) {
  echo "$value";
 }

 ?></p>

<?php endwhile; ?>

我用来创建自定义结帐字段的插件是Woothemes Custom Field Editor

最终目标是在“订单 - 属性”部分下将此列作为此列中的列:

    <?php   $customer_orders = get_posts( apply_filters(         
     'woocommerce_my_account_my_orders_query', array(
     'numberposts' => $order_count,
     'meta_key'    => '_customer_user',
     'meta_value'  => get_current_user_id(),
     'post_type'   => 'shop_order',
     'post_status' => 'publish'
     ) ) );

if ( $customer_orders ) : ?>
  <table class="shop_table my_account_orders" width="100%">

    <thead>
        <tr>
            <th class="order-number"><span class="nobr"><?php _e( 'Order', 'woocommerce' ); ?></span></th>
            <th class="order-property"><span class="nobr"><?php _e( 'Property', 'woocommerce' ); ?></span></th>
            <th class="order-date"><span class="nobr"><?php _e( 'Date', 'woocommerce' ); ?></span></th>
            <th class="order-status"><span class="nobr"><?php _e( 'Status', 'woocommerce' ); ?></span></th>
            <th class="order-total"><span class="nobr"><?php _e( 'Total', 'woocommerce' ); ?></span></th>
            <th class="order-actions">&nbsp;</th>
        </tr>
    </thead>

    <tbody><?php
        foreach ( $customer_orders as $customer_order ) {
            $order = new WC_Order();

            $order->populate( $customer_order );

            $status     = get_term_by( 'slug', $order->status, 'shop_order_status' );
            $item_count = $order->get_item_count();

            ?><tr class="order">
                <td class="order-number">
                    <a href="<?php echo $order->get_view_order_url(); ?>">
                        <?php echo $order->get_order_number(); ?>
                    </a>
                </td>
                <td class="order-property">

                </td>
                <td class="order-date">
                    <time datetime="<?php echo date( 'Y-m-d', strtotime( $order->order_date ) ); ?>" title="<?php echo esc_attr( strtotime( $order->order_date ) ); ?>"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ); ?></time>
                </td>
                <td class="order-status" style="text-align:left; white-space:nowrap;">
                    <?php echo wc_get_order_status_name( $order->get_status() ); ?>
                </td>
                <td class="order-total">
                    <?php echo sprintf( _n( '%s for %s item', '%s for %s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ); ?>
                </td>
                <td class="order-actions">
                    <?php
                        $actions = array();

                        if ( in_array( $order->status, apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $order ) ) ) {
                            $actions['pay'] = array(
                                'url'  => $order->get_checkout_payment_url(),
                                'name' => __( 'Pay', 'woocommerce' )
                            );
                        }

                        if ( in_array( $order->status, apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ), $order ) ) ) {
                            $actions['cancel'] = array(
                                'url'  => $order->get_cancel_order_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ),
                                'name' => __( 'Cancel', 'woocommerce' )
                            );
                        }

                        $actions['view'] = array(
                            'url'  => $order->get_view_order_url(),
                            'name' => __( 'View', 'woocommerce' )
                        );

                        $actions = apply_filters( 'woocommerce_my_account_my_orders_actions', $actions, $order );

                        if ($actions) {
                            foreach ( $actions as $key => $action ) {
                                echo '<a href="' . esc_url( $action['url'] ) . '" class="button ' . sanitize_html_class( $key ) . '">' . esc_html( $action['name'] ) . '</a>';
                            }
                        }
                    ?>
                </td>
            </tr><?php
        }
    ?></tbody>

</table>

我还在学习PHP,这让我整天疯狂,所以非常感谢任何帮助!

0 个答案:

没有答案