Add and save a text field in WooCommerce customer order detail pages

心已入冬 提交于 2021-02-10 04:41:32

问题


I have been able to add a custom text input field, in woocommerce's order details page, but I can't save the data submitted data.

This is the code:

add_action( 'woocommerce_order_details_before_order_table', 'add_custom_Field',10,2 );
function add_custom_Field( $order_id) {
    $user = wp_get_current_user();
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
    ?>
<form method="post">
     <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <label for="custom_URL"><?php _e( 'URL', 'woocommerce' ); ?></label>
        <input type="text" name="custom_URL" id="custom_URL" value="<?php echo esc_attr( $order_id->custom_URL ); ?>" />
    </p>
        <input type="submit" name="test" id="test" value="RUN" /><br/>
</form>
<?php

function submit()
{
    update_user_meta( $user_id, 'custom_URL', sanitize_text_field( $_POST['custom_URL'] ) );
    echo "Your function on button click is working";
}

   if(array_key_exists('test',$_POST)){
       submit();
    }
}   

Do you know what I'm doing wrong?


回答1:


The following will allow you to add a user custom field to customer order detail and to save its value on submission:

// Display user custom field
add_action( 'woocommerce_order_details_before_order_table', 'add_user_custom_url_field_to_order' );
function add_user_custom_url_field_to_order( $order ) {
    global $current_user;

    $custom_url = get_user_meta( $current_user->ID, 'custom_URL', true );
    ?>
    <form method="post">
         <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
            <label for="custom_URL"><?php _e( 'URL', 'woocommerce' ); ?></label>
            <input type="text" name="custom_URL" id="custom_URL" value="<?php echo $custom_url; ?>" />
        </p>
        <input type="submit" name="submit-custom_URL" value="<?php _e('RUN', 'woocommerce'); ?>" /><br/>
    </form>
    <?php
}

// Save the field as custom user data
add_action( 'template_redirect', 'save_user_custom_url_field_from_order' );
function save_user_custom_url_field_from_order() {
    global $current_user;

    if( isset($_POST['custom_URL']) ){
        update_user_meta( $current_user->ID, 'custom_URL', sanitize_url( $_POST['custom_URL'] ) );
        wc_add_notice( __("Submitted data has been saved", "woocommerce") );
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


Note based on your comment:

If the custom url has to be different for each order, you can't save it as user meta data, but instead as $order item meta data... But the code will be slightly different and you will need to define how will be your custom urls.



来源:https://stackoverflow.com/questions/62777322/add-and-save-a-text-field-in-woocommerce-customer-order-detail-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!