Skip to content

Instantly share code, notes, and snippets.

@mikaelz
Created June 28, 2017 07:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mikaelz/418a337dace188ba95b476ce65abfe0c to your computer and use it in GitHub Desktop.
Save mikaelz/418a337dace188ba95b476ce65abfe0c to your computer and use it in GitHub Desktop.
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