我在使用下面的代码时遇到了一些问题,我希望在 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 <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86ffe9f3f4c6e3ebe7efeaa8e5e9eb" rel="noreferrer noopener nofollow">[email protected]</a>>' . "\r\n";
wp_mail($to_email, 'subject', 'message', $headers );
}
}
}
最佳答案
当订单状态更改为“已完成”时,以下代码将向客户发送特定产品类别的自定义电子邮件:
// 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 <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3a0bbbca393b6beb2babffdb0bcbe" rel="noreferrer noopener nofollow">[email protected]</a>>' . "\r\n"; // From admin email
wp_mail( $to_email, $subject, $message, $headers ); // Send email
break; // Stop the loop
}
}
}
代码位于事件子主题(或事件主题)的functions.php 文件中。经过测试并有效
关于php - 如果 woocommerce 订单包含特定产品类别的商品,则发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49813079/