Untitled
webwizards
plain_text
14 days ago
3.6 kB
9
Indexable
/**
* 1) When a subaccount places an order pending approval, notify all Manager subaccounts.
* Fires on the order transitioning to "pcompany" status, which is what the
* B2BKing Company Approval gateway sets the order to.
*/
add_action('woocommerce_order_status_pcompany', function($order_id, $order){
if (!$order) $order = wc_get_order($order_id);
if (!$order) return;
$customer_id = $order->get_customer_id();
if (get_user_meta($customer_id, 'b2bking_account_type', true) !== 'subaccount') return;
// Guard against duplicate sends if the status transition re-fires
if ($order->get_meta('_my_managers_notified') === 'yes') return;
$parent_id = intval(get_user_meta($customer_id, 'b2bking_account_parent', true));
if ($parent_id === 0) return;
$managers = get_users(array(
'meta_query' => array(
'relation' => 'AND',
array('key' => 'b2bking_account_parent', 'value' => $parent_id),
array('key' => 'b2bking_account_type', 'value' => 'subaccount'),
array('key' => 'is_manager', 'value' => 'yes'),
),
'fields' => array('ID', 'user_email'),
));
if (empty($managers)) return;
$subaccount = get_user_by('id', $customer_id);
$subaccount_name = trim($subaccount->first_name . ' ' . $subaccount->last_name);
if ($subaccount_name === '') $subaccount_name = $subaccount->user_login;
$message = sprintf(
__('A new order #%1$s placed by %2$s requires your review. Click <a href="%3$s">here</a> to view orders.', 'b2bking'),
$order_id,
esc_html($subaccount_name),
esc_url(wc_get_account_endpoint_url('orders'))
);
foreach ($managers as $manager) {
do_action('b2bking_new_message', $manager->user_email, $message, 'Quoteemail:1', 0);
}
$order->update_meta_data('_my_managers_notified', 'yes');
$order->save();
}, 10, 2);
/**
* 2) When a Manager approves an order, notify the parent account so they can pay.
* The plugin already fires `b2bking_after_approve_order` after the order status
* transitions to pending payment. We only send if the approver is a Manager
* subaccount (so a parent self-approving doesn't email themselves).
*/
add_action('b2bking_after_approve_order', function($order){
if (!$order) return;
$current_user_id = get_current_user_id();
if (get_user_meta($current_user_id, 'b2bking_account_type', true) !== 'subaccount') return;
if (get_user_meta($current_user_id, 'is_manager', true) !== 'yes') return;
$parent_id = intval(get_user_meta($current_user_id, 'b2bking_account_parent', true));
if ($parent_id === 0) return;
$parent_user = get_user_by('id', $parent_id);
if (!$parent_user) return;
$manager = wp_get_current_user();
$manager_name = trim($manager->first_name . ' ' . $manager->last_name);
if ($manager_name === '') $manager_name = $manager->user_login;
$order_id = $order->get_id();
if (floatval($order->get_total()) > 0) {
$message = sprintf(
__('Order #%1$s has been approved by %2$s and is ready for payment. Click <a href="%3$s">here</a> to pay.', 'b2bking'),
$order_id,
esc_html($manager_name),
esc_url($order->get_checkout_payment_url())
);
} else {
$message = sprintf(
__('Order #%1$s has been approved by %2$s.', 'b2bking'),
$order_id,
esc_html($manager_name)
);
}
do_action('b2bking_new_message', $parent_user->user_email, $message, 'Quoteemail:1', 0);
}, 10, 1);Editor is loading...
Leave a Comment