drtiwd06558 2018-06-21 10:43 采纳率: 100%
浏览 95
已采纳

在后端显示Woocommerce中下拉结帐字段中的选项标签

I created a dropdown field in the checkout of woocommerce and gave every one a named key. my question is how to save the named key and show them in my backend. Till now i can't show the named key.

function cloudways_custom_checkout_fields($fields){
$fields['cloudways_extra_fields'] = array(

        'cloudways_dropdown' => array(
            'type' => 'select',
            'options' => array( 'freudige_anlaesse' => __( 'Freudige Anlässe' ), 'traurige_anlaesse' => __( 'Traurige Anlässe' ), 'testamentsspende' => __( 'Testamentsspende' ), 'stiftungen' => __( 'Stiftungen' ), 'unternehmen' => __( 'Unternehmen' ) ),
            'required'      => true,
            'label' => __( 'Grund Ihrer Spende' )
            )
        );

return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'cloudways_custom_checkout_fields' );

function cloudways_extra_checkout_fields(){

$checkout = WC()->checkout(); ?>

<div class="col-2">
<h3><?php _e( 'Verwendungszweck' ); ?></h3>

<?php
   foreach ( $checkout->checkout_fields['cloudways_extra_fields'] as $key => $field ) : ?>

        <?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>

    <?php endforeach; ?>
</div>

<?php }
    add_action('woocommerce_checkout_after_customer_details','cloudways_extra_checkout_fields'  );

Here I am saving the data.

//SAVE THE DATA
function cloudways_save_extra_checkout_fields( $order_id, $posted ){
// don't forget appropriate sanitization if you are using a different field type
if( isset( $posted['cloudways_text_field'] ) ) {
    update_post_meta( $order_id, '_cloudways_text_field', sanitize_text_field( $posted['cloudways_text_field'] ) );
}

if( isset( $posted['cloudways_dropdown'] ) && in_array( $posted['cloudways_dropdown'], array( 'freudige_anlaesse' , 'traurige_anlaesse', 'testamentsspende', 'stiftungen', 'unternehmen' ) ) ) {

    update_post_meta( $order_id, '_cloudways_dropdown', $posted['cloudways_dropdown'] );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'cloudways_save_extra_checkout_fields', 10, 2 );

And here I try to show the array in the order admin panel. I get the fisrt part of the array to show but not the named key I associated with this value. How can I show the named key?

function kia_display_order_data_in_admin( $order ){  ?>
<div class="order_data_column">
    <h4><?php _e( 'Verwendungszweck' ); ?></h4>
    <?php 
         echo '<p><strong>' . __( 'Grund der Spende: ' ) . '</strong>'. get_post_meta( $order->id, '_cloudways_dropdown', true ) . '</p>';?>

</div>
  • 写回答

1条回答 默认 最新

  • dongliang1223 2018-06-21 12:09
    关注

    Update 2 - I have revisited your code, making some changes, removing unnecessary code and make the custom field value displayed in admin Order edit pages:

    function spende_options_array(){
        return array( 'freudige_anlaesse' => __( 'Freudige Anlässe' ), 'traurige_anlaesse' => __( 'Traurige Anlässe' ), 'testamentsspende' => __( 'Testamentsspende' ), 'stiftungen' => __( 'Stiftungen' ), 'unternehmen' => __( 'Unternehmen' ) );
    }
    
    add_filter( 'woocommerce_checkout_fields', 'add_custom_checkout_field' );
    function add_custom_checkout_field($fields){
        $fields['extra_fields'] = array( 'spende_dropdown' => array(
            'type'     => 'select',
            'label'    => __( 'Grund Ihrer Spende' ),
            'options'  => spende_options_array(),
            'required' => true,
        ) );
    
        return $fields;
    }
    
    add_action('woocommerce_checkout_after_customer_details','add_extra_checkout_field'  );
    function add_extra_checkout_field(){
        $checkout = WC()->checkout();
        ?>
        <div class="col-2">
        <h3><?php _e( 'Verwendungszweck' ); ?></h3>
        <?php
    
        foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) :
            woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
        endforeach;
        ?>
        </div>
        <?php
    }
    
    add_action( 'woocommerce_checkout_create_order', 'save_extra_checkout_fields', 10, 2 );
    function save_extra_checkout_fields( $order, $posted ){
        if( isset( $posted['spende_dropdown'] ) && in_array( $posted['spende_dropdown'], array( 'freudige_anlaesse' , 'traurige_anlaesse', 'testamentsspende', 'stiftungen', 'unternehmen' ) ) ) {
            $order->update_meta_data( '_spende_dropdown', $posted['spende_dropdown'] );
        }
    }
    
    add_action( 'woocommerce_admin_order_data_after_order_details', 'display_order_custom_meta_data_in_admin' );
    function display_order_custom_meta_data_in_admin( $order ){
        $spende_options = spende_options_array();
        $key = get_post_meta( $order->get_id(), '_spende_dropdown', true );
        ?>
        <div class="order_data_column">
            <h4><?php _e( 'Verwendungszweck' ); ?></h4>
            <p><strong><?php _e( 'Grund der Spende: ' ); ?></strong><?php echo $spende_options[$key]; ?></p>
        </div>
        <?php
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    This will display the selected <option> label from the dropdown in Order admin edit pages.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?