📅  最后修改于: 2023-12-03 14:48:31.771000             🧑  作者: Mango
在使用WordPress的电商插件WooCommerce时,我们可能需要在订单状态更改时做一些特定的操作,比如发送邮件通知客户或更新库存等。在这个场景下,我们可以使用woocommerce_order_status_changed
的钩子函数来添加自定义操作。
woocommerce_order_status_changed
函数需要的参数如下:
$order_id
:订单ID,表示当前更改状态的订单$old_status
:旧订单状态$new_status
:新订单状态$order
:订单对象,包括订单详情和顾客信息等以下是一个在订单状态更改时发送邮件通知管理员和客户的示例代码:
function send_email_on_order_status_change( $order_id, $old_status, $new_status, $order ) {
// Get customer email
$customer_email = $order->get_billing_email();
// Email subject and message
$subject = 'Order status changed';
$message = sprintf( 'Order #%s status has been changed from %s to %s.', $order_id, $old_status, $new_status );
// Admin email notification
wp_mail( get_option( 'admin_email' ), $subject, $message );
// Customer email notification
wp_mail( $customer_email, $subject, $message );
}
add_action( 'woocommerce_order_status_changed', 'send_email_on_order_status_change', 10, 4 );
上述代码中,我们通过$order->get_billing_email()
获取顾客邮箱,使用wp_mail()
函数向顾客和管理员发送邮件通知订单状态更改的情况。