fatBuzz of Fatbuzz Development Team
12/18/2017 - 10:18 AM

WooCommerce - AJAX cart

Add ajax-cart.php to functions.php, add cart.php to the theme's template where it is required.

<?php
<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 item', '%d items', 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 
// 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 item', '%d items', 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;
}