jmccole83
12/12/2017 - 5:13 PM

WooCommerce - AJAX Cart

Copy the first snippet to your themes functions.php file, copy cart.php to your themes template for where you need to output it.

<?php
if ( class_exists( 'WooCommerce' ) ): ?>
<a class="cart-contents basket" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>">
  <span class="hidden-xs"><?php echo sprintf ( _n( '%d</span>' . ' <span class="item-count"> item</span>', '%d</span>' . ' <span class="item-count"> items</span>', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - </span>
  <span class="basket-total"><?php echo WC()->cart->get_cart_total(); ?></span>
</a>
<?php endif; ?>
// AJAX update cart totals
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
    ob_start();
    ?>
<a class="cart-contents" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><span class="hidden-xs"><?php echo sprintf (_n( '%d</span>' . ' <span class="item-count"> item</span>', '%d</span>' . ' <span class="item-count"> items</span>', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - </span><?php echo WC()->cart->get_cart_total(); ?></a>
    <?php

    $fragments['a.cart-contents'] = ob_get_clean();

    return $fragments;
}