Autoswitch sales agent to customer account on new registration
Sunday, 02 January 2022
WooCommerce B2B Sales Agents allows sales agents to create new customers from their dashboard, if the “NEW CUSTOMERS” option is enabled on the plugin settings page.
After this, sales agent can switch on customer account in customer details page (by clicking “Details” button in customers list).
If you desire to automatically switch on new customer account after registration, you can edit functions.php file that you find in your site’s FTP space in:
wp-content/themes/{YOUR_ACTIVE_THEME}/functions.php
and insert this code:
add_action( 'woocommerce_created_customer', function( $customer_id, $new_customer_data, $password_generated ) { if ( is_user_logged_in() && wcb2bsa_has_role( get_current_user_id(), 'sales_agent' ) ) { $user_id = get_current_user_id(); set_transient( 'wcb2bsa_switched_' . $customer_id, $user_id, 86400 ); setcookie( 'wcb2bsa_switched', 'true', 0, '/', COOKIE_DOMAIN, is_ssl(), true ); wp_set_current_user( $customer_id ); wp_set_auth_cookie( $customer_id ); wp_redirect( wc_get_page_permalink( 'shop' ) ); exit; } }, 99, 3 );
It is advisable not to edit the theme files directly, but to use a child theme.
For more information on child themes, please read: WordPress Official guide on Child Themes