Php 根据选定的自定义签出字段值添加自定义电子邮件收件人

Php 根据选定的自定义签出字段值添加自定义电子邮件收件人,php,wordpress,woocommerce,checkout,custom-fields,Php,Wordpress,Woocommerce,Checkout,Custom Fields,我需要Woocommerce向不同的个人发送自定义电子邮件,具体取决于为字段签出选择的选项(从技术上讲,自定义字段是报告他们购买的产品变体的人员,但我不确定如何根据购买的产品变体自定义电子邮件收据,因此如下所示) 首先,我使用以下代码建立了自定义字段 /** * Add the field to the checkout */ add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' ); functi

我需要Woocommerce向不同的个人发送自定义电子邮件,具体取决于为字段签出选择的选项(从技术上讲,自定义字段是报告他们购买的产品变体的人员,但我不确定如何根据购买的产品变体自定义电子邮件收据,因此如下所示)

首先,我使用以下代码建立了自定义字段

/**
 * Add the field to the checkout
  */
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h2>' . __('Membership') . '</h2>';

    woocommerce_form_field( 'my_field_name', array(
        'type'          => 'select',
        'class'         => array('wps-drop'),
        'label'         => __('Membership purchased'),
        'options'       => array(
            'blank'     => __( 'Select membership ordered', 'wps' ),
            'premium'   => __( 'Premium Membership', 'wps' ),
            'gold'  => __( 'Gold Membership', 'wps' ),
            'silver'    => __( 'Silver Membership', 'wps' ),
            'bronze'    => __( 'Bronze Membership', 'wps' )
        )
    ), $checkout->get_value( 'my_field_name' ));

    echo '</div>';

}

/**
 * Process the checkout
 */
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['my_field_name'] =='blank')
        wc_add_notice( __( 'Please select status.' ), 'error' );
}

但是,当我使用这个代码时,应该接收指定电子邮件的收据实际上都没有。代码怎么了

您刚刚错过了在order元数据中保存所选自定义字段值。我还回顾了您的代码:

// Add custom checkout field
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
    echo '<div id="my_custom_checkout_field"><h2>' . __('Membership') . '</h2>';

    woocommerce_form_field( 'my_field_name', array(
        'type'      => 'select',
        'class'     => array('wps-drop'),
        'label'     => __('Membership purchased'),
        'required'  => true, // Missing
        'options'   => array(
            ''          => __( 'Select membership ordered', 'wps' ),
            'premium'   => __( 'Premium Membership', 'wps' ),
            'gold'      => __( 'Gold Membership', 'wps' ),
            'silver'    => __( 'Silver Membership', 'wps' ),
            'bronze'    => __( 'Bronze Membership', 'wps' )
        )
    ), $checkout->get_value( 'my_field_name' ) );
    echo '</div>';
}

// Process the checkout
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( empty( $_POST['my_field_name'] ) )
        wc_add_notice( __( 'Please select status.' ), 'error' );
}

// Save the custom checkout field in the order meta
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_field_checkout_update_order_meta', 10, 1 );
function my_custom_field_checkout_update_order_meta( $order_id ) {

    if ( ! empty( $_POST['my_field_name'] ) )
        update_post_meta( $order_id, 'my_field_name', $_POST['my_field_name'] );
}

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

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

    // Get the custom field value (with the right $order_id)
    $my_field_name = get_post_meta( $order_id, 'my_field_name', true );

    if ($my_field_name == "premium")
        $recipient .= ',emailreceipt1@gmail.com';
    elseif ($my_field_name == "gold")
        $recipient .= ',emailreceipt2@gmail.com';
    elseif ($my_field_name == "silver")
        $recipient .= ',emailreceipt1@gmail.com';
    elseif ($my_field_name == "bronze")
        $recipient .= ',emailreceipt2@gmail.com';

    return $recipient;
}
//添加自定义签出字段
添加操作(“订单注释后的woocommerce”、“我的自定义”签出字段”);
函数my_custom_checkout_字段($checkout){
回显“.”(“成员资格”);
woocommerce\u表单\u字段('my\u field\u name',数组(
'类型'=>'选择',
'class'=>array('wps-drop'),
“标签”=>“‘购买会员资格’”,
“必需”=>true,//缺少
“选项”=>数组(
''=>\('Select membership ordered','wps'),
“premium”=>uuu(“premium会员”、“wps”),
“黄金”=>(黄金会员资格、wps),
“银牌”=>“(“银牌会员”、“wps”),
“青铜会员”=>(青铜会员资格”、“wps”)
)
),$checkout->get_value('my_field_name');
回声';
}
//处理结帐
添加_操作('woocmerce_checkout_process'、'my_custom_checkout_field_process');
函数my\u custom\u checkout\u field\u process(){
//检查是否已设置,如果未设置,则添加错误。
if(空($\u POST['my\u field\u name']))
wc_添加_通知(_uu('Please select status'),'error');
}
//在订单元数据中保存自定义签出字段
添加操作('woocommerce\u checkout\u update\u order\u meta'、'my\u custom\u field\u checkout\u update\u order\u meta',10,1);
函数my\u custom\u field\u checkout\u update\u order\u meta($order\u id){
如果(!空($\u POST['my\u field\u name']))
更新发布元($order\u id,'my\u field\u name',$\u post['my\u field\u name');
}
添加过滤器('woocommerce\u email\u recipient\u new\u order','new\u order\u conditional\u email\u recipient',10,2);
功能新订单有条件电子邮件收件人($recipient,$order){
如果(is_admin())返回$recipient;
//获取订单ID(恢复兼容性)
$order\u id=方法\u存在($order,'get\u id')?$order->get\u id():$order->id;
//获取自定义字段值(使用正确的$order\u id)
$my_field_name=get_post_meta($order_id,'my_field_name',true);
如果($my_field_name==“premium”)
$recipient.=',emailreceipt1@gmail.com';
elseif($my_field_name==“gold”)
$recipient.=',emailreceipt2@gmail.com';
elseif($my_field_name==“silver”)
$recipient.=',emailreceipt1@gmail.com';
elseif($my_field_name==“brown”)
$recipient.=',emailreceipt2@gmail.com';
返回$recipient;
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中

在WooCommerce 3+和works中测试

正如您将看到的,收件人已正确添加到“新订单”电子邮件通知中,具体取决于客户的选择

// Add custom checkout field
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
    echo '<div id="my_custom_checkout_field"><h2>' . __('Membership') . '</h2>';

    woocommerce_form_field( 'my_field_name', array(
        'type'      => 'select',
        'class'     => array('wps-drop'),
        'label'     => __('Membership purchased'),
        'required'  => true, // Missing
        'options'   => array(
            ''          => __( 'Select membership ordered', 'wps' ),
            'premium'   => __( 'Premium Membership', 'wps' ),
            'gold'      => __( 'Gold Membership', 'wps' ),
            'silver'    => __( 'Silver Membership', 'wps' ),
            'bronze'    => __( 'Bronze Membership', 'wps' )
        )
    ), $checkout->get_value( 'my_field_name' ) );
    echo '</div>';
}

// Process the checkout
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( empty( $_POST['my_field_name'] ) )
        wc_add_notice( __( 'Please select status.' ), 'error' );
}

// Save the custom checkout field in the order meta
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_field_checkout_update_order_meta', 10, 1 );
function my_custom_field_checkout_update_order_meta( $order_id ) {

    if ( ! empty( $_POST['my_field_name'] ) )
        update_post_meta( $order_id, 'my_field_name', $_POST['my_field_name'] );
}

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

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

    // Get the custom field value (with the right $order_id)
    $my_field_name = get_post_meta( $order_id, 'my_field_name', true );

    if ($my_field_name == "premium")
        $recipient .= ',emailreceipt1@gmail.com';
    elseif ($my_field_name == "gold")
        $recipient .= ',emailreceipt2@gmail.com';
    elseif ($my_field_name == "silver")
        $recipient .= ',emailreceipt1@gmail.com';
    elseif ($my_field_name == "bronze")
        $recipient .= ',emailreceipt2@gmail.com';

    return $recipient;
}