Home > Software design >  WooCommerce API get order off metadata value
WooCommerce API get order off metadata value

Time:07-28

Been a little stumped after researching and was curious if anyone could provide assistance.

I was curious if there is a way to tap into the WC API and retrieve and order using the endpoint /wp-json/wc/v3/orders but based off of the following meta data:

                "id": 3944240,
                "key": "_alg_wc_full_custom_order_number",
                "value": "D########"

I would like to get an order off that meta data key value of D###### rather than order id or any other parameters their documentation provides, if not possible totally understand just figured id try here!

Thanks!

CodePudding user response:

AFAIK, the WC shop order endpoint doesn't accept meta_key parameter out of the box. But you can hook to the endpoint filter with custom function as below, then you can use the endpoint as such:

/wp-json/wc/v3/orders?custom_order_number=D#####

add_filter( 'woocommerce_rest_shop_order_object_query', 'filter_orders_by_custom_order_number', 100, 2 );
function filter_orders_by_custom_order_number( $args, $request ) {
    if( !isset( $request['custom_order_number'] ) ) {
        return $args;
    }
    
    // add custom query to existing meta_query if exist
    if( isset( $args['meta_query'] ) ) {
        $args['meta_query']['relation'] = 'AND';
    } else {
        $args['meta_query'] = array();
    }

    $args['meta_query'][] = array(
        'key' => '_alg_wc_full_custom_order_number',
        'value' => $request['custom_order_number']
    );
    
    return $args;
}
  • Related