如何按订单状态进行过滤 [英] How to Filter by Order Status

查看:256
本文介绍了如何按订单状态进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用WooCommerce的Wordpress上,现在一个名为Parcelware的插件允许我在两个日期之间导出任何订单,请参阅下面的代码。我需要添加一个过滤器,这意味着只会导出处理订单。

I'm on Wordpress using WooCommerce, now a plugin called Parcelware allows me to export any orders between 2 dates, see the code below. I need to add to that a filter that means only 'Processing' orders are exported.

如果有人可以帮助我,会很棒!

Would be great if someone can assist me!

我发现这个WooCommerce的链接不知道是否任何帮助。

I found this link on WooCommerce not sure if it is of any help.

http://docs.woothemes.com/document/customer-order-csv-import-suite/#importingorders

/**
 * Read order variables from the database and store them in
 * their respective variable slot. This function is called 
 * on creation of the object.
 * 
 * @abstract
 */
abstract function read_order_settings();

/**
 * Get orders
 * 
 * @return mixed order
 */
static function get_orders( $date_from, $date_to ){
    // Get orders between the two defined dates, this function uses a filter.
    define('PARCELWARE_GET_ORDERS_FILTER_DATE_FROM', $date_from );
    define('PARCELWARE_GET_ORDERS_FILTER_DATE_TO', $date_to );
    add_filter('posts_where', array( __CLASS__, 'order_page_get_orders_where_dates_between') );
    $orders = get_posts( array(
        'numberposts' => -1,
        'offset' => 0,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'shop_order',
        'suppress_filters' => false
    ) );
    remove_filter('posts_where', 'order_page_get_orders_where_dates_between');

    return $orders;
}

/**
 * Applies a where clause on the get_posts call
 * 
 * @param string $where
 * @return string $where
 */
static function order_page_get_orders_where_dates_between( $where ){
    global $wpdb;

    if( ! defined('PARCELWARE_GET_ORDERS_FILTER_DATE_FROM') || ! defined('PARCELWARE_GET_ORDERS_FILTER_DATE_TO') )
        return $where;

    $where .= $wpdb->prepare(" AND post_date >= '%s' ", PARCELWARE_GET_ORDERS_FILTER_DATE_FROM);
    $where .= $wpdb->prepare(" AND post_date <= '%s' ", PARCELWARE_GET_ORDERS_FILTER_DATE_TO);

    return $where;
}

/**
 * Builds the header row for the csv file
 * 
 * @return string $csv
 */
static function get_csv_header(){
    return implode( self::$separator, array_keys( self::$variable_keys ) );
}

/**
 * Converts this object to a comma separated values line
 * 
 * @param mixed array $array
 * @return string $csv_line
 */
function to_CSV(){
    if( empty( $this->variables ) )
        return '';

    $csv = '';
    foreach( $this->variables as $variable )
        $csv .= $variable . self::$separator;

    return implode( self::$separator, $this->variables );
}

}

推荐答案

订单状态是一个分类法,因此以下内容应该为您过滤:

Order statuses are a taxonomy, so the following should filter that for you:

static function get_orders( $date_from, $date_to ){
    // Get orders between the two defined dates, this function uses a filter.
    define('PARCELWARE_GET_ORDERS_FILTER_DATE_FROM', $date_from );
    define('PARCELWARE_GET_ORDERS_FILTER_DATE_TO', $date_to );
    add_filter('posts_where', array( __CLASS__, 'order_page_get_orders_where_dates_between') );
    $orders = get_posts( array(
        'numberposts' => -1,
        'offset' => 0,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'shop_order',
        'suppress_filters' => false,
        'tax_query' => array(
                array(
                    'taxonomy' => 'shop_order_status',
                    'field' => 'slug',
                    'terms' => array('processing')
                )
            )
    ) );
    remove_filter('posts_where', 'order_page_get_orders_where_dates_between');

    return $orders;
}

这篇关于如何按订单状态进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆