WooCommerce 查询所有带有购买备注的产品

WooCommerce query all products with a purchase note

我正在努力获取所有具有购买记录的产品,包括简单产品和可变产品。

WC_Product_Query好像做不到,因为它没有提供购买票据的方法。 因为它不是 meta_data 而是一个实际的产品字段,所以我不知道该怎么做。

有没有大佬指点一下可以查询以上内容?

$products = wc_get_products(array(
     'post_status' => array('publish', 'pending', 'draft', 'future', 'private'),
    'limit' => 1000,
    'orderby' => 'id',
    'order'   => 'asc',
    '_purchase_note' => true,
));
function handle_custom_query_var( $query, $query_vars ) {
    if ( ! empty( $query_vars['_purchase_note'] ) ) {
        $query['meta_query'][] = array(
            'key' => '_purchase_note',
            'compare' => '!=',
            'value' => ''
        );
    }

    return $query;
}
add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'handle_custom_query_var', 10, 2 );

查看更多详情here