最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - Mini cart in header using divi is not getting updated on changing the cart items count:

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Jan 31, 2018 at 8:23 devgeekdevgeek 1611 silver badge5 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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; 
}
发布评论

评论列表(0)

  1. 暂无评论