admin管理员组文章数量:1122846
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', "[email protected]" );
$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.
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', "[email protected]" );
$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.
Share Improve this question asked Nov 8, 2017 at 18:37 bamabachobamabacho 1013 bronze badges2 Answers
Reset to default 0You 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 :)
You can use below code in your active theme functions.php file or active child theme
add_action( 'woocommerce_checkout_update_order_meta', 'save_recipient_email_to_order_meta', 10, 2 );
function save_recipient_email_to_order_meta( $order_id, $posted_data ) {
// Get the recipient email from the posted data
$recipient_email = isset( $posted_data['recipient_email'] ) ? $posted_data['recipient_email'] : '';
// Save the recipient email as post meta data for the order
if ( $recipient_email ) {
update_post_meta( $order_id, 'recipient_email', $recipient_email );
}
}
You will get meta data using order_id
$order_id = 123; // Replace 123 with the actual order ID
// Get the recipient email from the order meta data
$recipient_email = get_post_meta( $order_id, 'recipient_email', true );
本文标签: pluginsGet value from an input field and pass into updatemetadata as metavalue
版权声明:本文标题:plugins - Get value from an input field and pass into update_meta_data as $meta_value 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736286629a1927709.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论