Send email notifications to a defined email address depending if a product in order has a specific TAG

I looking to send new order woocommerce email notification to two diferent emails depending if a product in the order has a specific TAG or not. So far what I came up with was the following:

//Custom NEW ORDER Email address depending on Product Tag
add_filter( 'woocommerce_email_recipient_new_order', 'new_order_conditional_email_recipient', 10, 2 );
function new_order_conditional_email_recipient( $recipient, $order ) {
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient; // (Optional)

    // Get the order ID (retro compatible)
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

    // Get the custom field value (with the right $order_id)    
    $product_tag = has_term( 'apples', 'product_tag' );

    if ($product_tag) 
        $recipient .= ', <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d504458505c54517d5a505c5451135e5250">[email protected]</a>'; 
    elseif (!$product_tag) 
        $recipient .= ', <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0669726e6374636b676f6a46616b676f6a2865696b">[email protected]</a>';
}

I have adapted the above from some related code search
but the above doesn´t seem to work

thanks in advance for any help

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

To check product has TAG or not you have to fetch product from order and search in them.

//Custom NEW ORDER Email address depending on Product Tag
add_filter( 'woocommerce_email_recipient_new_order', 'new_order_conditional_email_recipient', 10, 2 );
function new_order_conditional_email_recipient( $recipient, $order ) {
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient; // (Optional)

    // Get the order ID (retro compatible)
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

    $items = $order->get_items();
    $order_has_term = false;

    if( !empty( $items ) ){
        foreach ( $items as $item ) {
            $product_id = $item['product_id'];
            $product_has_tag = has_term( 'apples', 'product_tag', $product_id );
            if( $product_has_tag ){
                $order_has_term = true;
                // if we found term in one of the product than we break the loop
                // otherwise it will be override
                break;
            }
        }
    }

    if ( $order_has_term ) {
        $recipient .= ', <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="701d09151d11191c30171d11191c5e131f1d">[email protected]</a>'; 
    } elseif ( !$product_tag ) {
        $recipient .= ', <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7a150e121f081f171b13163a1d171b131654191517">[email protected]</a>';
    }

    return $recipient;
}

One more thing you are extending a filter hook so you have to return a value.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x