Untitled

 avatar
unknown
plain_text
a year ago
2.1 kB
16
Indexable
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'send_email_to_agent', 10, 2 );
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'send_email_to_agent', 10, 2 );
add_filter( 'woocommerce_email_recipient_customer_refunded_order', 'send_email_to_agent', 10, 2 );
add_filter( 'woocommerce_email_recipient_customer_on_hold_order', 'send_email_to_agent', 10, 2 );
add_filter( 'woocommerce_email_recipient_cancelled_order', 'send_email_to_agent', 10, 2 );
add_filter( 'woocommerce_email_recipient_failed_order', 'send_email_to_agent', 10, 2 );
add_filter( 'woocommerce_email_recipient_new_order', 'send_email_to_agent', 10, 2 );
function send_email_to_agent( $recipient, $order ) {

	// Bail on WC settings pages since the order object isn't yet set yet
	$page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
	if ( 'wc-settings' === $page ) {
		return $recipient; 
	}
	
	// just in case
	if ( ! $order instanceof WC_Order ) {
		return $recipient; 
	}

	// if the customer of this order, or his group has an assigned agent
	$customer_id = $order->get_customer_id();

	// first, check if the customer has an agent assigned directly.
	$customer_agent = get_user_meta($customer_id,'salesking_assigned_agent', true);
	if (!empty($customer_agent) && $customer_agent !== 'none'){
		// found agent
	} else {
		// keep searching in the group
		$customer_is_b2b = get_user_meta($customer_id,'b2bking_b2buser', true);
		if ($customer_is_b2b === 'yes'){
			$customergroup = get_user_meta($customer_id, 'b2bking_customergroup', true);
			$customer_agent = get_post_meta($customergroup, 'salesking_assigned_agent', true);
		}
	}

	if (empty($customer_agent) or $customer_agent === 'none'){
		$order_id = $order->get_id();
		$customer_agent = $order->get_meta('salesking_assigned_agent');
	}

	if (!empty($customer_agent) && $customer_agent !== 'none'){
		// send email to agent as well
		$agent_info = get_userdata($customer_agent);
		$agent_email = $agent_info->user_email;
		$recipient .= ', '.$agent_email;
	}


	return $recipient;
}
Editor is loading...
Leave a Comment