I need to re-populate mini-cart when product added via ajax add to cart. I manage to update cart quantity with filter woocommerce_add_to_cart_fragments like this:
当通过ajax向cart添加产品时,我需要重新填充迷你购物车。我设法用woocommerce_add_to_cart_fragment这样的过滤器更新cart数量:
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 is
我的HTML标记是
<div class="cart-contents">
<?php echo WC()->cart->get_cart_contents_count(); ?>
</div>
Bellow that is hidden div witch showing on .cart-contents hover
这是隐藏的div女巫出现在。cart-contents悬停
<div class="header-quickcart"><?php woocommerce_mini_cart(); ?></div>
I want to update this div content same way or similar to woocommerce_add_to_cart_fragments. Or should I change HTML markup and hold everything in 1 div? What is common way or best practice to doing that?
我希望以与woocommerce_add_to_cart_fragment类似的方式更新这个div内容。或者我应该更改HTML标记并将所有内容保存在一个div中?通常的做法或最佳实践是什么?
1 个解决方案
#1
9
Ok so I just realized that I can use woocommerce_add_to_cart_fragments filter 2 times, like so:
我刚刚意识到我可以使用woocommerce_add_to_cart_fragment过滤器2次,如下所示:
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 updating quantity and aother refreshing mini-cart view.
首先更新数量和其他刷新的迷你购物车视图。
#1
9
Ok so I just realized that I can use woocommerce_add_to_cart_fragments filter 2 times, like so:
我刚刚意识到我可以使用woocommerce_add_to_cart_fragment过滤器2次,如下所示:
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 updating quantity and aother refreshing mini-cart view.
首先更新数量和其他刷新的迷你购物车视图。