I override the Woocommerce checkout page
to make a dropdown list on it. And I want to save the meta data of the dropdown list to the wp_postmeta
.
I am trying to use these code, but it doesn't work. (form-checkout)
Making the downdrop list, and get the admin name.
<select class = "drop-down-list" name = "drop-down-list" >
<!-- Search the administrator user -->
<?php
$args = array (
'role' => 'administrator',
'order' => 'ASC',
'orderby' => 'display_name',
'search' => '*'.esc_attr( $search_term ).'*',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'description',
'value' => $search_term ,
'compare' => 'LIKE'
)
)
);
$wp_user_query = new WP_User_Query($args);
// Get the results
$authors = $wp_user_query->get_results();
// Check for results
if (!empty($authors)) {
// loop through each author
foreach ($authors as $author)
{
// get all the user's data
$author_info = get_userdata($author->ID);
echo '<option value =\'' . $author_info->display_name . '\'>' . $author_info->display_name . '</option>';
}
} else {
echo 'No authors found';
}
?>
</select>
Update the wp_postmeta
when click the place order button.
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta_lmc' );
function my_custom_checkout_field_update_order_meta_lmc() {
global $woocommerce, $post;
$order = new WC_Order($post->ID);
//to escape # from order id
$order_id = trim(str_replace('#', '', $order->get_order_number()));
if ( ! empty( $_POST['drop-down-list'] ) ) {
update_post_meta( $order_id, 'drop-down-list', sanitize_text_field( $_POST['drop-down-list'] ) );
}
}
But it doesn't work. How can I do it? Thx.
The first code you provided where you display a list of all admins in what file did u place that code? It should be placed between the form tag in the form-checkout.php
located in your theme folder under woocommerce/checkout
or in the plugin templates/checkout