Support » Plugin: WP REST Cache » Woocommerce 3.2.6 API Cache

  • Resolved shotagency



    Hope everything is going well!

    I would like to know if this plugin will help me to cached the woocommerce API for speed up my iOS/Android app?


Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Richard Korthuis


    Hi @shotagency

    Thank you for using our plugin!

    Yes, in theory this is possible (we just did a really small test), but you have to keep in mind that the WooCommerce REST API is using authentication so you must make sure the authentication headers are used to distinguish between caches.

    We tested this with the products endpoint. First we registered the products endpoint so it would be cached (this is necessary since it is a custom endpoint):

     * Register the WooCommerce endpoints so they will be cached.
    function wprc_add_wc_endpoints( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'wc/v3' ] ) || ! in_array( 'products', $allowed_endpoints[ 'wc/v3' ] ) ) {
            $allowed_endpoints[ 'wc/v3' ][] = 'products';
        return $allowed_endpoints;
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_wc_endpoints', 10, 1);

    And then we made sure the authentication headers were used to distinguish seperate caches. This can be done in two ways:
    1. Go to Settings > WP REST Cache and add authorization to the Global cacheable request headers.
    2. Use the hook wp_rest_cache/cacheable_request_headers to specify per endpoint which request headers should be used:

    function wprc_add_cacheable_request_headers( $cacheable_headers ) {
        $cacheable_headers['wc/v3/products'] = 'authorization';
        return $cacheable_headers;
    add_filter('wp_rest_cache/cacheable_request_headers', 'wprc_add_cacheable_request_headers', 10, 1);

    You must also keep in mind that these caches aren’t flushed automatically when editing products (or any other types you might want to be cached). So either you have to flush the caches manually or you have to write the code to flush them automatically (I could give you some pointers on how to do that, so let me know if you need any help on that point.)
    And a really important thing to keep in mind is that whenever you revoke a WooCommerce API Key, you have to flush the caches also, because otherwise the key will still work for already cached items (until these caches expire ofcourse).

    Hello, can you post some pointer how i can flush the cache on edit woocommerce product , category or something relate to woocommerce

    Can you please post the pointers you mentioned above?

    Plugin Author Richard Korthuis


    Hi @yromara and @zaheerahmad

    Thank you fro using our plugin!

    As it appears automatic flushing of WooCommerce it quite easy. If you followed the steps above, they are already cached. Unfortunately they are cached with a wrong ‘object type’, this is because of the way the rest response is set up by WooCommerce. But we can fix this by using the wp_rest_cache/determine_object_type filter:

    function wc_determine_object_type( $type, $cache_key, $data, $uri ) {
    	if ( '/wp-json/wc/v3/products' === substr( $uri, 0, 23 ) ) {
    		return 'product';
    	return $type;
    add_filter( 'wp_rest_cache/determine_object_type', 'wc_determine_object_type', 10, 4 );

    That’s all, now the product caches will be automatically flushed ๐Ÿ™‚

    Now for the categories…
    Let’s start with adding it to the wp_rest_cache/determine_object_type filter:

    function wc_determine_object_type( $type, $cache_key, $data, $uri ) {
    	if ( '/wp-json/wc/v3/products/categories' === substr( $uri, 0, 34 ) ) {
    		return 'product_cat';
    	} else if ( '/wp-json/wc/v3/products' === substr( $uri, 0, 23 ) ) {
    		return 'product';
    	return $type;
    add_filter( 'wp_rest_cache/determine_object_type', 'wc_determine_object_type', 10, 4 );

    Unfortunately they still aren’t automatically cached this way ๐Ÿ™ This is again due to the fact that the rest response is set up differently than the ‘normal’ categories endpoint and that is causing the WP REST Cache plugin to not being able to detect the cache relations (i.e. which categories are in which cache). We do have an action you could hook into to detect those cache relations yourself, but after looking at it I have determined that we need to send more data to the hook to work with it in an efficient way (I have put it on our todo list).
    So for now the only thing we can really do is flush all category caches whenever a category is edited/added/deleted:

    function wc_flush_category_caches() {
    	\WP_REST_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_cache_by_endpoint( '/wp-json/wc/v3/products/categories', \WP_REST_Cache_Plugin\Includes\Caching\Caching::FLUSH_LOOSE );
    add_action( 'created_product_cat', 'wc_flush_category_caches', 10 );
    add_action( 'edit_product_cat', 'wc_flush_category_caches', 10 );
    add_action( 'delete_product_cat', 'wc_flush_category_caches', 10 );
    Plugin Author Acato


    This thread has been marked as resolved due to lack of activity.

    Youโ€™re always welcome to open a new topic.

    Thanks for understanding!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Woocommerce 3.2.6 API Cache’ is closed to new replies.