Skip to content

Instantly share code, notes, and snippets.

@codelion7
Last active May 19, 2020 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codelion7/b69f6c6e8155dbc31bf659ad6fb4faf2 to your computer and use it in GitHub Desktop.
Save codelion7/b69f6c6e8155dbc31bf659ad6fb4faf2 to your computer and use it in GitHub Desktop.
Auto-Assign KB Support Ticket to EDD FES Vendor, based on the Download/Product Submitted
<?php
if ( class_exists( 'KB_Support' ) ) {
/**
* Allow Vendors to Manage Tickets (Make them Support Agents)
*
* @since 1.0
* @return array
*/
function kbs_edd_fes_allow_vendor_ticket_management( $roles ) {
if ( ! function_exists( 'EDD_FES' ) ) return $roles;
// Add FES Vendor Roles
$roles[] = 'shop_vendor';
$roles[] = 'shop_manager';
$roles[] = 'shop_worker';
return $roles;
}
add_filter( 'kbs_agent_user_roles', 'kbs_edd_fes_allow_vendor_ticket_management' );
/**
* Auto-Assign the Ticket to the Download/Product Vendor
*
* @since 1.0
* @return void
*/
function kbs_edd_fes_assign_vendor_support_agent ( $ticket_data = array() ) {
if ( ! function_exists( 'EDD_FES' ) ) return $ticket_data;
if ( ! array_key_exists( 'edd_downloads', $ticket_data ) || empty( $ticket_data['edd_downloads'] ) ) return $ticket_data;
$downloads = $ticket_data['edd_downloads'];
$vendor_ids = array();
if ( is_array( $downloads ) ) {
foreach ( $downloads as $download_id ) {
$post = get_post( $download_id );
$vendor_id = $post->post_author; // Vendor's WP User ID
// Skip if they are not a vendor
if ( ! EDD_FES()->vendors->user_is_vendor( $vendor_id ) ) continue;
// Add vendor to list, if not already added
if ( ! in_array( $vendor_id, $vendor_ids ) ) $vendor_ids[] = $vendor_id;
}
}
$single_vendor = ( count( $vendor_ids ) > 1 ) ? false : true;
$agent_id = $vendor_ids[0];
// Assign to multiple vendors/agents
if ( ! $single_vendor ) {
$ticket_data['agents'] = $vendor_ids;
}
$ticket_data['agent_id'] = $agent_id; // Assign ticket to Vendor
return $ticket_data;
}
add_filter( 'kbs_add_ticket_data', 'kbs_edd_fes_assign_vendor_support_agent', 99, 1 );
}
?>
@mikeyhoward1977
Copy link

if ( ! $single_vendor ) {
	$ticket_data['agents'] = $vendor_ids;
}

The KBS_Ticket class will take care of everything else as part of the ticket creation process including removing the $agent_id from the $vendor_ids array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment