自定义 Woocommerce 结帐 select 字段保存的值是数字而不是文本
Custom Woocommerce checkout select field saved value is a number instead of text
我有一个小问题,我在结帐页面中做 select 可用字段,当我 select 选项时,在管理订单中打印数字而不是文本。
我的代码如下:
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' ) );
现在你会得到一个文本值…
我有一个小问题,我在结帐页面中做 select 可用字段,当我 select 选项时,在管理订单中打印数字而不是文本。
我的代码如下:
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' ) );
现在你会得到一个文本值…