*_jemarea_*

Website Development

  • Home
  • Articles
    • All articles
    • Linux
    • WordPress
    • Codeigniter
    • PHP
    • JavaScript
    • MySql
  • About
  • Contact

July 3, 2018 By jerome Leave a Comment

Update Cart in Header Item is Removed

This is a situation that you might experience sometime, therefore I thought it would be a good idea to give you the solution here.

You have build your site using WooCommerce and – to make it easy for the user – you have added a short cart information in the header (via widget or hook).

However, you realise that when an item is removed from the cart, this information in your header is not updated.

First, let’s say this is the DIV you have added to your header:

<div class="short-cart-info">
   <span class="item-num"><?php echo $woocommerce->cart->get_cart_contents_count(); ?></span>
   <span class="total-price">&dollar;<?php echo $woocommerce->cart->total; ?></span>
</div>

Now, add the following in your functions.php

//* Make sure cart amount and item count is updated in header when item is removed from cart
add_filter( 'woocommerce_add_to_cart_fragments', 'my_updated_fragments', 10, 1 ); 
function my_updated_fragments( $fragments ) {
    
    $fragments['div.short-cart-info'] = '<div class="short-cart-info"><span class="item-num">' . WC()->cart->get_cart_contents_count() . '</span><span class="total-price">&dollar;' . WC()->cart->total . '</span></div>';
    
    return $fragments;

}

This should, normally, update your cart information in the header. If not, please tell me in the comment and explain what your set up is (also what other woocommerce plugins you are using).

Enjoy coding!

Filed Under: PHP, WooCommerce, WordPress

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · jemarea.com