WooCommerce send new order email to customer

梦想与她 提交于 2019-11-29 12:35:27

You do not need to do all these stuff again for sending multiple emails. You can simply add recipient to "New Order" email(with customer's email). Here is the code you can try:

<?php
/**
 * Add another email recipient for admin New Order emails if a shippable product is ordered
 *
 * @param string $recipient a comma-separated string of email recipients (will turn into an array after this filter!)
 * @param \WC_Order $order the order object for which the email is sent
 * @return string $recipient the updated list of email recipients
 */
function sv_conditional_email_recipient( $recipient, $order ) {
    // Bail on WC settings pages since the order object isn't yet set yet
    // Not sure why this is even a thing, but shikata ga nai
    $page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
    if ( 'wc-settings' === $page ) {
        return $recipient; 
    }

    // just in case
    if ( ! $order instanceof WC_Order ) {
        return $recipient; 
    }
    $items = $order->get_items();

    // check if a shipped product is in the order   
    foreach ( $items as $item ) {
        $product = $order->get_product_from_item( $item );

        // add our extra recipient if there's a shipped product - commas needed!
        // we can bail if we've found one, no need to add the recipient more than once
        if ( $product && $product->needs_shipping() ) {
            $recipient .= ', warehouse-manager@example.com';
            return $recipient;
        }
    }

    return $recipient;
}
add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2 );
Andy

WRONG :

$recipient .= ', ' . $order->billing_email

CORRECT:

$recipient .= "," . $order->get_billing_email();

I tested and debug this for a whole day.

Update 2018-2019 - For Woocommerce 3+

The accepted answer code is outdated since Woocommerce 3, with some errors in the code.

  • get_product_from_item() method is deprecated and replaced by $item->get_product().
  • The Customer email is missing
  • There is unnecessary code (removed and replaced).

Here is a similar up to date and clean version:

add_filter( 'woocommerce_email_recipient_new_order', 'custom_new_order_email_recipient', 10, 2 );
function custom_new_order_email_recipient( $recipient, $order ) {
    // Avoiding backend displayed error in Woocommerce email settings for undefined $order
    if ( ! is_a( $order, 'WC_Order' ) ) 
        return $recipient;

    // Check order items for a shipped product is in the order   
    foreach ( $order->get_items() as $item ) {
        $product = $item->get_product(); // Get WC_Product instance Object

        // When a product needs shipping we add the customer email to email recipients
        if ( $product->needs_shipping() ) {
            return $recipient . ',' . $order->get_billing_email();
        }
    }
    return $recipient;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!