• Hi,

    I am trying to clear product cache for only product that is affected on a product update

    this is what i have but not working can you please assist, I tried following most of the topics but on clearing related i’m falling but flushing the whole cache by endpoint works.

    /**
     * Register 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);
    
    function wprc_add_v2_wc_endpoints( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'wc/v2' ] ) || ! in_array( 'products', $allowed_endpoints[ 'wc/v2' ] ) ) {
            $allowed_endpoints[ 'wc/v2' ][] = 'products';
        }
        return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_v2_wc_endpoints', 10, 1);
    
    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);
    
    function wprc_v2_add_cacheable_request_headers( $cacheable_headers ) {
        $cacheable_headers['wc/v2/products'] = 'Authorization';
        return $cacheable_headers;
    }
    add_filter('wp_rest_cache/cacheable_request_headers', 'wprc_v2_add_cacheable_request_headers', 10, 1);
    
    function flutter_multi_vendor_wc_endpoints( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'api/flutter_multi_vendor' ] ) || ! in_array( 'product', $allowed_endpoints[ 'api/flutter_multi_vendor' ] ) ) {
            $allowed_endpoints[ 'api/flutter_multi_vendor' ][] = 'product';
        }
        return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'flutter_multi_vendor_wc_endpoints', 10, 1);
     
    
    // Determine object type
    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';
    	} else 	if ( '/wp-json/wc/v2/products/categories' === substr( $uri, 0, 34 ) ) {
    		return 'product_cat';
    	} else if ( '/wp-json/wc/v2/products' === substr( $uri, 0, 23 ) ) {
    		return 'product';
    	} else if ('/wp-json/api/flutter_multi_vendor/products' === substr( $uri, 0, 42 ) ){
            return 'product';
        }else if ('/wp-json/api/flutter_multi_vendor/product-categories' === substr( $uri, 0, 52 ) ){
            return 'product_cat';
        }
    
    	return $type;
    }
    add_filter( 'wp_rest_cache/determine_object_type', 'wc_determine_object_type', 10, 4 );
    
    /**
     * Process cache relations.
     *
     * @param int    $cache_id The row id of the current cache.
     * @param mixed  $data The data that is to be cached.
     * @param string $object_type Object type.
     * @param string $uri The requested URI.
     */
    function wprc_process_cache_relations( $cache_id, $data, $object_type, $uri ) {
    	if ( ! isset( $data['data'] ) || false === strpos( $uri, '/wp-json/wc/v2/products' ) ) {
    		return;
    	}
    
    	$caching = \WP_Rest_Cache_Plugin\Includes\Caching\Caching::get_instance();
    
    	if ( isset( $data['data']['id'] ) ) {
    		// Single item.
    		$caching->insert_cache_relation( $cache_id, $data['data']['id'], 'my-post-type' );
    		return;
    	}
    
    	foreach ( $data['data'] as $record ) {
    		if ( ! is_array( $record ) || ! isset( $record['id'] ) ) {
    			continue;
    		}
    		$caching->insert_cache_relation( $cache_id, $record['id'], 'my-post-type' );
    	}
    }
    
    add_action( 'wp_rest_cache/process_cache_relations', 'wprc_process_cache_relations', 10, 4 );
    //flushing products cache
    function wc_flush_products_caches() {
    	// \WP_REST_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_cache_by_endpoint( '/wp-json/wc/v3/products', \WP_REST_Cache_Plugin\Includes\Caching\Caching::FLUSH_LOOSE );
        \WP_REST_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_related_caches( $post_id, $object_type );
    }
    add_action( 'woocommerce_new_product', 'wc_flush_products_caches', 10, 1 );
    add_action( 'woocommerce_update_product', 'wc_flush_products_caches', 10, 1 );
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Richard Korthuis

    (@rockfire)

    Hi @libca1993

    Thank you for using our plugin!

    With simply looking at your code (so I have not tested it) there are some things I notice:

    In the function wc_flush_products_caches() you are calling the function delete_related_caches with variables which do not exist in your function, so this will not work.

    But I think you don’t need to make that function call work, because if you have correctly set the object type through wp_rest_cache/determine_object_type the flushing of related caches will automatically work because the WordPress hook save_post will be fired and our plugin will process it. Have you checked if the object type is correctly saved to the database for the cache records?

    Thread Starter libca1993

    (@libca1993)

    Hi

    Thank you for your response,

    I have tried to modify the object type to product but at this moment i didn’t modify for ‘product’ on object type is coming as simple or variable, But when i try to update a product the cache for that product is not being cleared.

    is there anything i need todo?

    here is the screen shot Screen Shoot

    /**
     * Register 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);
    
    function wprc_add_v2_wc_endpoints( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'wc/v2' ] ) || ! in_array( 'products', $allowed_endpoints[ 'wc/v2' ] ) ) {
            $allowed_endpoints[ 'wc/v2' ][] = 'products';
        }
        return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_v2_wc_endpoints', 10, 1);
    
    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);
    
    function wprc_v2_add_cacheable_request_headers( $cacheable_headers ) {
        $cacheable_headers['wc/v2/products'] = 'Authorization';
        return $cacheable_headers;
    }
    add_filter('wp_rest_cache/cacheable_request_headers', 'wprc_v2_add_cacheable_request_headers', 10, 1);
    
    function flutter_multi_vendor_wc_endpoints( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'api/flutter_multi_vendor' ] ) || ! in_array( 'product', $allowed_endpoints[ 'api/flutter_multi_vendor' ] ) ) {
            $allowed_endpoints[ 'api/flutter_multi_vendor' ][] = 'product';
        }
        return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'flutter_multi_vendor_wc_endpoints', 10, 1);
    // Determine object type
    function wc_determine_object_type( $type, $cache_key, $data, $uri ) {
    
        if(strstr($uri,'category')){
            return 'category';
        }
        else if(strstr($uri, 'variations')){
            return 'variations';
        }else if ('/wp-json/wc/v2/products/attributes' === substr( $uri, 0, 34)){
            return 'attritubes';
        } else if  (strstr($uri, 'tags')){
            return 'tags';
        }else if ('/wp-json/api/flutter_multi_vendor/product-categories' === substr( $uri, 0, 52 ) ){
             return 'category';
         }
        return $type;
    }
    add_filter( 'wp_rest_cache/determine_object_type', 'wc_determine_object_type', 10, 4 );
    • This reply was modified 2 years, 1 month ago by libca1993.
    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @libca1993

    The object type should correspond with the post type (or taxonomy type) that is in the REST response. If I look at your screenshot I can see you have two different object types (variable and simple) for the same endpoint (only the orderby parameter is different). I am pretty sure this is incorrect.

    Once a post type is created or updated our plugin will find all related caches based upon the object type. So if the object type does not correspond with the post type, it will not clear that cache record. So the first thing you have to do is make sure those oject types are correct.

    N.B. In order to make sure you’re testing correctly it would be wise to first manually delete the incorrect cache records (delete, not flush)

    Thread Starter libca1993

    (@libca1993)

    Hi @rockfire

    So for the viriable and simple should classify them as product?

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @libca1993

    I am not familiair with the endpoint you are using, but product sounds better than variable and/or simple

    Thread Starter libca1993

    (@libca1993)

    Hi @rockfire

    I also tried to clear cache using \WP_REST_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_related_caches( $post_id, $object_type );

    Can you please provide me with and an example i’m falling to understand how use it. A sample code might help alot

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @libca1993

    Well that is actually pretty straightforward, you simply have to provide the $post_id (i.e. the ID of the post you want the related caches for to be cleared) and the object_type (i.e. the post type of the post).

    But as said, you wouldn’t have to use that function if your object type is saved correctly with the cache. That way our code will handle the clearing of related caches for you, by hooking in to the save_post hook of WordPress core. You can see how we do this here: https://github.com/acato-plugins/wp-rest-cache/blob/master/includes/caching/class-caching.php#L313-L332

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Clearing only affected or related caches’ is closed to new replies.