Skip to content

Instantly share code, notes, and snippets.

@asalkey
Last active August 22, 2017 23:55
Show Gist options
  • Save asalkey/d4b41f5726889aa41edd to your computer and use it in GitHub Desktop.
Save asalkey/d4b41f5726889aa41edd to your computer and use it in GitHub Desktop.
Woocoomerce: pull total with ajax
// Add an extra key + value
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<div class="total"> <?php echo $woocommerce->cart->get_cart_total(); ?> </div>
<?php
$fragments['total'] = ob_get_clean();
return $fragments;
}
// Create ajax callback function function
your_php_ajax_function(){
$ajax = new WC_AJAX();
$ajax->get_refreshed_fragments( );
}
// Get json data
jQuery.ajax({
type:'POST',
url: '/wp-admin/admin-ajax.php',
data: use_points_processing,
success:function(data){
console.log(data.fragments.total); }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment