Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.8 kB
7
Indexable
Never

add_filter( 'woocommerce_is_purchasable', 'disable_purchasable_non_agent');
add_filter( 'woocommerce_variation_is_purchasable', 'disable_purchasable_non_agent');

function disable_purchasable_non_agent($purchasable){
	$user_id = get_current_user_id();

	if (!is_agent($user_id) && !check_user_is_agent_with_access()){
		$purchasable = false;
	}
	// if not agent or agent shopping as customers, not purchasable

	return $purchasable;
}
function is_agent($user_id){
	$agent_group = get_user_meta($user_id,'salesking_group',true);
	if ($agent_group !== 'none' && !empty($agent_group)){
		return true;
	} else {
		return false;
	}
}
function check_user_is_agent_with_access(){
	
	//allow if aff cookie or link
	if(isset($_COOKIE['salesking_affiliate_cookie']) || isset($_GET['affid'])){
		return true;
	}

	// check if switch cookie is set
	if (isset($_COOKIE['salesking_switch_cookie'])){
    	$switch_to = sanitize_text_field($_COOKIE['salesking_switch_cookie']);
    	$current_id = get_current_user_id();

    	if (!empty($switch_to) && is_user_logged_in()){
    		// show bar
			$udata = get_userdata( get_current_user_id() );
			$name = $udata->first_name.' '.$udata->last_name;

			// get agent details
			$agent = explode('_',$switch_to);
			$customer_id = intval($agent[0]);
			$agent_id = intval($agent[1]);
			$agent_registration = $agent[2];
			// check real registration in database
			$udataagent = get_userdata( $agent_id );
            $registered_date = $udataagent->user_registered;

            // if current logged in user is the one in the cookie + agent cookie checks out
            if ($current_id === $customer_id && $agent_registration === $registered_date){
            	return apply_filters('b2bking_enable_salesking_visibility', true);
            }
        }
    }
    return false;
}