如果woocommerce订单包含特定产品类别的商品,则发送电子邮件

时间:2018-04-13 08:59:44

标签: php wordpress woocommerce custom-taxonomy email-notifications

我在使用下面的代码时遇到了一些问题,我希望在订购完成后发送电子邮件给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 );
          }
        }
    }

1 个答案:

答案 0 :(得分:0)

当订单状态更改为“已完成”时,以下代码会向客户发送特定产品类别的自定义电子邮件:

// 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
        }
    }
}

代码放在活动子主题(或活动主题)的function.php文件中。经过测试和工作