I am developing a woocommerce widget which will show cart subtotal, cart total, cart items, and shipping total its working fine but what I want to do is to update shipping total as well as cart total whenever I toggle between shipping methods with use of ajax. Currently it updates itself only after page reload. Is there any hook available for this purpose ?
Leave a Reply
You must be logged in to post a comment.
You can do this with the
add_to_cart_fragments
filter.My implementation only updates the number of items shown with AJAX but it can be used to update totals, etc as well. This is the normal code in the template that displays the cart details:
This is the filter added in
functions.php
:There are definitely some resources / documentation on this out there – I remember using some for reference when I wrote this code but they are a bit tough to google for.
For anyone else wondering about this, there is official documentation on the WooThemes site HERE. Apologies for not being able to post this in a comment, I don’t have enough reputation points.
The code seems to have been updated though. They now use
get_cart_contents_count()
instead ofcart_contents_count
, amongst other things.Here are the newer code snippets, copied directly from the WooThemes documentation (you can obviously edit the code to display whatever cart information you like, but make sure you make the same edits in both snippets):
To display the cart contents and total in your template use something like:
To ajaxify your cart viewer so it updates when an item is added (via ajax) use: