如果 woocommerce 订单包含来自特定产品类别的项目,请发送电子邮件
Send email if woocommerce order has items from a specific product category
我在使用下面的代码时遇到了一些问题,我希望在 woo commerce 上完成订单时发送一封电子邮件,问题是它必须属于某个类别,有人可以帮忙吗。
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 );
}
}
}
当订单状态更改为 "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 文件。已测试并有效
我在使用下面的代码时遇到了一些问题,我希望在 woo commerce 上完成订单时发送一封电子邮件,问题是它必须属于某个类别,有人可以帮忙吗。
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 );
}
}
}
当订单状态更改为 "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 文件。已测试并有效