-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make wc-json api compatible #22
Comments
@rafaelcmrj here is the code snippet i developed to add the order_number filter to Woocommerce API (it is made and tested for latest woocommerce api v3+) /**
* Add capability to get orders by 'order_number' in WC API
* Depends on WooCommerce Sequential Order Numbers (Free or Pro)
* Credits: Amir Moradi (https://linkedin.com/in/amirhmoradi) for Avnox.com
*
*/
function ax_wc_add_custom_order_number_query_arg( $args ) {
$args['order_number'] = array(
'description' => 'Filter by custom order number',
'type' => 'string',
'validate_callback' => 'is_string',
);
return $args;
}
add_filter( 'woocommerce_rest_orders_collection_params', 'ax_wc_add_custom_order_number_query_arg', 10, 1 );
function ax_wc_filter_orders_by_custom_order_number_with_plugin( $args, $request ) {
if ( ! empty( $request['order_number'] ) ) {
// Check if the Sequential Order Numbers plugin is active and the method exists
$wc_sec_ordnum_function = function_exists('wc_seq_order_number_pro') ? 'wc_seq_order_number_pro' : (function_exists('wc_sequential_order_numbers') ? 'wc_sequential_order_numbers' : null);
if ( $wc_sec_ordnum_function ) {
$order_id = $wc_sec_ordnum_function()->find_order_by_order_number( $request['order_number'] );
// Additional check to ensure the order number matches exactly
if ( $order_id ) {
$order = wc_get_order( $order_id );
$actual_order_number = method_exists($order, 'get_order_number') ? $order->get_order_number() : '';
if ( $actual_order_number === $request['order_number'] ) {
$args['post__in'] = array( $order_id );
} else {
// Set an impossible condition, such as a post status that doesn't exist
$args['post_status'] = 'no-status';
$args['post__in'] = array( 0 );
}
} else {
// If no matching order is found
// Set an impossible condition, such as a post status that doesn't exist
$args['post_status'] = 'no-status';
$args['post__in'] = array( 0 );
}
} else {
// Optionally, handle the case where the plugin is not active or the method doesn't exist
// This could be a simple log, an error message, or a fallback behavior
// error_log( 'PLUGIN NOT FOUND' );
}
}
return $args;
}
add_filter( 'woocommerce_rest_orders_prepare_object_query', 'ax_wc_filter_orders_by_custom_order_number_with_plugin', 100, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd like to make the v2/orders/:id endpoint work with the sequential order number. Now it only accepts the actual order ID, is there a way to make it recognize the order_number as well?
The text was updated successfully, but these errors were encountered: