如果 woocommerce 订单有来自特定产品类别的项目,则发送电子邮件 [英] Send email if woocommerce order has items from a specific product category
本文介绍了如果 woocommerce 订单有来自特定产品类别的项目,则发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对下面的代码有一些问题,我希望在 woo commerce 上完成订单时发送电子邮件,问题是它必须属于某个类别,有人可以帮忙.
I am having some problems with the code below, i am looking to send an email when an order completes on woo commerce, that catch is that it must be of a certain category can someone please help.
add_action( 'woocommerce_catmail', 'my_function' );
function my_function($order_id) {
$order = wc_get_order( $order_id );
foreach($order->get_items() as $orderid) {
if ($orderid['product_cat']== 630) {
$_product = get_product($item['product_id']);
$to_email = $order->billing_email;
$headers = 'From: Your Name <your@email.com>' . "\r\n";
wp_mail($to_email, 'subject', 'message', $headers );
}
}
}
推荐答案
当订单状态更改为已完成"时,以下代码将针对特定产品类别向客户发送自定义电子邮件:
The code below will send a custom email to customer for a specific product category when order status is changed to "completed":
// On order completed status
add_action('woocommerce_order_status_completed', 'send_a_custom_email', 20, 1 );
function send_a_custom_email( $order_id ) {
$order = wc_get_order( $order_id ); // The WC_Order object
foreach ( $order->get_items() as $item_id => $item ) {
$product = $item->get_product(); // The WC_Product object
// Get the parent product ID for product variations for product categories
$the_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
if ( has_term( array( 630 ), 'product_cat', $the_id ) ) {
$to_email = $order->get_billing_email(); // To customer
$subject = "Your subject goes here";
$message = "Your message goes here";
$headers = 'From: Shop Name <shop@email.com>' . "\r\n"; // From admin email
wp_mail( $to_email, $subject, $message, $headers ); // Send email
break; // Stop the loop
}
}
}
代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效
Code goes in functions.php file of your active child theme (or active theme). Tested and works
这篇关于如果 woocommerce 订单有来自特定产品类别的项目,则发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文