简体   繁体   中英

How to update a post custom value in wordpress/woocommerce

I have a custom fields that I have to update in the woocommerce shopping cart, when the user completes his order

the following function:

$moreinfo_values = get_post_meta($_product->id,'more_info_data');

gets me the array:

    Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [m] => NSW
                        [o] => Sydney
                        [d] => Something
                        [p] => Array
                            (
                                [0] => 13/08/2015
                                [1] => 14/08/2015
                            )

                        [n] => 9am - 4pm
                        [f] => 20
                    )

                [1] => Array
                    (
                        [m] => SA
                        [o] => Adelaide
                        [d] => Hotel SA 5000
                        [p] => Array
                            (
                                [0] => 02/04/2015
                            )

                        [n] => 9am - 4pm
                        [f] => 12
                    )


            )

    )

I need to update [f] => 12 with the following function:

update_post_meta( $_product->id, 'more_info_data', 'f', '100'  );

But I dont seem to figure it out how this function works.

I really appreciate your help on advance

Based on your updates i created simple function which doing update_post_meta , as per my knowledge i don't see any option to update the value using your function like update_post_meta( $_product->id, 'more_info_data', 'f', '100' );

You can make the condition inside the function update_custom_post_meta_data for your wish,am not sure doesn't know the exactly which data to split the condition as common and hence it is up to you.

so i created alternate function like

function update_custom_post_meta_data($id, $meta_name, $array_key, $array_value) {
    // Get the data from meta name
    $get_meta_data = get_post_meta($id, $meta_name, true);
    $new_array = array();
    foreach ($get_meta_data as $key => $values) {
        if (isset($values[$array_key])) {
            $values[$array_key] = $array_value;
        }
        $new_array[$key] = $values;
    }
    update_post_meta($id, $meta_name, $new_array);
}

You can use this function by replacing the function name of your existing one to this one

update_custom_post_meta_data( $_product->id, 'more_info_data', 'f', '100'  );

It will works for your requirements.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM