在后端自动填充 WooCommerce 自定义产品设置选择器
Auto-populate in backend a WooCommerce Custom Product setting selector
关于我之前在 上成功回答的问题,我现在有 select 个字段并且还想添加自动完成的自定义字段(我还没有研究这个)
问题:
1.) 如何自动填充自定义 select 字段?
//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 );
Update: Set programmatically the <option>
in an select field:
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 ) );
A quick test:
To see it in action, replace for example: 'value' => $value,
by 'value' => 'two',
Then selected value will be: Nano …
关于我之前在
问题: 1.) 如何自动填充自定义 select 字段?
//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 );
Update: Set programmatically the
<option>
in an select field:
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 ) );
A quick test:
To see it in action, replace for example:'value' => $value,
by'value' => 'two',
Then selected value will be: Nano …