WooCommerce send new order email to customer

后端 未结 3 1466
野性不改
野性不改 2020-12-18 16:52

I have a problem and it sounds stupid, but I\'m really stucked.

I need to send \"new order\" email also to customer. I tried adding function to functions.php file,

相关标签:
3条回答
  • 2020-12-18 17:05

    WRONG :

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

    CORRECT:

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

    I tested and debug this for a whole day.

    0 讨论(0)
  • 2020-12-18 17:05

    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.

    0 讨论(0)
  • 2020-12-18 17:14

    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 );
    
    0 讨论(0)
提交回复
热议问题