woocommerce 2.0: ajax refresh of mini cart

Things are alot harder now if you want to hid the mini cart if there are no items in the cart. If things doesn’t work out of the box in woocommerce 2.0, create your own mini cart widget.

1) duplicate the mini cart from plugins/woocommerce/classes/widgets/class-wc-widget-cart.php to your theme dir.

2) rename the widget class to something-else.php and change the class and function names accordingly.

3) In about line 62 of something-else.php, replace “echo $before_widget” with

echo preg_replace('/sidebar-widget/', 'sidebar-widget widget_shopping_cart', $before_widget);

4) in your theme’s function.php, register your newly created widget, eg

add_action('widgets_init', 'my_register_widgets');
function my_register_widgets() {

5) go into your admin panel and replace your mini cart widget with your newly created widget.

Conclusion: I still don’t like this hack because user still see the widget appearing first and hidden suddenly after the page is loaded. In the woocommerce 1.x, adding widget_shopping_cart class is the trick to get wooocommerce to refresh the cart when items are added to the cart. In woocommece 2.x, they added extra classes and div to get the ajax to work, there are also a lot more ugly javaascript in cart-fragments.js. I don’t think they should offload the job of hiding the widget to javascript.

Author: bpeh

Bernard Peh is a great passioner of web technologies and one of the co-founder of Sitecritic.net Website Design and Reviews. He works with experienced web designers and developers everyday, developing and designing commercial websites. He specialises mainly in SEO and PHP programming.