programing

우커머스에서 날짜별로 사용자 주문을 받는 방법은?

codeshow 2023. 10. 29. 20:01
반응형

우커머스에서 날짜별로 사용자 주문을 받는 방법은?

저는 날짜 범위 또는 현재 한 달 동안의 사용자 총 주문 합계를 얻을 수 있는 표준 방법을 찾고 있습니다.

wocommerce source code를 탐색해보니 woo는 이런것을 사용하고 있습니다.

            $order_item_amounts = $this->get_order_report_data( array(
            'data' => array(
                '_line_total' => array(
                    'type'            => 'order_item_meta',
                    'order_item_type' => 'line_item',
                    'function' => 'SUM',
                    'name'     => 'order_item_amount'
                ),
                'post_date' => array(
                    'type'     => 'post_data',
                    'function' => '',
                    'name'     => 'post_date'
                ),
                '_product_id' => array(
                    'type'            => 'order_item_meta',
                    'order_item_type' => 'line_item',
                    'function'        => '',
                    'name'            => 'product_id'
                ),
            ),
            'where_meta' => array(
                'relation' => 'OR',
                array(
                    'type'       => 'order_item_meta',
                    'meta_key'   => array( '_product_id', '_variation_id' ),
                    'meta_value' => $this->product_ids,
                    'operator'   => 'IN'
                ),
            ),
            'group_by'     => 'product_id, ' . $this->group_by_query,
            'order_by'     => 'post_date ASC',
            'query_type'   => 'get_results',
            'filter_range' => true
        ) );

클래스 wc-report-sales-by-product.php

하지만 아시다시피, 이것은 사용자가 아닌 제품을 기반으로 작동합니다.위의 코드에 의하면,$this->group_by_querypart는 woo source에서 찾을 수 있는 날짜의 조건을 보유하고 있습니다.제 질문은 실제로 wocommerce의 내장 기능을 사용하여 주어진 날짜 범위를 기준으로 주문 목록을 생성하는 방법에 관한 것입니다.

감사해요.

당신의 대답을 들어보면, 제가 생각하기엔 정확합니다.

필드에 를 추가하면 됩니다.다음과 같은 것.

public function get_customer_total_order() {
    $customer_orders = get_posts( array(
        'numberposts' => - 1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => array( 'shop_order' ),
        'post_status' => array( 'wc-completed' ),
        'date_query' => array(
            'after' => date('Y-m-d', strtotime('-10 days')),
            'before' => date('Y-m-d', strtotime('today')) 
        )

    ) );

    $total = 0;
    foreach ( $customer_orders as $customer_order ) {
        $order = wc_get_order( $customer_order );
        $total += $order->get_total();
    }

    return $total;
}

추가 판독값:

날짜 형식

어떻게 사용할 것인가의 문제에 맞추어get_order_report_data, 이런 식으로 하면 돼요...이것을 당신의 것에 붙여넣을 수 있습니다.functions.php시험해 볼 주제로

include_once( WP_PLUGIN_DIR . '/woocommerce/includes/admin/reports/class-wc-admin-report.php');

$reports = new WC_Admin_Report();
$args = array(
    'data' => array(
        '_order_total' => array(
            'type'     => 'meta',
            'function' => 'SUM',
            'name'     => 'total_sales'
        ),
    ),
    'where' => array(
        array(
            'key'      => 'post_date',
            'value'    => date( 'Y-m-d', strtotime( '01/01/2016' ) ), // starting date
            'operator' => '>'
        ),
        array(
            'key'      => 'post_date',
            'value'    => date( 'Y-m-d', strtotime( '02/01/2016' ) ), // end date...
            'operator' => '<'
        ),
    ),
    'where_meta' => array(
        array(
            'meta_key'   => '_customer_user',
            'meta_value' => '1', // customer id
            'operator'   => '='
        )
    ),
);
$data = $reports->get_order_report_data($args);
print_r($data); // prints like: stdClass Object ( [total_sales] => 200 )

위의 코드에서 언급한 내용을 참고하시기 바랍니다.

언급URL : https://stackoverflow.com/questions/35146663/how-to-get-user-orders-by-date-in-woocommerce

반응형