programing

woocommerce에서 현재 사용자의 모든 주문을 가져오는 방법

javamemo 2023. 3. 1. 08:47
반응형

woocommerce에서 현재 사용자의 모든 주문을 가져오는 방법

플러그인 기능 내에서 현재 사용자의 주문을 모두 받고 싶습니다.

이것을 사용하고 있습니다.

    function get_all_orders(){
        $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'   => wc_get_order_types( 'view-orders' ),
            'post_status' => array_keys( wc_get_order_statuses() )
        ) ) );
   return $customer_orders;
  }

이것은 테마 내에서는 잘 동작하지만 커스텀 플러그인 내에서는 아무것도 반환하지 않습니다.내가 뭘 잘못하고 있나요?먼저 WooCommerce 수업을 불러야 하나요?

API는 첫 번째 질문 이후 변경되었을 수 있지만, 이것은 훨씬 더 우아하고 WC 고유의 기능을 사용합니다.

$args = array(
    'customer_id' => $user_id,
    'limit' => -1, // to retrieve _all_ orders by this user
);
$orders = wc_get_orders($args);

다른 $args를 더 많이 사용할 수 있습니다.

    if (!class_exists('WooCommerce')) :
        require ABSPATH . 'wp-content/plugins/woocommerce/woocommerce.php';
        $orders = get_all_orders();
    endif;

    function get_all_orders() {
        $customer_orders = get_posts(apply_filters('woocommerce_my_account_my_orders_query', array(
            'numberposts' => -1,
            'meta_key' => '_customer_user',
            'meta_value' => get_current_user_id(),
            'post_type' => wc_get_order_types('view-orders'),
            'post_status' => array_keys(wc_get_order_statuses())
                )));
        return $customer_orders;
    }

이 코드를 사용해 보세요.

언급URL : https://stackoverflow.com/questions/42643065/how-to-get-all-orders-of-current-user-in-woocommerce

반응형