Okay
  Public Ticket #4103356
New customer approval
Open

Comments

  • Andreas started the conversation

    Hi,

    We have the B2BKing and Sales King installed for our shop. 

    The process is that new B2B clients require a manual approval. During their registration process, they can select a Sales Agent via the Agent ID. 

    Would it be possible that the selected agent receives the New customer requires approval email and can approve the customer. And the default email address gets the email when no agent is selected?

    Thanks//Andreas

  •  2,472
    WebWizards replied

    Hi Andreas,

    Glad to assist,

    I believe I can help with a custom code snippet to make the selected agent get the "New customer requires approval" email as you suggested.

     

    However, regarding the approval functionality, there's a bit of a challenge since by default, agents don't have access to the backend user profile page where customer approval takes place.

    Would it be a workable solution for you if we:
    1. Set up the email notifications to go to the selected agent
    2. Give agents permission to edit users assigned to them in the WordPress admin panel? This would allow them to handle the approval process directly, but also some restricted admin backend access.

     

     

    Kind regards,
    Stefan

  • Andreas replied

    Hi Stefan,Thanks for your answer.
    This could be an option. What would be the role the agent would get to reach the backend?

    //Andreas

  •  2,472
    WebWizards replied

    Hi Andreas,

    The agents wouldn't necessarily need a specific role - they could use the default 'Customer' role, and we would add custom permissions via code to allow them to edit users assigned to them.

    To implement this properly and ensure everything works smoothly with your specific setup, would you be able to share backend access to your site or a staging site? Different sites often have varying permission configurations, and I want to make sure the solution I provide works for your specific case.

    Kind regards,
    Stefan

  •   Andreas replied privately
  •  2,472
    WebWizards replied

    Yes, that would be perfect!

  •   Andreas replied privately
  •  2,472
    WebWizards replied

    Hi Andreas,

    I looked into this now on the site and I believe I have completed it successfully.

     

    This is the code snippet that handles it:

    add_filter( 'woocommerce_email_recipient_b2bking_new_customer_requires_approval_email', 'send_email_to_agent', 10, 1 );
    
    function send_email_to_agent( $recipient ) {
    
    	// Bail on WC settings pages since the order object isn't yet set yet
    	$page = isset( $_GET['page'] ) ? $_GET['page'] : '';
    	if ( 'wc-settings' === $page ) {
    		return $recipient; 
    	}
    
    	if (isset($_POST['salesking_registration_link'])){
    		$registration_value = sanitize_text_field($_POST['salesking_registration_link']); // agent id
    
    		if (!empty($registration_value)){
    			// get agent user ID
    			$agent = get_users(array(
    			    'meta_key'     => 'salesking_agentid',
    			    'meta_value'   => $registration_value,
    			    'meta_compare' => '=',
    			    'fields' => 'ids',
    			));
    			if (count($agent) === 1 || $registration_value === 'none'){
    				$agent_id = $agent[0];
    				$agent_user = new WP_User($agent_id);
    				$agent_email = $agent_user->user_email;
    				$recipient = $agent_email;
    			}
    		}
    	}
    
    	return $recipient;
    }
    
    add_filter( 'user_has_cap', 'agents_caps_manage_users', 1000, 3 );
    function agents_caps_manage_users( $allcaps, $caps, $args ){
    
    	if (isset($_GET['user_id'])){
    		$userid = sanitize_text_field($_GET['user_id']);
    		$user_agent = get_user_meta($userid, 'salesking_assigned_agent', true);
    		if (intval($user_agent) === get_current_user_id()){
    			$caps = array('edit_users', 'edit_user', 'manage_woocommerce');
    
    			foreach ($caps as $cap){
    				$allcaps[$cap] = 1;
    			}
    		}
    	}
    
    	return $allcaps;
    }
    
    add_action('init', function(){
    
    	if (isset($_GET['user_id'])){
    		$userid = sanitize_text_field($_GET['user_id']);
    		$user_agent = get_user_meta($userid, 'salesking_assigned_agent', true);
    		if (intval($user_agent) === get_current_user_id()){
    			global $b2bking_admin;
    			add_action( 'show_user_profile', array($b2bking_admin, 'b2bking_show_user_meta_profile'), 1000, 1 );
    			add_action( 'edit_user_profile', array($b2bking_admin, 'b2bking_show_user_meta_profile'), 1000, 1 );
    		}
    	}
    	
    });
    
    add_filter('b2bking_backend_capability_needed', function($capability){
    	if (isset($_POST['chosen_group'])){
    		return 'default_wholesaler';
    	}
    	return $capability;
    	
    }, 10, 1);

    On your live site, it has to be added to the code snippets plugin.

     

    On the staging site, I added it here:

    https://staging3.sigtunastearinljusfabrik.se/wp-admin/admin.php?page=edit-snippet&id=10

     

    This takes care of both sending the email to the agent, and then allowing the agent to edit the user in the backend so they can approve the user.

    In the snippet, we need to specify the role of agents (currently "default_wholesaler"). Please note that if agents are given a different role than default_wholesaler you will need to update the snippet,

     

    I tested that code on the staging site and I believe it is working. Let me know if you run into any issues,

     

    Kind regards,

    Stefan