Woocommerce REST API 在订单端点中排除特定产品订单

Woocommerce REST API exclude specific product oder in orders endpoints

我想排除订单端点中的特定产品订单 /wp-json/wc/v3/orders

我试过命令准备查询挂钩,但它不起作用。

add_filter('woocommerce_rest_orders_prepare_object_query', function (array $args, \WP_REST_Request $request) {


    $args['product_id'] != 21380;

    return $args;
}, 10, 2);

如果有人能帮助我,我将不胜感激。

终于找到解决办法了

参考 https://github.com/woocommerce/woocommerce-rest-api/blob/master/src/Controllers/Version2/class-wc-rest-orders-v2-controller.php

add_filter('woocommerce_rest_orders_prepare_object_query', function (array $args, \WP_REST_Request $request) {

    global $wpdb;
   // Search by product.
        if ( ! empty( $request['product'] ) ) {
            $order_ids = $wpdb->get_col(
                $wpdb->prepare(
                    "SELECT order_id
                    FROM {$wpdb->prefix}woocommerce_order_items
                    WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
                    AND order_item_type = 'line_item'",
                    $request['product']
                )
            );

    $order_ids = !empty($order_ids) ? $order_ids : array(0);
    $args['post__in'] = $order_ids;
    return $args;
}, 10, 2);