Im正在定制WooCommerce签出,添加了一个下拉字段,可以获取用户的元键,并在签出页面中显示为选择选项。我还设法在签出后将所选选项保存到订单中。但是,该选项在自定义字段中显示为整数,而不是文本。
以下代码是我的发展,希望有人能指导我。
检索用户元键并显示为下拉选项
add_action(\'woocommerce_after_order_notes\', \'wps_add_select_checkout_field\');
function wps_add_select_checkout_field( $checkout ) {
$user_id = get_current_user_id();
$vessel_one = get_user_meta( $user_id, \'vessel_one\', true );
$vessel_two = get_user_meta( $user_id, \'vessel_two\', true );
$vessel_three = get_user_meta( $user_id, \'vessel_three\', true );
$vessel_four = get_user_meta( $user_id, \'vessel_four\', true );
$vessel_five = get_user_meta( $user_id, \'vessel_five\', true );
woocommerce_form_field( \'order_vessel\', array(
\'type\' => \'select\',
\'class\' => array( \'wps-drop\' ),
\'label\' => __( \'Select a Vessel\' ),
\'options\' => array( $vessel_one, $vessel_two, $vessel_three, $vessel_four, $vessel_five )
),
$checkout->get_value( \'order_vessel\' ));
}
将值保存到订单上的自定义字段中
add_action(\'woocommerce_checkout_update_order_meta\', \'wps_select_checkout_field_update_order_meta\');
function wps_select_checkout_field_update_order_meta( $order_id ) {
if ($_POST[\'order_vessel\']) update_post_meta( $order_id, \'order_vessel\', esc_attr($_POST[\'order_vessel\']));
}
我希望有人能告诉我如何保存值,而不是将其转换为整数。提前非常感谢您。