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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line