我有以下代码来尝试向 WooCommerce 中的客户发送订单通知电子邮件。
我还有一些其他代码已经使自定义订单状态成为“提醒”。我还可以从 WooCommerce 订单页面的批量操作下拉框中选择这个新的“提醒”。
如果选择一个订单并从下拉框中选择自定义订单状态“提醒”,它会成功在该订单页面上的订单上添加注释,但不会向用户发送通知电子邮件。
怎么会?
// Do the bulk action from the selected orders
add_filter('handle_bulk_actions-edit-shop_order', 'status_bulk_action_edit_shop_order', 10, 3);
function status_bulk_action_edit_shop_order($redirect_to, $action, $post_ids) {
if ($action === 'reminder') {
foreach ($post_ids as $post_id) {
$order = wc_get_order($post_id);
$the_order_id = $order->get_id();
$note = 'Test1';
$order->update_status('reminder');
send_order_note_email($the_order_id, $note);
}
}
return $redirect_to;
}
function send_order_note_email($order_id, $note)
{
// Get the order object
$order = wc_get_order($order_id);
// Check if order exists and is a valid order
if (!$order) {
return;
}
// Add the note to the order
$order->add_order_note($note);
// Send the email notification
$mailer = WC()->mailer()->get_emails()['WC_Email_Note_Customer'];
if ($mailer) {
$mailer->trigger($order_id, $note);
}
}
尝试了各种功能和不同的钩子。
你让事情变得更加复杂:
WC_Order
方法add_order_note()
有一个额外的参数$is_customer_note
,需要将赌注设置为1
或true
,并自动发送customer_note
电子邮件通知.
您可以使用两种方法:
// Do the bulk action from the selected orders
add_filter('handle_bulk_actions-edit-shop_order', 'status_bulk_action_edit_shop_order', 10, 3);
function status_bulk_action_edit_shop_order($redirect_to, $action, $post_ids) {
if ($action === 'reminder') {
foreach ($post_ids as $post_id) {
$order = wc_get_order($post_id);
$order->update_status('reminder');
}
}
return $redirect_to;
}
// Add a note to customer when order get "reminder" status (sends the email notification)
add_action('woocommerce_order_status_reminder', 'add_reminder_order_note_to_customer', 10, 2);
function add_reminder_order_note_to_customer( $order_id, $order )
{
// Define the customer note to be sent
$note_to_customer = 'Test1';
// Add the customer note to the order
$order->add_order_note($note_to_customer, 1);
}
// Do the bulk action from the selected orders
add_filter('handle_bulk_actions-edit-shop_order', 'status_bulk_action_edit_shop_order', 10, 3);
function status_bulk_action_edit_shop_order($redirect_to, $action, $post_ids) {
if ($action === 'reminder') {
foreach ($post_ids as $post_id) {
$order = wc_get_order($post_id);
$order->update_status('reminder');
// Define the customer note to be sent
$note_to_customer = 'Test1';
// Add the customer note to the order
$order->add_order_note($note_to_customer, 1);
}
}
return $redirect_to;
}
代码位于子主题的functions.php 文件中(或插件中)。两种方法都应该有效。