Introduction
Optimizing WooCommerce REST API performance is crucial for headless implementations and custom integrations. Here’s how to make your API endpoints faster and more efficient.
Key Optimization Strategies
1. Custom Endpoint Optimization
function register_optimized_endpoints() {
register_rest_route('wc/v3', '/optimized-products', [
'methods' => 'GET',
'callback' => 'get_optimized_products',
'permission_callback' => function() {
return current_user_can('manage_woocommerce');
}
]);
}
function get_optimized_products() {
global $wpdb;
// Direct SQL for better performance
$results = $wpdb->get_results("
SELECT p.ID, p.post_title, pm.meta_value as price
FROM {$wpdb->posts} p
LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
WHERE p.post_type = 'product'
AND p.post_status = 'publish'
AND pm.meta_key = '_price'
");
return new WP_REST_Response($results, 200);
}
2. Response Optimization
function optimize_api_response($response, $handler, $request) {
// Remove unnecessary data
if (isset($response->data)) {
unset($response->data['_links']);
unset($response->data['_embedded']);
}
// Add cache headers
$response->header('Cache-Control', 'public, max-age=3600');
return $response;
}
add_filter('rest_post_dispatch', 'optimize_api_response', 10, 3);
3. Query Optimization
function optimize_product_queries($args, $request) {
// Optimize query arguments
$args['update_post_meta_cache'] = false;
$args['update_post_term_cache'] = false;
if (!empty($request['fields'])) {
$args['fields'] = explode(',', $request['fields']);
}
return $args;
}
add_filter('woocommerce_rest_product_query', 'optimize_product_queries', 10, 2);
Best Practices
-
Pagination
function implement_cursor_pagination($args, $request) { if (isset($request['after'])) { $args['post__in'] = get_posts_after_cursor($request['after']); } return $args; } function get_posts_after_cursor($cursor) { global $wpdb; return $wpdb->get_col($wpdb->prepare(" SELECT ID FROM {$wpdb->posts} WHERE post_type = 'product' AND ID > %d ORDER BY ID ASC LIMIT 100 ", $cursor)); } -
Caching
function cache_api_response($response, $handler, $request) { $cache_key = 'wc_api_' . md5($request->get_route() . serialize($request->get_params())); $cached = wp_cache_get($cache_key); if (false !== $cached) { return $cached; } wp_cache_set($cache_key, $response, '', 3600); return $response; } -
Field Selection
function filter_response_fields($response, $post, $request) { if (empty($request['fields'])) { return $response; } $fields = explode(',', $request['fields']); $data = array_intersect_key($response->data, array_flip($fields)); return new WP_REST_Response($data, 200); }
Implementation Example
add_action('rest_api_init', function() {
register_optimized_endpoints();
// Add filters
add_filter('woocommerce_rest_product_query', 'optimize_product_queries', 10, 2);
add_filter('woocommerce_rest_prepare_product', 'filter_response_fields', 10, 3);
// Add caching
add_filter('rest_post_dispatch', 'cache_api_response', 10, 3);
});
Monitoring and Testing
- Performance Metrics
function log_api_performance($response, $handler, $request) { $start_time = defined('REST_REQUEST_START_TIME') ? REST_REQUEST_START_TIME : 0; $execution_time = microtime(true) - $start_time; error_log(sprintf( 'API Request: %s, Time: %f seconds', $request->get_route(), $execution_time )); return $response; }
Conclusion
Optimizing your WooCommerce REST API is crucial for maintaining performance as your store grows. These strategies will help you build faster, more efficient API endpoints that can handle high traffic loads while maintaining responsiveness.