Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
679 views
in Technique[技术] by (71.8m points)

php - Send Woocommerce Order to email address listed on product page

I'm using Woocommerce and the Product Addons plugin to add extra fields to a product. One of those fields is an email address to let people send the confirmation of the order to a DIFFERENT address than the billing address shown on the checkout page. Emails should be sent to both addresses.

Any thoughts on maybe how to modify the functions.php file to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the woocommerce_email_recipient_{$this->id} filter hook, you can use the $order argument to get your 2nd email.

But first Lets add globally an email field with the Product Add-ons plugin…

  1. The add on field on the product (fill the field and add to cart):

enter image description here

  1. This "Email" field in order-received (Thank you) page, after checkout:

enter image description here

As you can notice the label of this field is "Email"…

Now if I look in the database in wp_woocommerce_order_itemmeta for this order I can see for the meta_key "Email" the meta_value "[email protected]" :

enter image description here

Now I can set the correct meta_key in the code below to get my email.

Here is the code that will add this additional email recipient for processing and completed customer order email notifications:

add_filter( 'woocommerce_email_recipient_customer_completed_order', 'additional_customer_email_recipient', 10, 2 ); // Processing Order
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'additional_customer_email_recipient', 10, 2 ); // Completed Order
function additional_customer_email_recipient( $recipient, $order ) {
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;

    $additional_recipients = array(); // Initializing…

    // Iterating though each order item
    foreach( $order->get_items() as $item_id => $item_data ){
        // HERE set the the correct meta_key (like 'Email') to get the correct value
        $email = wc_get_order_item_meta( $item_id, 'Email', true );

        // Avoiding duplicates (if many items with many emails)
        // or an existing email in the recipient
        if( ! in_array( $email, $additional_recipients ) && strpos( $recipient, $email ) === false )
            $additional_recipients[] = $email;
    }

    // Convert the array in a coma separated string
    $additional_recipients = implode( ',', $additional_recipients);

    // If an additional recipient exist, we add it
    if( count($additional_recipients) > 0)
        $recipient .= ','.$additional_recipients;

    return $recipient;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...