【问题标题】:Send email if woocommerce order has items from a specific product category如果 woocommerce 订单包含来自特定产品类别的商品,则发送电子邮件
【发布时间】:2018-09-23 13:58:31
【问题描述】:

我在使用下面的代码时遇到了一些问题,我希望在 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 );
          }
        }
    }

【问题讨论】:

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


    【解决方案1】:

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

    // 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 文件中。经过测试和工作

    【讨论】:

    • 感谢您的回复,我可以通过电子邮件发送到特定的电子邮件地址 $to_email = "email@email.com";
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 2019-08-29
    • 2021-04-30
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多