Search code examples
ajaxwordpresscart

Header mini cart using divi is not getting updated on changing the cart items count: Wordpress


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?


Solution

  • Please try the following code in your functions.php file

    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; 
    }