简体   繁体   中英

In Woocommerce order page, create a custom text field related to order id

In this code comes come an answer to one of my previous questions, and the custom url data is related to the user data.

I need that the custom url have to be different, related to the order id (ex. 100 order, 100 different custom url, one per order's page).

// 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']) && ! empty($_POST['custom_URL']) ){
        update_user_meta( $current_user->ID, 'custom_URL', sanitize_url( $_POST['custom_URL'] ) );
        wc_add_notice( __("Your custom URL has been saved saved", "woocommerce") );
    }
}

Here is the way to save the data as order custom meta data instead of user meta data:

add_action( 'woocommerce_order_details_before_order_table', 'add_custom_url_field_to_order' );
function add_custom_url_field_to_order( $order ) {
    $custom_url = $order->get_meta( 'custom_URL' );
    ?>
    <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="hidden" name="the_order_id" value="<?php echo $order->get_id(); ?>" />
        <input type="submit" name="submit-custom_URL" value="<?php _e('RUN', 'woocommerce'); ?>" /><br/>
    </form>
    <?php
}

// Save the field
add_action( 'template_redirect', 'save_custom_url_field_from_order' );
function save_custom_url_field_from_order() {
    if( isset($_POST['custom_URL']) && ! empty($_POST['custom_URL']) && isset($_POST['the_order_id']) ){
        update_post_meta( esc_attr($_POST['the_order_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.

Now each custom url will be related to an order, but not anymore to user meta data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM