• Resolved coldrealms65

    (@coldrealms65)


    I am having issues integrating this with Woocommerce. If there are no plans for an official add-on could you please break it down for a novice:

    I use Woody Snippets so ideally, an export of the working snippet/s would be best.

    I truly appreciate your help

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter coldrealms65

    (@coldrealms65)

    So to help, here is my full snippet file:

    /**
     * Register the WooCommerce endpoints so they will be cached.
     */
    function wprc_add_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_wc_endpoints', 10, 1);
    
    function wprc_add_cacheable_request_headers( $cacheable_headers ) {
        $cacheable_headers['wc/v2/products'] = '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/wc/v2/products/categories' === substr( $uri, 0, 34 ) ) {
    		return 'product_cat';
    	} else if ( '/wp-json/wc/v2/products' === 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/wc/v2/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 );

    changed from v3 to v2 as all my hits are on v2 apparently. I show nothing in my “Endpoint API caches”

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @coldrealms65

    Thank you for using our plugin!

    We do have plans for a WooCommerce AddOn, but it is still only a plan. So that could take some time before it is finished.

    I looked at your code snippets and at the WooCommerce REST API v2. What I notice is that there is a difference in the namespace of the v2 endpoint compared to the v3. For v3 the namespace is /wc/v3/, but for v2 it is /wc-api/v2/.
    So in your code snippet you should change every occurence of wc/v2 to wc-api/v2

    Thread Starter coldrealms65

    (@coldrealms65)

    Thanks for getting back to me. Looking forward to the woocommerce integration.

    Over the weekend I traced the requests that I needed specifically cached and all the paths that the external site is poking are rooted at “/wp-json/wc/v2/products”. A few hours after I posted I started to see cache logs in the plugin indicating that it was doing something

    I can also see cache hits on those pages. So apparently not everyone implements things correctly ?? at least I got it to do what I was looking for, and for that alone you rock!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Woocommerce integration’ is closed to new replies.