Woocommerce add to cart ajax and mini cart

I need to refill the mini basket when the product is added via ajax to the basket. I am able to update the cart quantity using the woocommerce_add_to_cart_fragments filter as follows:

add_filter( 'woocommerce_add_to_cart_fragments', function($fragments) { ob_start(); ?> <div class="cart-contents"> <?php echo WC()->cart->get_cart_contents_count(); ?> </div> <?php $fragments['div.cart-contents'] = ob_get_clean(); return $fragments; } ); 

And my HTML markup

 <div class="cart-contents"> <?php echo WC()->cart->get_cart_contents_count(); ?> </div> 

Duster that is hidden by a div that shows on .cart-content hover

 <div class="header-quickcart"><?php woocommerce_mini_cart(); ?></div> 

I want to update this div content in the same way or similar to woocommerce_add_to_cart_fragments. Or do I need to change the HTML markup and save everything in 1 div? What is the general way or best practice for this?

+5
source share
1 answer

So, I just realized that I can use the woocommerce_add_to_cart_fragments filter 2 times, for example:

 add_filter( 'woocommerce_add_to_cart_fragments', function($fragments) { ob_start(); ?> <div class="cart-contents"> <?php echo WC()->cart->get_cart_contents_count(); ?> </div> <?php $fragments['div.cart-contents'] = ob_get_clean(); return $fragments; } ); add_filter( 'woocommerce_add_to_cart_fragments', function($fragments) { ob_start(); ?> <div class="header-quickcart"> <?php woocommerce_mini_cart(); ?> </div> <?php $fragments['div.header-quickcart'] = ob_get_clean(); return $fragments; } ); 

First quantity update and another mini-cart update.

+8
source

Source: https://habr.com/ru/post/1258001/


All Articles