简体   繁体   English

Woocommerce 自定义 email 如果特定产品 ID 在订单中则触发,但如果其他产品在订单中则不会

[英]Woocommerce custom email is triggered if specific product ID is in order but not if other products are in the order

I have a custom woocommerce email trigger function in my Woocommerce store.我在我的 ZBC0A7FD30B18C2C3290D04C28FD8 商店中有一个自定义 woocommerce email 触发器 function。 This email function triggers 2 different emails based on product meta data.此 email function 根据产品元数据触发 2 封不同的电子邮件。 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.如果特定的产品元数据为空,则会触发 email 编号 1,如果它不为空,则会触发 email 编号 2。当我的订单中只有特定 ID 时,这非常有效。 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. 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 基于此产品 ID 2805 元数据。

Thanks in advance for your help.在此先感谢您的帮助。

<?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

}
}

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

}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM