1

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 that handles the saving:

// Hook save action to database
add_action('woocommerce_process_product_meta', 'woocommerce_save_fields');  

// Save field to database
function woocommerce_save_fields($post_id)
    {
        $myField = $_POST['field_name'];
        if (!empty($myField))
          update_post_meta($post_id, 'field_name', esc_attr($myField), esc_attr($myField));
        else update_post_meta( $post_id, 'field_name', '' );

    }

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

1 Answer 1

0

Okay found it. It was a silly mistake I overlooked. I had 1 attribute (esc_attr($myField)) double inside the update_post_meta() method.

So this function works perfectly:

function woocommerce_save_fields($post_id)
    {
        $myField = $_POST['field_name'];
        if (!empty($myField))
          update_post_meta($post_id, 'field_name', esc_attr($myField));
        else update_post_meta( $post_id, 'field_name', '' );

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.