I'm building a plugin where a customer(s) is attached to a shop manager. The shop manager can only see orders of customers that are attached to him. The plugin has two parts :
The first one, i already built it, hide all orders that aren't attached to a shop manager. Here's the code in case you want to see how it works :
function before_checkout_create_order($order, $data) {
$store_manager_id = '';
switch ($order->get_user_id()) {
case 34:
$store_manager_id = 18;
break;
case 33:
$store_manager_id = 20;
break;
}
$order->update_meta_data('_store_manager_id', $store_manager_id);
}
add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
function custom_admin_shop_manager_orders($query) {
global $pagenow;
$qv = &$query->query_vars;
$currentUserRoles = wp_get_current_user()->roles;
$user_id = get_current_user_id();
if (in_array('shop_manager', $currentUserRoles)) {
if ( $pagenow == 'edit.php' &&
isset($qv['post_type']) && $qv['post_type'] == 'shop_order' ) {
// I use the meta key from step 1 as a second parameter here
$query->set('meta_key', '_store_manager_id');
// The value we want to find is the $user_id defined above
$query->set('meta_value', $user_id);
}
}
return $query;
}
add_filter('pre_get_posts', 'custom_admin_shop_manager_orders');
?>
The second part where i need help : I'm trying to have emails only for the shop manager concerned.
Eg : A customer order some products, the shop manager attached to him receives an email about his order but all others shop managers dont receive anything.
If you have any ideas feel free !
CodePudding user response:
Place the following function in your functions.php
Using woocommerce_thankyou
hook
function notify_store_manager($order_get_id) {
//Get current order
$order = wc_get_order( $order_get_id );
//Get the manager id for this order
$manager_id = $order->get_meta('_store_manager_id');
//Get manager data
$manager = get_userdata($manager_id);
//Get manager email address
$manager_email = $manager->user_email;
//Build your email info
$order_id = $order->get_id();
$to = $manager_email;
$subject = 'You have new order'.$order_id.'';
$body = 'You have new order with id '.$order_id.'';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
}
add_action('woocommerce_thankyou', 'notify_store_manager', 10);