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.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?