Skip to content
Advertisement

How to save from WooCommerce checkout a custom checkbox field state?

I have a problem with the update_post_meta function. I have a user submitted value, which I pass via $_POST and then saving to post meta.

All is working fine, but when the value is ‘0’ the post meta is not updated.

This is My code:

JavaScript

Does anyone have any idea what might be wrong?

Advertisement

Answer

Since WooCommerce 3, here below is the best way to save your custom checkout checkbox field value as order meta data (including when the checkbox is unchecked):

JavaScript

Now as user meta data is used by WC_Checkout get_value() method in your first function on:

JavaScript

So if you want the submitted value to be displayed on checkout page for the next purchase, you will need to save that custom checkout field also as user meta data using instead the following:

JavaScript

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement