I have a custom input field in the WooCommerce cart for every cart item. When somebody clicks update cart I want to send the extra data to the server and process that data and add it to the current cart.
I can’t find anywhere how to do this. Is this even possible?
I tried:
woocommerce_update_cart_action_cart_updated
in the $_POST variable the custom data is not available
Trough custom javascript
Run a ajax request every time the input field get’s changed. But this is interfering with the regular way to update the WooCommerce cart. Lot’s of bugs…
A hook before updating the cart
I can’t find if there’s a hook for sending extra data before updating the cart.
2
Answers
In the end the solution was quite simple. Since the cart is a regular HTML form you can add custom input data. When the cart is updated all the form data is serialised and sent trough with ajax. You just need to set the right name attribute on the input field.
It took the following steps:
Give it the right name. Like this:
Use the hook:
In this hook you can easily access the $_POST['cart'] variable and do your stuff. For example:
Et voilĂ . Now when you fill in the input field and update the cart, the data is stored in the cart. Now for example you can save the extra cart data on storing a new order or use the data to calculate a fee.