Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Update WooCommerce mini cart count via AJAX
<?php
add_action('wp_ajax_cart_count', 'custom_cart_count');
add_action('wp_ajax_nopriv_cart_count', 'custom_cart_count');
function custom_cart_count() {
echo WC()->cart->cart_contents_count;
wp_die();
}
add_action('wp_enqueue_scripts', 'custom_scripts');
function custom_scripts()
{
wc_enqueue_js( "
$.ajax({
url: '".admin_url('admin-ajax.php')."',
data: {
'action': 'cart_count'
},
success: function(resp) {
$('REPLACE_WITH_COUNT_ELEMENT_SELECTOR').text(resp);
}
});
");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment