/cart/addItem does not update visible cart-Item-Count #10705
-
I have a Javascript-component running in a html-Element within a default wooCommerce-Page that has the default navigation and basket-Item in the header of the page. When the Javascript-component calls /cart/addItem using the storeAPI, I would like that the basket-item's counter updates so that the user can see that the product has reached the basket successfully. However, the basket keeps showing the "old" count and - using mouseover - is pretending that no product has been added. Only after a page reload the correct count is shown. Is there a way to make wooCommerce update the shown count of products in the basket without a forced page reload and without changing the codebase of wooCommerce (e.g. adding filters etc.) Best regards |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 1 reply
-
Hi @elaspix, thanks for your question, I understand your cart isn't updating following a request to the store API. I think this is expected behaviour since we wouldn't update any UI based on an API request alone, the code starting the request should use the data returned to update the UI, but with a little more information we may be able to give some pointers to figure this out anyway. Could you confirm what you mean by:
What is the basket-Item you're referring to? Is it the Mini Cart Block? Could you maybe share a screenshot if possible? |
Beta Was this translation helpful? Give feedback.
-
As @opr said, you should either:
|
Beta Was this translation helpful? Give feedback.
-
@senadir |
Beta Was this translation helpful? Give feedback.
-
The events presented above are meant for that, you would trigger them and they would cause a legacy mini cart refresh. Newer mini cart provide much more functionality. |
Beta Was this translation helpful? Give feedback.
-
ah ok, thank you. But this means I must use the mini cart block and cannot keep the current default wooCommerce header with cart-icon (which has all I need already, except the updating problem)? |
Beta Was this translation helpful? Give feedback.
-
Using events is the only way to get this and keep the old cart yes. |
Beta Was this translation helpful? Give feedback.
-
I have found that an event is registered to the body-element called: updated_wc_div |
Beta Was this translation helpful? Give feedback.
I have found that an event is registered to the body-element called: updated_wc_div
If this event is called after I have added items to the cart using StoreAPI's /cart/addItem, the cart-count updates as wished.
I call this event via javascript using
jQuery("body").trigger("updated_wc_div")