Created
April 14, 2026 04:06
-
-
Save vapvarun/305ae7c7c7f7bdcdd2151909dd42765c to your computer and use it in GitHub Desktop.
Configure a CDN for WooCommerce (woocustomdev.com)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /cart/* | |
| /checkout/* | |
| /my-account/* | |
| /wp-admin/* | |
| /wp-login.php | |
| /wp-json/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (http.cookie contains "wordpress_logged_in_") or | |
| (http.cookie contains "wp_woocommerce_session_") or | |
| (http.cookie contains "woocommerce_items_in_cart") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_action( 'woocommerce_product_set_stock', 'purge_product_from_cdn' ); | |
| add_action( 'woocommerce_variation_set_stock', 'purge_product_from_cdn' ); | |
| add_action( 'save_post_product', 'purge_product_from_cdn' ); | |
| function purge_product_from_cdn( $product_or_id ) { | |
| $product_id = is_object( $product_or_id ) ? $product_or_id->get_id() : $product_or_id; | |
| $product = wc_get_product( $product_id ); | |
| if ( ! $product ) { | |
| return; | |
| } | |
| $urls_to_purge = [ get_permalink( $product_id ) ]; | |
| // Also purge category archives where this product appears. | |
| $cat_ids = wc_get_product_cat_ids( $product_id ); | |
| foreach ( $cat_ids as $cat_id ) { | |
| $term = get_term( $cat_id, 'product_cat' ); | |
| if ( $term && ! is_wp_error( $term ) ) { | |
| $urls_to_purge[] = get_term_link( $term ); | |
| } | |
| } | |
| // Purge the shop page. | |
| $shop_page_id = wc_get_page_id( 'shop' ); | |
| if ( $shop_page_id > 0 ) { | |
| $urls_to_purge[] = get_permalink( $shop_page_id ); | |
| } | |
| cloudflare_purge_urls( $urls_to_purge ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment