Hello there,
I understand that you want to add a new checkout field, and based on the option selected, you want to register the customer with a different role.
You can add the checkout field using Checkout Field Editor: https://woocommerce.com/products/woocommerce-checkout-field-editor
Registering them with the customer role though would require custom coding. This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.
I can also recommend the following places for more development-oriented questions:
- WooCommerce Slack Community: https://woocommerce.com/community-slack/
- WooCommerce FB group: https://www.facebook.com/groups/advanced.woocommerce/
i have found a big part of the solution, the only part that is missing is the text field.
if select, show a text field, required to fill in
add_action('admin_init', 'uiwc_new_role');
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');
}
}
}
Please help