Generally, when you create Woocommerce supported themes from the scratch, you need to show a cart in the top header. Mostly a tiny piece of code is used to show the cart. But not sufficient when you want to update cart item and price automatically when a product added or removed. That is why you need to ajaxify cart.
How to Ajaxify Woocommerce Cart
Generally, this code will show a cart in themes. Although it doesn’t update automatically cart item and price.
<a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf ( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
Rather, to update price and product automatically, add this code in functions.php, then it will update cart when a product added or removed.
First, you need to add a custom function using add_filter to current Woocommerce function “add_to_cart_fragments”.
Second, use ajax so that the script will not send a request to another page. According to PHP manual “The ob_start() function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.”
The $fragments parameter gets the ob_get_clean() to obtain current buffer contents and deletes the output buffer.