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

在后端显示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>

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.