【发布时间】:2020-10-22 12:51:21
【问题描述】:
我正在使用自定义复选框开发 woocommerce 注册解决方案。
计划是当有人选择自定义复选框时,会打开一个额外的文本字段并且必须是必需的。
起作用的部分:
// add the special customer role
add_action('admin_init', 'uiwc_new_role');
function uiwc_new_role() {
add_role(
'kundenkarte',
"Kundenkarte",
array(
'read' => true,
'delete_posts' => false
)
);
}
add_action( 'woocommerce_after_order_notes', 'custom_checkout_field_with_wholesale_option' );
function custom_checkout_field_with_wholesale_option( $checkout ) {
if( current_user_can( 'wholesale_customer' ) ) return; // exit if it is "wholesale customer"
echo '<div id="wholesale_checkbox_wrap">';
woocommerce_form_field('wholesale_checkbox', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Do you have a Customer Card?'),
'placeholder' => __('card'),
'required' => false,
'value' => true
), '');
echo '</div>';
}
// Conditionally change customer user role
add_action( 'woocommerce_checkout_update_order_meta', 'wholesale_option_update_user_meta' );
function wholesale_option_update_user_meta( $order_id ) {
if ( isset($_POST['wholesale_checkbox']) ) {
$user_id = get_post_meta( $order_id, '_customer_user', true ); // Get user ID
if( $user_id > 0 ){
$user = new WP_User($user_id);
$user->remove_role('customer');
$user->add_role('kundenkarte');
}
}
}
我的PHP知识很低。
【问题讨论】:
-
您的问题附录已被删除,因为它已发布在答案空间中。您可以在他们的回答下回复 LoicTheAztec;如果您想显示更多代码,可以使用 GitHub Gist 或 Pastie 链接。
标签: php jquery wordpress woocommerce checkout