我有一个小问题,我在结帐页面中做了可选字段,当我选择该选项时,在管理订单中打印数字而不是文本。
我的代码如下:
foreach($xml as $data){
$location = $data -> A0_NAME;
if (strpos($location, 'LT') !== false) {
$vieta = $data -> NAME;
$adresas = $data-> A2_NAME;
$zip = $data -> ZIP;
$fulladress = $vieta . ' ' . $adresas . ' ' . $zip;
$option[] = $fulladress;
}
}
woocommerce_form_field( 'my_field_name1', array(
'type' => 'select',
'required' => true,
'class' => array('my-field-class form-row-wide'),
'label' => __('Select an option:', 'my_theme_slug'),
'options' => $option
),
$checkout->get_value( 'my_field_name1' ));
并且有一行正在更新我的订单:
update_post_meta($order_id, 'my_field', sanitize_text_field( $_POST['my_field_name1']) );
问题来自您的$option
数组键...你肯定有这样的东西:
$option = array( 'Text one', 'Text two', 'Text three');
或
$option = array( '1' => 'Text one', '2' => 'Text two', '3' => 'Text three');
因此,当您将数据字段保存到订单时,它会保存选定的数据键...
相反,您需要以这种方式设置它:
$option = array(
'Text one' => 'Text one',
'Text two' => 'Text two',
'Text three' => 'Text three',
);
有关更新代码的更新:
这样它将保存文本而不是密钥数字...所以你的完整代码:
$options = [];
foreach($xml as $data){
$location = $data -> A0_NAME;
if (strpos($location, 'LT') !== false) {
$vieta = $data -> NAME;
$adresas = $data-> A2_NAME;
$zip = $data -> ZIP;
$fulladress = $vieta . ' ' . $adresas . ' ' . $zip;
$options[$fulladress] = $fulladress;
}
}
woocommerce_form_field( 'my_field_name1', array(
'type' => 'select',
'required' => true,
'class' => array('my-field-class form-row-wide'),
'label' => __('Select an option:', 'my_theme_slug'),
'options' => $options,
), $checkout->get_value( 'my_field_name1' ) );
现在您将获得一个文本值...