Woocommerce更新3.0的自定义订单查询

时间:2017-04-12 22:47:11

标签: wordpress woocommerce

我有一些代码会将完成的订单列表输出到我的functions.php中的[sales-list]短代码中。它在Woocommerce的3.0更新之前完美运行,但由于(我假设)3.0中的新CRUD类已停止工作。

问题是:我如何调整它以适应新课程?或者如果这不是问题,还有什么可能改变了?

以下是代码:

/* Sales List */
function saleslist_shortcode() {
  global $woocommerce;
  $args = array(
      'post_type' => 'shop_order',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'tax_query' => array(
          array(
              'taxonomy' => 'shop_order_status',
              'field' => 'slug',
              'terms' => array(
                  'completed'
              )
          )
      )
  );

  $loop = new WP_Query($args); ?>

  <div class="sales-list">
  <?php
  while ($loop->have_posts()):
      $loop->the_post();
      $order_id = $loop->post->ID;
      $order    = new WC_Order($order_id); ?>
      <article>
        <h6 class="mtn ptn">Order #<?php echo $order_id; ?> &mdash; <time datetime="<?php the_time('c'); ?>"><?php echo the_time('d/m/Y'); ?></time></h6>
          <table cellspacing="0" cellpadding="2">
              <tbody>
                    <?php echo $order->email_order_items_table( array(
                        'show_sku'      => true,
                        'show_image'    => false,
                        'image_size'    => array( 32, 32 ),
                        'plain_text'    => $plain_text,
                        'sent_to_admin' => true
                    ) ); ?>
              </tbody>
          </table>
        <div style="clear:both;"></div>   
      </article>
    <?php endwhile; ?>
  </div>
  <?php
}

add_shortcode( 'sales-list', 'saleslist_shortcode' );

1 个答案:

答案 0 :(得分:1)

我可能会首先将post_type参数更改为"post_type" => array_keys( wc_get_order_statuses() )。并将税务查询作为订单状态删除由“发布状态”处理。

我想我会建议切换到wc_get_orders()。这是我现在最好的猜测。请记住,如果您不使用WC3.0,这将导致致命错误...即:它不兼容。

/* Sales List */
function saleslist_shortcode() {

  $args = array(
        'limit' => -1,
        'status' => array( 'wc-completed' ),
        'type' => array( 'shop_order' )
  );

  $orders = wc_get_orders( $args );

  ob_start();

  if( $orders ){ ?>

    <div class="sales-list">

      <?php foreach( $orders as $order ){ ?>

        <article>

          <h6 class="mtn ptn"><?php printf( __( 'Order #%s', 'your-plugin' ), $order->get_order_number() ); ?> <?php printf( '<time datetime="%s">%s</time>', $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) ); ?></h6>

            <table>
                <thead>
                    <tr>
                        <th class="td"><?php _e( 'Product', 'your-plugin' ); ?></th>
                        <th class="td"><?php _e( 'Quantity', 'your-plugin' ); ?></th>
                        <th class="td"><?php _e( 'Price', 'your-plugin' ); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php echo wc_get_email_order_items( $order, array(
                        'show_sku'      => false,
                        'show_image'    => false,
                        'image_size'    => array( 32, 32 ),
                        'plain_text'    => false,
                        'sent_to_admin' => false,
                    ) ); ?>
                </tbody>
            </table>

        </article>

      <?php } ?>

    </div>
  <?php }

  return ob_get_clean();

}
add_shortcode( 'sales-list', 'saleslist_shortcode' );