【问题标题】:How to Filter by Order Status如何按订单状态筛选
【发布时间】:2013-04-02 18:36:17
【问题描述】:

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

如果有人可以帮助我,那就太好了!

我在 WooCommerce 上找到此链接,不确定是否有任何帮助。

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

}

【问题讨论】:

    标签: wordpress filter woocommerce export orders


    【解决方案1】:

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

    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;
    }
    

    【讨论】:

    • 不幸的是,它仍然导出了它们日期之间的所有订单。
    猜你喜欢
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 2021-05-06
    • 2019-11-22
    • 2021-06-03
    • 1970-01-01
    相关资源
    最近更新 更多