Woocommerce API clear cache when a new order is placed
-
Hello,
Thank you for the wonderful plugin. I’m using Woocommerce API to get the list of orders for the logged in customer
/wp-json/wc/v3/orders?customer=5
, however if the customer created a new order the cached version is not flushed. So if the orders endpoint is called again it will not show the most recent order. Below is my custom code:<?php /** * Register the custom API endpoints so it will be cached, example: /wp-json/wc/v3/products */ function wprc_api_endpoints( $allowed_endpoints ) { $WC = 'wc/v3'; $API = 'api/v1'; $DOKAN = 'dokan/v1'; $base_endpoints[$WC] = array('orders', 'products'); $base_endpoints[$API] = array('orders', 'taxonomies', 'listing'); $base_endpoints[$DOKAN] = array('products', 'stores'); foreach ( $base_endpoints as $endpoint => $values ) { foreach ($values as $ep) { if ( ! isset( $allowed_endpoints[ $endpoint ] ) || ! in_array( $ep, $allowed_endpoints[ $endpoint ] ) ) { $allowed_endpoints[ $endpoint ][] = $ep; } } } return $allowed_endpoints; } function wprc_determine_object_type($object_type, $cache_key, $data, $uri) { if ($object_type !== 'unknown') { return $object_type; } if ( strpos( $uri, '/wp-json/wc/v3/orders' ) ) { $object_type = 'wc_orders'; } if ( strpos( $uri, '/wp-json/api/v1/orders' ) ) { $object_type = 'api_orders'; } return $object_type; } add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_api_endpoints', 10, 1); add_filter( 'wp_rest_cache/determine_object_type', 'wprc_determine_object_type', 10, 4 );
How can I flush the orders cache for the current customer after a new order is made?
Thanks
- The topic ‘Woocommerce API clear cache when a new order is placed’ is closed to new replies.