Home > Mobile >  WooCommerce query all products with a purchase note
WooCommerce query all products with a purchase note

Time:05-13

I am struggling to get all products that have a purchase note, both simple products and variable products.

WC_Product_Query doesn't seem to be able to do that, as that does not provide a method for purchase notes. As it's not meta_data but an actual product field I'm don't see how to do this.

Has anyone a direction in which I would be able to query the above?

CodePudding user response:

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

See more details here

  • Related