• Resolved johnferz

    (@johnferz)


    Dear All
    Plugin doesn’t work, our code in functions.php:
    Please help to understand why.

    function wprc_add_imserv_prod_posts_endpoint( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'ims-sort/v1' ] ) || ! in_array( 'products', $allowed_endpoints[ 'ims-sort/v1' ] ) ) {
            $allowed_endpoints[ 'ims-sort/v1' ][] = 'products';
        }
        return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_imserv_prod_posts_endpoint', 10, 1);
    
    function wprc_add_cacheable_request_headers( $cacheable_headers ) {
        $cacheable_headers['ims-sort/v1/products'] = 'authorization';
        return $cacheable_headers;
    }
    add_filter('wp_rest_cache/cacheable_request_headers', 'wprc_add_cacheable_request_headers', 10, 1);

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter johnferz

    (@johnferz)

    Changed route to https://abs.imservtest.ru/wp-json/wc/v3/products
    Plugin still doesn’t work.

    function wprc_add_imserv_prod_posts_endpoint( $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_imserv_prod_posts_endpoint', 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);



    Thread Starter johnferz

    (@johnferz)

    mu file exists!

    Thread Starter johnferz

    (@johnferz)

    Found also this

    1. Caching is working with default endpoints in wp/v2, works fine, quite fast, 50-100 ms.
    2. Caching is not working with custom endpoint in wp/v2

    https://abs.imservtest.ru/wp-json/wp/v2/products-ims

    function wprc_add_imserv_prod_posts_endpoint( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'wp/v2' ] ) || ! in_array( 'products-ims', $allowed_endpoints[ 'wp/v2' ] ) ) {
            $allowed_endpoints[ 'wp/v2' ][] = 'products-ims';
        }
        return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_imserv_prod_posts_endpoint', 10, 1);
    
    function wprc_add_cacheable_request_headers( $cacheable_headers ) {
        $cacheable_headers['wp/v2/products-ims'] = 'authorization';
        return $cacheable_headers;
    }
    add_filter('wp_rest_cache/cacheable_request_headers', 'wprc_add_cacheable_request_headers', 10, 1);
    
    function wc_determine_object_type( $type, $cache_key, $data, $uri ) {
    	if ( '/wp-json/wp/v2/products-ims/categories' === substr( $uri, 0, 34 ) ) {
    		return 'product_cat';
    	} else if ( '/wp-json/wp/v2/products-ims' === substr( $uri, 0, 23 ) ) {
    		return 'product';
    	}
    
    	return $type;
    }
    add_filter( 'wp_rest_cache/determine_object_type', 'wc_determine_object_type', 10, 4 );
    
    function wc_flush_category_caches() {
    	\WP_REST_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_cache_by_endpoint( '/wp-json/wp/v2/products-ims/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 );
    • This reply was modified 1 year, 11 months ago by johnferz.
    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @johnferz

    Thank you for using our plugin!

    I know it has already been a month since your last post, so maybe you already solved the issue. But I do see the x-wp-cached-call: served-cache so your endpoint seems to be cached?
    By the way I also noticed your endpoint isn’t returning valid json, but rather a single string of what seems to be escaped json.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Cache custom endpoint woocommerce’ is closed to new replies.