我在woocommerce的结帐中创建了一个下拉字段,并为每个字段指定了一个命名密钥。我的问题是如何保存命名密钥并将其显示在我的后端。 到目前为止,我无法显示已命名的密钥。
df1$key<-paste(df1$name,df1$postcode,sep="_")
df2$key<-paste(df2$name,df2$postcode,sep="_")
df<-merge(x=df1,y=df2,by=c('key'),all.x=T)
我在这里保存数据。
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' );
在这里,我尝试在订单管理面板中显示该数组。我得到要显示的数组的第一部分,但没有显示与此值关联的命名键。 如何显示已命名的密钥?
//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 );
答案 0 :(得分:0)
更新2 -我已经重新访问了您的代码,进行了一些更改,删除了不必要的代码,并使自定义字段值显示在管理订单编辑页面中:
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
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。
这将显示从订单管理员编辑页面的下拉列表中选择的
<option>
标签。