在Woocommerce订单页面中,创建与订单ID相关的自定义文本字段 [英] In Woocommerce order page, create a custom text field related to order id

查看:165
本文介绍了在Woocommerce订单页面中,创建与订单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.

我需要自定义网址必须与订单ID有关(例如100个订单,100个不同的自定义网址,每个订单的页面一个).

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") );
    }
}

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

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.

这篇关于在Woocommerce订单页面中,创建与订单ID相关的自定义文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆