0

I am building a woocommerce site and I have some input fields the on the product page. one is for recipient email (its for a gift car). Everything works, and the recipient email is saved on the order, but I want to be able to pass the email address to a function to update_meta_data so that I can use in other parts of the code (namely to send out a separate email to the recipient to redeem the card).

here is the function I am trying to use:

add_action('woocommerce_checkout_update_order_meta',function( $order_id, $posted ) {

    $order = wc_get_order( $order_id );
    $order->update_meta_data( 'recipient_email', "joe@acme.com" );
    $order->save();
} , 10, 2);

Where "joe@acme" is, I want to pass the value from the form. the form is rendered using a custom form plugin. the html of the form looks like this:

<input name="recipient_email" id="recipient_email" data-type="text" data-req="on" data-message="no email" maxlength="255" style="width: 100%; padding: 0px;" type="text">

As I said, the form works to save the recipient email to the order, but not as post meta data, so I don't know how to retrieve and store as a variable that I can use for for other parts of the code.

0

You can use the $posted parameter to get the right value in your function.

add_action('woocommerce_checkout_update_order_meta',function( $order_id, $posted ) {

$order = wc_get_order( $order_id );
$order->update_meta_data( 'recipient_email', $posted['recipient_email'] );
$order->save();
} , 10, 2);

Verify that $posted is an array not an object with a var_dump on $posted. if it's an object replace $posted['recipient_email'] with $posted->recipient_email

Hope it helps :)

  • thanks. I think I am trying to do this at the wrong place in the workflow. either as array or object, it doesn't return anything. – bamabacho Nov 8 '17 at 22:02
  • You need to make à var_dump($posted); to check if the variable is populated. Did not test it before my answer – Benoti Nov 8 '17 at 22:07
  • You can also try to use the save_post action to save the form values. – Benoti Nov 8 '17 at 22:08
  • var dump on $posted is empty. – bamabacho Nov 8 '17 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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