Php 在后端自定义产品设置选择器中自动填充
关于我在上一次会议上成功回答的问题,我现在有选择的字段,并且希望添加自动完成的自定义字段。我尚未对此进行研究 问题: 1.如何自动填充自定义选择字段Php 在后端自定义产品设置选择器中自动填充,php,wordpress,woocommerce,dropdown,product,Php,Wordpress,Woocommerce,Dropdown,Product,关于我在上一次会议上成功回答的问题,我现在有选择的字段,并且希望添加自动完成的自定义字段。我尚未对此进行研究 问题: 1.如何自动填充自定义选择字段 //Adding the custom field select woocommerce_wp_select( array( 'id' => '_select', 'label' => __( 'SIM Type', 'woocommerce' ), 'options' => array( 'one' => __(
//Adding the custom field select
woocommerce_wp_select(
array(
'id' => '_select',
'label' => __( 'SIM Type', 'woocommerce' ),
'options' => array(
'one' => __( 'Regular', 'woocommerce' ),
'two' => __( 'Nano', 'woocommerce' ),
'three' => __( 'Micro', 'woocommerce' )
)
)
);
//Saving
$woocommerce_select = $_POST['_select'];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, '_select', esc_attr( $woocommerce_select ) );
// Display Custom Field Value
echo get_post_meta( $post->ID, '_select', true );
更新:以编程方式在选择字段中设置:
1您需要存储选项键值的关联数组:
// The associative array to store (once)
$options_array = array(
'' => __( 'Select a value', 'woocommerce' ), // default empty value
'one' => __( 'Regular', 'woocommerce' ),
'two' => __( 'Nano', 'woocommerce' ),
'three' => __( 'Micro', 'woocommerce' )
);
// Serialize the array as a string
$option_str = maybe_serialize( $options_array );
// Save this array in Wordpress options
update_option( 'my_custom_selector_options', $option_str );
2获取并取消序列化您的选择器选项:
// Get your options select data
$select_options_str = get_option( 'my_custom_selector_options' );
// Unserialize this data:
$select_options_arr = maybe_unserialize( $select_options_str );
// Get the saved selected 'value' if it exist
$value = get_post_meta( $post->ID, '_select', true );
if( empty( $value ) ) $value = ''; // When 'value' is not defined
//
woocommerce_wp_select(
array(
'id' => '_select',
'label' => __( 'SIM Type', 'woocommerce' ),
'options' => $select_options_arr,
'value' => $value,
)
);
现在,您的字段选择器选项已经由您从WordPress选项中获得的数据填充
要自动填充woocommerce_wp_select,必须以以下方式添加“value”键:
## 1. The select (dropdown)
// Get the 'value' data if it exist
$value = get_post_meta( $post->ID, '_select', true );
if( empty( $value ) ) $value = ''; // When 'value' is not defined
woocommerce_wp_select(
array(
'id' => '_select',
'label' => __( 'SIM Type', 'woocommerce' ),
'options' => array(
'' => __( 'Select a value', 'woocommerce' ), // Added a default empty value
'one' => __( 'Regular', 'woocommerce' ),
'two' => __( 'Nano', 'woocommerce' ),
'three' => __( 'Micro', 'woocommerce' )
),
'value' => $value, // <=== === === === === HERE set the 'value' key (autofill)
)
);
## ---------------------------------
## 2. SAVING
$woocommerce_select = $_POST['_select'];
// The Default empty value is not saved (added in this condition below)
if( !empty( $woocommerce_select ) || $woocommerce_select != '' )
update_post_meta( $post_id, '_select', esc_attr( $woocommerce_select ) );
快速测试:
要查看它的运行情况,请将例如:“value”=>$value替换为“value”=>“two”,
然后选择的值将是:Nano
您好,谢谢@LoicTheAztec,但我想问的是如何像for循环一样,通过编程在选择中添加常规、纳米和微米等值。如何储存和获取?它也是通过meta吗?非常感谢。谢谢,我将为默认值执行此操作;这仍然有帮助;