Capslock10
Capslock10

Reputation: 806

Add a additional detail to the order. Woocommerce

I added the dropdown list on the checkout page and I want to add the dropdown list value to the order detail. I tried to update the value to the meta data, but my code is not work.

Here is my code: (On Theme)

     <from>
     <select class = "drop-down-list" name = "drop-down-list"  id="drop-down-list" >
    <?php 
      $age = array(..,...,...);
      $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);

        //Print out this <option value ='myName'>

        echo '<option value =\'' . $author_info->display_name . '\'>' 
          . $author_info->display_name . '</option>';
    }

    } else {
       echo 'No authors found';
    }

?>
</select>
</from>

And here is my plugin:

add_action( 'woocommerce_checkout_order_processed', 'my_custom_checkout_field_update_order_meta_lmc' );

function my_custom_checkout_field_update_order_meta_lmc_ao($post_id) {
    global $woocommerce, $post;
    $order = new WC_Order($post_id);

    //to escape # from order id
    echo 'test this function';
    //$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'] ) );

    }
}
 do_action('woocommerce_checkout_order_processed');

I used these code, but the data didn't update. I try put all the code on the same file, but it still doesn't work. How can I do this?


Here is the <select> enter image description here

Test the function: enter image description here

Thanks

Upvotes: 0

Views: 972

Answers (1)

Ratnakar - StoreApps
Ratnakar - StoreApps

Reputation: 4351

I've modified your plugin's code a little bit. Try this:

add_action( 'woocommerce_checkout_order_processed', 'my_custom_checkout_field_update_order_meta_lmc', 10, 2 );
function my_custom_checkout_field_update_order_meta_lmc_ao($post_id, $posted) {
    global $woocommerce, $post;
    $order = wc_get_order( $post_id );

    //to escape # from order id

    //$order_id = trim(str_replace('#', '', $order->get_order_number()));
    if ( ! empty( $posted['drop-down-list'] ) ) {
       update_post_meta( $order->id, 'drop-down-list', sanitize_text_field( $posted['drop-down-list'] ) );

    }
}

One more thing, you've used <from>...</from> instead of <form>...</form>. If your drop down box is appearing in checkout form then there is no need for <form> tag.

Upvotes: 1

Related Questions