How to update custom field in WooCommerce
I have added 2 custom fields in a WooCommerce installation using the instructions on this link. It works to save the values but I’m having some problem changing/updating values.
Here is my code:
// Hook save action to database
add_action('woocommerce_process_product_meta', 'woocommerce_save_3Dconfig_fields');
// ... there is more (irrelevant) code to create the fields here
// Save field to database
function woocommerce_save_3Dconfig_fields($post_id)
{
$configurator_path = $_POST['path_3Dconfigurator'];
if (!empty($configurator_path))
update_post_meta($post_id, 'path_3Dconfigurator', esc_attr($configurator_path), esc_attr($configurator_path));
else update_post_meta( $post_id, 'path_3Dconfigurator', '' );
$product_uid = $_POST['product_UID'];
if (!empty($product_uid))
update_post_meta($post_id, 'product_UID', esc_attr($product_uid), esc_attr($product_uid));
else update_post_meta( $post_id, 'product_UID', '' );
}
The if (!empty(...))
saves the field value when first adding it.
The else ...
allows to empty the field and delete it’s value.
But I still can’t just change the value. And after deleting, I can’t save a new value to the fields.
I know my problem is inside this function. But I can’t figure it out.
Any tips on this?
Thanks
Leave an answer