Untitled

 avatar
unknown
plain_text
a year ago
3.6 kB
4
Indexable

add_filter('salesking_agent_assigned_order', function($order_assigned_agent, $order){

	$order_assigned_agent = 0;

	// shop as customer
    if ($order_assigned_agent === 0){
    	if (check_user_is_agent_with_access()){
			$agent_id = get_current_agent_id();
			$order_assigned_agent = $agent_id;
		}
    }

    //coupons
    if ($order_assigned_agent === 0){
		foreach( $order->get_coupon_codes() as $coupon_code) {

			$coupon_id = get_coupon_id_by_code( $coupon_code );

	        // check agent
	        $coupon_agent = get_post_meta($coupon_id,'salesking_agent', true);

	        if (!empty($coupon_agent)){

	        	if (is_agent($coupon_agent)){
	        		$order_assigned_agent = $coupon_agent;
	        	}
	        }
	    }
	}

	// cart sharing
    if ($order_assigned_agent === 0){
    	if (isset($_COOKIE['salesking_affiliate_cookie'])){
    		$affiliate_cookie = sanitize_text_field($_COOKIE['salesking_affiliate_cookie']);
    		// search agentid
			$agent = get_users(array(
			    'meta_key'     => 'salesking_agentid',
			    'meta_value'   => $affiliate_cookie,
			    'meta_compare' => '=',
			    'fields' => 'ids',
			));
			if (count($agent) === 1){
				$order_assigned_agent = $agent[0];

			}
    	}
    }

    // agent assigned
    if ($order_assigned_agent === 0){
    	$customer_id = $order->get_customer_id();
    	$assignedagent = get_user_meta($customer_id, 'salesking_assigned_agent', true);
    	if ($assignedagent !== 'none' && !empty($assignedagent)){
    		$order_assigned_agent = $assignedagent;
    	}
    }

	return $order_assigned_agent;
}, 10, 2);

function get_coupon_id_by_code( $coupon_code ) {
    global $wpdb;

    // Query the database to find the coupon post by its title (coupon code)
    $coupon_id = $wpdb->get_var( $wpdb->prepare( "
        SELECT ID
        FROM $wpdb->posts
        WHERE post_title = %s
        AND post_type = 'shop_coupon'
    ", $coupon_code ) );

    return $coupon_id;
}


function check_user_is_agent_with_access(){
	// 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 true;
            }
        }
    }
    return false;
}
function get_current_agent_id(){
	if (isset($_COOKIE['salesking_switch_cookie'])){
    	$switch_to = sanitize_text_field($_COOKIE['salesking_switch_cookie']);
    	if (!empty($switch_to)){
    		$agent = explode('_',$switch_to);
    		$agent_id = intval($agent[1]);
    		return $agent_id;
    	}
    } else {
    	if (intval(get_option( 'salesking_agents_own_orders_commission_setting', 0 )) === 1){
    		return get_current_user_id();
    	}
    }
    return false;
}
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;
	}
}
Editor is loading...
Leave a Comment