Clearing only affected or related caches
-
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)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘Clearing only affected or related caches’ is closed to new replies.