Created
June 28, 2017 07:43
-
-
Save mikaelz/418a337dace188ba95b476ce65abfe0c to your computer and use it in GitHub Desktop.
Update WooCommerce mini cart count via AJAX
This file contains 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
<?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