Skip to content

Instantly share code, notes, and snippets.

@wplit
Created March 26, 2025 09:27
Show Gist options
  • Save wplit/75d7501e242b98c0fc14fcf6b1ef9a02 to your computer and use it in GitHub Desktop.
Save wplit/75d7501e242b98c0fc14fcf6b1ef9a02 to your computer and use it in GitHub Desktop.
ensure 'wc_fragment_refresh' is triggered when woocommerce add to cart blocks are used.
<?php
add_action( 'wp_enqueue_scripts', 'ensure_wc_cart_fragments', 20 );
function ensure_wc_cart_fragments() {
wp_enqueue_script( 'wc-cart-fragments' );
}
add_action('wp_enqueue_scripts', 'custom_enqueue_cart_refresh_script');
function custom_enqueue_cart_refresh_script() {
wp_add_inline_script('wc-cart-fragments', "
document.addEventListener('wc-blocks_added_to_cart', function(event) {
jQuery(document.body).trigger('wc_fragment_refresh');
});
");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment