Update post meta on wordpress and woocommerce

3

I am creating an e-commerce based on tourism where on the payment page the purchased trip is shown along with the number of people who are going to travel. This amount can be changed on the same page and then after the change the traveler's data is rendered as many times as travelers are going to buy. The data are the following:

  • Name.
  • Identification document.
  • Email

This has already been developed in PHP and javascript but the problem is that when the quantity is changed and the fields are rendered, these are not included in the post call when finalizing the purchase.

In PHP I create an array such that:

    $fields['extra_fields'] = array(
'traveller_details' => array(
    'type' => 'traveller_details',
    'required'      => false,
    'label' => __( 'Traveller details' )
    ),
);

Afterwards, I update the post-goal through an action:

function supreme_custom_checkout_field_update_order_meta( $order_id, $posted ){

  if( ! empty( $posted["traveller_details"] ) ){
    update_post_meta( $order_id, "_traveller_details", $posted["traveller_details"] );
  } else {
    delete_post_meta( $order_id, "_traveller_details" );
  }

}
add_action( 'woocommerce_checkout_update_order_meta', 'supreme_custom_checkout_field_update_order_meta', 10, 2 );

My question is how I could capture the values before sending the post and send them in the post call.

    
asked by David 02.10.2016 в 23:10
source

1 answer

1
  

My question is how I could capture the values before sending the post and send them in the post call.

Just add the key 'default' to the array and as a value you place the result of get_post_meta() :

$datos = get_post_meta( $order_id, '_traveller_details' , true );

$fields['extra_fields'] = array(
        'traveller_details' => array(
        'type'              => 'traveller_details',
        'required'          => false,
        'label'             => __( 'Traveller details' )
        'default'           => $datos // si el resultado es un string. Si es array sería $datos['algo']
    ),
);

With that the field will come with the values.

Another thing: you say that ...

  

In PHP I create an array such that:

I assume that you wrote that array so "bare" to summarize, but if it is not so you must place it inside a function and assign it a filter.

    
answered by 17.10.2016 в 01:21