John Davidson

php - Get data from custom select field and paste to New Order E-mail - Woocommerce

0 comments
Message:


On my Checkout page I have an additional select type field with several options to choose from:



select name="_billing_rola"

option value="option1" > Example 1

option value="option2" > Example 2

option value="option3" > Example 3



I'm trying to insert this information into a new order email. So far I've been able to get a half-way result with code like this:


// Short code [woo_mb_custom_code type="rola"]
if (isset($attr['type']) && $attr['type'] == 'rola') {
$order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id;

$rola = get_post_meta($order_id, "_billing_rola", true);

echo '<p>' . $rola . '</p>';
}

As you can see, $rola displays values from the option value items i.e. option1, option2, option3 depending on what the customer chooses.


What I would like to get is that the last line of code displays Example 1, Example 2, Example 3 values depending on the selection.



My Answer:
"custom_select_field"
option value="option1">Option 1
option value="option2">Option 2
option value="option3">Option 3


To get the selected value from this custom select field and paste it into the New Order E-mail in WooCommerce, you can use the following code snippet:

php
add_action( 'woocommerce_email_order_meta', 'custom_select_field_order_meta', 10, 3 );
function custom_select_field_order_meta( $order, $sent_to_admin, $plain_text ) {
$custom_select_field = get_post_meta( $order->get_id(), 'custom_select_field', true );

if ( $custom_select_field ) {
echo '

Custom Select Field: ' . $custom_select_field . '

';
}
}


You can add this code to your theme's functions.php file or a custom plugin. This code will retrieve the selected value from the custom select field for each order and display it in the New Order E-mail.

Rate this post

4 of 5 based on 9564 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us