2017-07-16 15 views
1

私はカスタムフィールドを持つチェックアウトフォームを持っています。カスタムフィールドに条件付きでWoocommerce電子メール通知の受信者

カスタムフィールドの値に基づいて注文電子メールに余分な受信者を追加したいと思います。カスタムフィールドは現在、3つのオプションしかないドロップダウンメニューです。

以下は、私がいくつかのグーグルと一緒に作れたコードですが、これは動作しないようです。

function sv_conditional_email_recipient($recipient, $order) { 

    $custom_field = get_post_meta($orderid, 'custom_field', true); 

    if ($custom_field == "Value 1") 
    { 
     $recipient .= ', [email protected]'; 
    } 
    elseif ($custom_field == "Value 2") 
    { 
     $recipient .= ', [email protected]'; 
    } 
    elseif ($custom_field == "Value 3") 
    { 
     $recipient .= ', [email protected]'; 
    } 
    return $recipient; 
} 

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

助けていただければ幸いです。

ありがとうございました。

答えて

0

問題は定義されていない$ orderidから発生します。代わりにこれを試してみてください:

add_filter('woocommerce_email_recipient_new_order', 'new_order_conditional_email_recipient', 10, 2); 
function new_order_conditional_email_recipient($recipient, $order) { 

    // 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) 
    $custom_field = get_post_meta($order_id, 'custom_field', true); 

    if ($custom_field == "Value 1") 
     $recipient .= ', [email protected]'; 
    elseif ($custom_field == "Value 2") 
     $recipient .= ', [email protected]'; 
    elseif ($custom_field == "Value 3") 
     $recipient .= ', [email protected]'; 

    return $recipient; 
} 

コードは、任意のプラグインファイルでも、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルになりますか。

コードは、WooCommerce 2.6.xおよび3+でテストされ、動作します。


関連する問題