php WooCommerce如何基于可变属性值添加电子邮件收件人

xoefb8l8  于 2023-02-28  发布在  PHP
关注(0)|答案(1)|浏览(187)

我想根据客户选择的产品变化属性添加不同的电子邮件收件人。我有以下代码,但目前无法正常工作。

function sv_conditional_email_recipient( $recipient, $order ) {
   
        
 foreach ( $order->get_items() as $item_id => $item ) {
           
           $variation = wc_get_product($item['variation_id']);
           $variation_attributes = $variation->get_variation_attributes();
           
           if($variation == "Branch" && $variation_attributes == "BT" ) $recipient .= 'bt@email.com';
           elseif ($variation == "Branch" && $variation_attributes == "PW" ) $recipient .= 'pw@email.com';
           elseif ($variation == "Branch" && $variation_attributes == "TP" ) $recipient .= 'tp@email.com';
           return $recipient;
        }
        
       
    }

    add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 15, 2 );

我很感激如果有人能帮助指出什么是问题,提前感谢你这么多!

s5a0g9ez

s5a0g9ez1#

function sv_conditional_email_recipient($recipient, $order, $email) {

    foreach ($order->get_items() as $item_id => $item) {

        $variation = wc_get_product($item['variation_id']);
        $variation_attributes = $variation->get_variation_attributes();

        foreach ($variation_attributes as $variation_attr_key => $variation_attr_value) {
            if ('BT' === $variation_attr_value) {
                $recipient .= ', bt@email.com';
            }
            if ('PW' === $variation_attr_value) {
                $recipient .= ', pw@email.com';
            }
            if ('TP' === $variation_attr_value) {
                $recipient .= ', tp@email.com';
            }
        }
        return $recipient;
    }
}

add_filter('woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 15, 3);

相关问题