Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.9 kB
0
Indexable
Never
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;
}
Leave a Comment