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>