如何使用get_post_meta()获取值而不是order_id

时间:2015-05-13 14:40:46

标签: wordpress woocommerce

我有以下代码在woocommerce结帐中添加自定义订单字段:

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

然后,接下来是以下代码,用字段值

更新订单元
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']));
    }
}

接下来,在订单编辑页面上显示该字段:

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>';
}

我遇到的问题是,如果我选择&#34;学术&#34;作为购买的理由,我得到&#34; option_2&#34;而不是&#34;学术&#34;在订单编辑页面中。

请指点我正确的方向。

1 个答案:

答案 0 :(得分:1)

之所以发生这种情况,是因为所选选项(以及$_POST['billing_reason_for_purchase'])的值实际上是数组的键(在您的示例中为option_2),而不是与文本相关的值。实际上这是创建的选项标签:

<option value="option_2">Academic</option>

您只保存数组的密钥。

因此,即使在my_custom_checkout_field_display_admin_order_meta函数上也需要检索选项数组,以获取正确的文本。

你可以在每个函数中复制数组(但是不方便复制代码),或者将它放在一个全局变量中以便能够从任何地方访问它(但是其他东西可以改变它),所以你可以使用返回数组的函数:

function reasons_for_purchase () {
    return array(
       'option_1' => __('Personal', 'woocommerce'),
       'option_2' => __('Academic', 'woocommerce'),
       'option_3' => __('Small Business', 'woocommerce'),
       'option_4' => __('Large Organization', 'woocommerce')
   );
}

然后在需要的地方使用它:

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>';
}