skip to Main Content

I have a custom woocommerce email trigger function in my Woocommerce store. This email function triggers 2 different emails based on product meta data. If specific product meta is empty it triggers email number 1 and if it’s not empty it triggers email number 2. This works excellent when there is only specific ID in my order. Once there is another product in the order, this emails are not triggered.

This is the code part

add_action('woocommerce_order_status_completed', 'send_a_custom_email', 20, 2 );

function send_a_custom_email( $order_id, $order ) {
    global $woocommerce;
    $order = new WC_Order( $order_id );
    $mailer = $woocommerce->mailer();
    $product_ids = array( ); // Initializing
    $customer_email = $order->get_billing_email();
    $customer_name = $order->get_billing_first_name();
    
    foreach ( $order->get_items() as $item ) {
        $meta_data  = $item->get_meta('meno'); // Zisti ake je meno
        $venovanie = $item->get_meta('venovanie'); // // Zisti ake je venovanie
        $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
        
        if( empty($meta_data) ) {
            $product_ids[] = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
        }
    } 

    if ( ! empty($product_ids) && $product_id == 2805 ) {

//email 1

} else if ( empty($product_ids) && $product_id == 2805 ) {

//email 2

}
}

If I remove the $product_id == 2805 from the function, then it sends this email with every order in the store which is also not good because this email has to be triggered only when product id 2805 is in the order and email 1 and email 2 is based on this product ID 2805 meta data.

Thanks in advance for your help.

2

Answers


  1. Chosen as BEST ANSWER

    This solved my problem

    add_action('woocommerce_order_status_completed', 'send_a_custom_email', 20, 2 );
    
    function send_a_custom_email( $order_id, $order ) {
        global $woocommerce;
        $order = new WC_Order( $order_id );
        $mailer = $woocommerce->mailer();
        $product_ids = array( ); // Initializing
        $customer_email = $order->get_billing_email();
        $customer_name = $order->get_billing_first_name();
        
        foreach ( $order->get_items() as $item ) {
            $meta_data  = $item->get_meta('meno'); // Zisti ake je meno
            $venovanie = $item->get_meta('venovanie'); // // Zisti ake je venovanie
            $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
            
            if( empty($meta_data) ) {
                $product_ids[] = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
            }
        } 
    
    if(( !empty($product_ids) && in_array( 2805, $product_ids)) || ( !empty($product_ids) && $product_id == 2805 )){
    
    //email 1
    
    } 
    if(( empty($product_ids) && in_array( 2805, $product_ids)) || ( empty($product_ids) && $product_id == 2805 )){
    
    //email 2
    
    }
    }
    

  2. <?php
    
    add_action('woocommerce_order_status_completed', 'send_a_custom_email', 20, 2 );
    
    function send_a_custom_email( $order_id, $order ) {
        global $woocommerce;
        $order = new WC_Order( $order_id );
        $mailer = $woocommerce->mailer();
        $product_ids = array( ); // Initializing
        $customer_email = $order->get_billing_email();
        $customer_name = $order->get_billing_first_name();
        
        foreach ( $order->get_items() as $item ) {
            $meta_data  = $item->get_meta('meno'); // Zisti ake je meno
            $venovanie = $item->get_meta('venovanie'); // // Zisti ake je venovanie
            $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
            
            if( empty($meta_data) ) {
                $product_ids[] = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
            }
        } 
    
        if ( count($product_ids)>0 && $product_id == 2805 ) {
    
    //email 1
    
    } else if ( empty($product_ids) && $product_id == 2805 ) {
    
    //email 2
    
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search