2

I am having a wordpress site which is using the Divi theme. When I update the number of items in the cart, the mini cart on the header is not getting updated accordignly. However, the number of items in the mini cart are getting updated on re-loading the page.

Divi is using the below function for updating the cart,

if ( ! function_exists( 'et_show_cart_total' ) ) { 
       function et_show_cart_total( $args = array() ) { 
               if ( ! class_exists( 'woocommerce' ) || ! WC()->cart ) { 
                       return; 
               } 

               $defaults = array( 
                       'no_text' => false, 
               ); 

               $args = wp_parse_args( $args, $defaults ); 

               $items_number = WC()->cart->get_cart_contents_count(); 

               $url = function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : WC()->cart->get_cart_url(); 

               printf( 
                       '<a href="%1$s" class="et-cart-info"> 
                               <span>%2$s</span> 
                       </a>', 
                       esc_url( $url ), 
                       ( ! $args['no_text'] 
                               ? esc_html( sprintf( 
                                       _nx( '%1$s Item', '%1$s Items', $items_number, 'WooCommerce items number', 'Divi' ), 
                                       number_format_i18n( $items_number ) 
                               ) ) 
                               : '' 
                       ) 
               ); 
       } 
}

How can I update the mini cart on updating the number of items in the cart on ajax call?

Please can anyone help?

3
  • Did you added global $woocmmerce ?
    – DHL17
    Commented Jan 31, 2018 at 11:32
  • Try as DHL17 is suggesting.
    – Aftab H.
    Commented Jan 31, 2018 at 12:16
  • Thanks but I kind of fixed my issue as per my answer below.
    – devgeek
    Commented Feb 2, 2018 at 6:32

1 Answer 1

4

I fixed the issue by adding the below in my functions.php:

add_filter( 'woocommerce_add_to_cart_fragments', 'your_custom_functions', 10, 1 ); 

function your_custom_functions( $fragments ) {
    $fragments['.your_cart_class'] = '' . WC()->cart->get_cart_contents_count() . ' Items';
    return $fragments; 
}
1
  • 1
    It's important to note if your theme requires html, you may want to return that as well. For example, using Divi I actually had to make a copy of their show_cart_total function, return the formatted html string (sprintf) and then set the desired fragment to that. This article has some more info: iconicwp.com/blog/…
    – Gabriel
    Commented Apr 21, 2019 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.