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 val

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

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

Share Improve this question edited May 20, 2020 at 19:06 TVBZ asked May 20, 2020 at 16:11 TVBZTVBZ 1298 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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', '' );

    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742415426a4439648.html

相关推荐

  • 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 val

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信