根据选定的自定义结帐字段值添加自定义电子邮件收件人

问题描述 投票:1回答:1

我需要Woocommerce根据为Field Checkout选择的选项向不同的个人发送自定义电子邮件(从技术上讲,自定义字段是报告他们购买的产品变体的人,但我不知道如何根据购买的产品型号,所以如下)。

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

/**
 * 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' );
}

然后我根据选择的值设置电子邮件收据:

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

    if ($my_field_name == "premium")
        $recipient .= ', [email protected]';
    elseif ($my_field_name == "gold")
        $recipient .= ', [email protected]';
    elseif ($my_field_name == "silver")
            $recipient .= ', [email protected]';
    elseif ($my_field_name == "bronze")
        $recipient .= ', [email protected]';

    return $recipient;
}

当我使用这个代码时,没有一个应该收到指定电子邮件的收据实际上是这样做的。代码有什么问题?

php wordpress woocommerce checkout custom-fields
1个回答
1
投票

您刚刚错过了将所选自定义字段值保存在订单元数据中。我还重新审视了你的代码:

// 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 .= ',[email protected]';
    elseif ($my_field_name == "gold")
        $recipient .= ',[email protected]';
    elseif ($my_field_name == "silver")
        $recipient .= ',[email protected]';
    elseif ($my_field_name == "bronze")
        $recipient .= ',[email protected]';

    return $recipient;
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

在WooCommerce 3+中测试过并且有效。

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

© www.soinside.com 2019 - 2024. All rights reserved.