保存结帐自定义字段值并将其显示在WooCommerce管理订单中 [英] Save checkout custom field value and display it in WooCommerce admin orders

查看:71
本文介绍了保存结帐自定义字段值并将其显示在WooCommerce管理订单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在结帐中还有一些其他字段,用于向客户询问有关其订单的某些事项的问题.其中大多数是文本输入字段,但只有一个选择"菜单,询问他们如何得知我的客户网站.我一直无法弄清楚如何在所选选项的订单的meta区域中显示值.这是我从主题生成我的functions.php文件中的选择下拉列表的方式.

I have some additional fields in my checkout for asking questions from the customer about certain things regarding their order. Most of these are text input fields but there's one Select menu asking them how they heard about my clients site. I've been unable to figure out how to display the value in the meta area of the order for the selected option. Here's how I'm generating that select drop down in my functions.php file from my theme.

woocommerce_form_field( 'aba_hear', array(
    'type'     => 'select',
    'required' => 'true',
    'class'    => array('hear-class form-row-wide'),
    'label'    => __('How did You Hear About Us?'),
    'options'  => array( // options for <select> or <input type="radio" />
        ''          => 'Please select', // empty values means that field is not selected
        'Instagram' => 'Instagram',
        'Facebook'  => 'Facebook',
        'Yelp'  => 'Yelp',
        'Other' => 'Other',
    )

), $checkout->get_value( 'aba_hear' ) );

现在我要添加一个函数来更新订单元值:

Now I go add a function to update the order meta values:

add_action( 'woocommerce_checkout_update_order_meta', 'aba_checkout_field_update_order_meta' );

function aba_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['aba_hear'] ) ) {
        update_post_meta( $order_id, 'How did You Hear About Us&#63;', sanitize_text_field( $_POST['aba_hear'] ) );
    }
}

最后在订单页面上显示该值:

And finally display the value on the order page:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'aba_checkout_field_display_admin_order_meta', 10, 1 );
function aba_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('How did You Hear About Us&#63;').':</strong> ' . get_post_meta( $order->id, 'Is this a Gift&#63;', true ) . '</p>';
}

最后,这是结帐时选择菜单代码的显示方式:

Lastly here's how the select menu's code appears at checkout:

<p class="form-row hear-class form-row-wide validate-required" id="aba_hear_field" data-priority="">
    <label for="aba_hear" class="">How did You Hear About Us&#063;&nbsp;<abbr class="required" title="required">*</abbr></label>
    <span class="woocommerce-input-wrapper">
        <select name="aba_hear" id="aba_hear" class="select " data-allow_clear="true" data-placeholder="Please select">
            <option value=""  selected='selected'>Please select</option>
            <option value="Instagram" >Instagram</option>
            <option value="Facebook" >Facebook</option>
            <option value="Yelp" >Yelp</option>
            <option value="Other" >Other</option>
        </select>
    </span>
</p>

现在,此选项适用于文本输入字段,但不适用于选择菜单.如何更改它以使其起作用,以便显示结果数据?

Now, this works fine for text input fields but not select menus. How can I change this to make it work so I can display the resulting data?

推荐答案

在某些函数中存在一些错误…在以下函数中,您需要使用与meta键相同的checkout字段键:

There are some mistakes in some of your functions… You need to use the same checkout field key as meta key in the following functions:

在第二个功能中,您使用另一个钩子并将自定义字段另存为用户元数据:

In the 2nd function, you use another hook and save custom field as user meta data too:

add_action( 'woocommerce_checkout_create_order', 'aba_checkout_field_update_order_meta' );

function aba_checkout_field_update_order_meta( $order ) {
    if ( isset($_POST['aba_hear']) && ! empty($_POST['aba_hear']) ) {
        $order->update_meta_data( '_aba_hear', sanitize_text_field( $_POST['aba_hear'] ) );

        // Update user data
        if( $order->get_user_id() > 0 ) {
            update_user_meta( $order->get_user_id(), 'aba_hear', true );
        }
    }
}

在第三个功能中使用此:

In the 3rd function use this:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'aba_checkout_field_display_admin_order_meta', 10, 1 );
function aba_checkout_field_display_admin_order_meta( $order ){
    $value = $order->get_meta( '_aba_hear' );

    if ( ! empty($value) ) {
        echo '<p><strong>'.__('How did You Hear About Us&#63;').':</strong> ' . $value . '</p>';
    }
}

现在应该可以更好地工作了.

It should better work now.

注意:

为什么还要将此结帐字段另存为自定义用户元数据?

因为在您的第一个函数中,您有 $ checkout-> get_value('aba_hear'),它将在此自定义结帐字段中显示客户最后一次下单时的选定值.该值是从用户元'aba_hear'中读取的.

Because in your first function you have $checkout->get_value( 'aba_hear' ) that will display the selected value on from customer last order in this custom checkout field. The value is read from user meta 'aba_hear'.

这篇关于保存结帐自定义字段值并将其显示在WooCommerce管理订单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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