如果在 Woocommerce 中购买了特定产品,则向特定地址发送电子邮件通知
Email notification to a particular address if a specific product is purchased in Woocommerce
我在我的 Wordpress 网站中使用 woocommerce 插件。我想知道如果客户购买了 产品 A,我该如何向特定地址的电子邮件发送电子邮件通知。
如何在 Woocommerce 中购买特定产品时向特定地址发送电子邮件通知?
add_action('woocommerce_thankyou', 'send_product_purchase_notification');
function send_product_purchase_notification($orderId)
{
$order = new WC_Order($orderId);
foreach ($order->get_items() as $item) {
$product_id = (is_object($item)) ? $item->get_product_id() : $item['product_id'];
if ($product_id == 1234) {
$custom_massage = 'Custom email content';
wp_mail('name@domain.com', 'Product #' . $product_id . ' has been purchased', $custom_massage);
return true;
}
}
}
当在订单商品中找到特定定义的产品 ID 时,下面的代码会将自定义的电子邮件收件人添加到新电子邮件通知中:
add_filter( 'woocommerce_email_recipient_new_order', 'conditional_recipient_new_email_notification', 15, 2 );
function conditional_recipient_new_email_notification( $recipient, $order ) {
if( is_admin() ) return $recipient; // (Mandatory to avoid backend errors)
## --- YOUR SETTINGS (below) --- ##
$targeted_id = 37; // HERE define your targeted product ID
$addr_email = 'name@domain.com'; // Here the additional recipient
// Loop through orders items
foreach ($order->get_items() as $item_id => $item ) {
if( $item->get_variation_id() == $targeted_id || $item->get_product_id() == $targeted_id ){
$recipient .= ', ' . $addr_email;
break; // Found and added - We stop the loop
}
}
return $recipient;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
您应该可以使用官方 WooCommerce Advanced Notifications extension 生成高级通知以向特定人员发送电子邮件。
我在我的 Wordpress 网站中使用 woocommerce 插件。我想知道如果客户购买了 产品 A,我该如何向特定地址的电子邮件发送电子邮件通知。
如何在 Woocommerce 中购买特定产品时向特定地址发送电子邮件通知?
add_action('woocommerce_thankyou', 'send_product_purchase_notification');
function send_product_purchase_notification($orderId)
{
$order = new WC_Order($orderId);
foreach ($order->get_items() as $item) {
$product_id = (is_object($item)) ? $item->get_product_id() : $item['product_id'];
if ($product_id == 1234) {
$custom_massage = 'Custom email content';
wp_mail('name@domain.com', 'Product #' . $product_id . ' has been purchased', $custom_massage);
return true;
}
}
}
当在订单商品中找到特定定义的产品 ID 时,下面的代码会将自定义的电子邮件收件人添加到新电子邮件通知中:
add_filter( 'woocommerce_email_recipient_new_order', 'conditional_recipient_new_email_notification', 15, 2 );
function conditional_recipient_new_email_notification( $recipient, $order ) {
if( is_admin() ) return $recipient; // (Mandatory to avoid backend errors)
## --- YOUR SETTINGS (below) --- ##
$targeted_id = 37; // HERE define your targeted product ID
$addr_email = 'name@domain.com'; // Here the additional recipient
// Loop through orders items
foreach ($order->get_items() as $item_id => $item ) {
if( $item->get_variation_id() == $targeted_id || $item->get_product_id() == $targeted_id ){
$recipient .= ', ' . $addr_email;
break; // Found and added - We stop the loop
}
}
return $recipient;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
您应该可以使用官方 WooCommerce Advanced Notifications extension 生成高级通知以向特定人员发送电子邮件。