When you register a route, parameters accepted via the query string (eg. ?a=b&c=d
) are not registered as part of the endpoint, like you have done. Those parameters just need to be defined in the args
property:
add_action(
'rest_api_init',
function() {
register_rest_route(
'woo/v2',
'woocommerce/order_summary_by_date',
array(
'methods' => 'GET',
'callback' => 'woocommerce_orders_by_dates'
'args' => array(
'start_date' => array(
'required' => true,
),
'end_date' => array(
'required' => true,
),
),
)
);
}
);
You can also defined sanitization and validation callbacks for the arguments. This is all described in the documentation.
To access the values you use the $request
object passed to the callback:
function woocommerce_orders_by_dates( $request ) {
$start_date = $request->get_param( 'start_date' );
$end_date = $request->get_param( 'end_date' );
}