如何使用 get_post_meta() 获取值而不是 order_id [英] How to get the value instead of order_id with get_post_meta()
问题描述
我有以下代码在 woocommerce 结帐中添加自定义订单字段:
I've had the following code to add a custom order field in the woocommerce checkout:
add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
$fields['billing']['billing_reason_for_purchase'] = array(
'label' => __('Reason for purchase', 'woocommerce'),
'placeholder' => _x('Reason for purchase', 'placeholder', 'woocommerce'),
'required' => false,
'type' => 'select',
'class' => array('form-row-first'),
'options' => array(
'option_1' => __('Personal', 'woocommerce'),
'option_2' => __('Academic', 'woocommerce'),
'option_3' => __('Small Business', 'woocommerce'),
'option_4' => __('Large Organization', 'woocommerce')
)
);
return $fields;
}
然后,接着是下面的代码,用字段值更新订单元
Then, followed by the below code to update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta($order_id) {
if(!empty($_POST['billing_reason_for_purchase'])) {
update_post_meta($order_id,'Reason for purchase',sanitize_text_field($_POST['billing_reason_for_purchase']));
}
}
接下来,在订单编辑页面显示该字段:
Next, display the field on the order edit page:
add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta',10,1);
function my_custom_checkout_field_display_admin_order_meta($order){
//echo '<p><strong>'.__('Reason for purchase').':<strong>'.get_post_meta($order->id, 'Reason for purchase',true).'</p>';
echo '<p><strong>'.__('Reason for purchase').':<strong>'.get_post_meta(get_the_ID(), 'Reason for purchase',true).'</p>';
}
我遇到的问题是,如果我创建了一个选择学术"作为购买原因的虚拟订单,我会在订单编辑页面中得到option_2"而不是学术".
The problem I'm having is if I created a dummy order with choosing "academic" as the reason for purchase, I'd get "option_2" instead of "academic" in the order edit page.
请帮助我指明正确的方向.
Please help point me in the right direction.
推荐答案
发生这种情况是因为所选选项的值(以及 $_POST['billing_reason_for_purchase']
的值)实际上是数组(在您的示例 option_2
中)而不是与文本相关的.实际上这是创建的选项标签:
It happens because the value of the selected option (and so of $_POST['billing_reason_for_purchase']
) is actually the key of the array (in your example option_2
) and not the text related. In fact this is the option tag created:
<option value="option_2">Academic</option>
您只保存了数组的键.
因此,即使在 my_custom_checkout_field_display_admin_order_meta
函数上,您也需要检索选项数组以获取正确的文本.
So you need to retrieve the array of options even on the my_custom_checkout_field_display_admin_order_meta
function to get the proper text.
您可以在每个函数中复制数组(但不方便复制代码),或者将其放在全局变量中以便能够从任何地方访问它(但其他东西可能会更改它),因此您可以使用返回数组的函数:
You could copy the array in each function (but is not convenient to duplicate code), or put it in a global variable to be able to access it from anywhere (but then something else could change it), so instead you can use a function that returns the array:
function reasons_for_purchase () {
return array(
'option_1' => __('Personal', 'woocommerce'),
'option_2' => __('Academic', 'woocommerce'),
'option_3' => __('Small Business', 'woocommerce'),
'option_4' => __('Large Organization', 'woocommerce')
);
}
然后在需要的地方使用它:
And then use it where you need it:
add_filter('woocommerce_checkout_fields','custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
$fields['billing']['billing_reason_for_purchase'] = array(
'label' => __('Reason for purchase', 'woocommerce'),
'placeholder' => _x('Reason for purchase', 'placeholder', 'woocommerce'),
'required' => false,
'type' => 'select',
'class' => array('form-row-first'),
'options' => reasons_for_purchase()
);
return $fields;
}
add_action('woocommerce_admin_order_data_after_billing_address','my_custom_checkout_field_display_admin_order_meta',10,1);
function my_custom_checkout_field_display_admin_order_meta($order){
$reasons = reasons_for_purchase();
$reason = get_post_meta($order->id, 'Reason for purchase', true);
if( isset($reasons[$reason]) )
echo '<p><strong>'.__('Reason for purchase').':</strong> '. $reasons[$reason] .'</p>';
}
这篇关于如何使用 get_post_meta() 获取值而不是 order_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!