标记特定的WooCommerce产品以添加自定义相关的电子邮件收件人

问题描述

我们的某些产品将是直接运送产品。一些制造商仅通过电子邮件接受订单。因此,在订购特定产品时,我希望对它们进行标记,以便在订购电子邮件中将其发送给那些特定制造商。

这怎么完成?

解决方法

一种好方法是使用自定义字段,仅在这些电子邮件中填写这些特定产品的制造商电子邮件。

您可以使用管理产品选择字段(下拉列表) ,在该字段中,您将为相关产品选择制造商名称(每个制造商名称都会隐藏相应的电子邮件地址)选项值)。

然后,当购买相关产品时,所选制造商还将收到新订单电子邮件通知。

您将必须在第一个功能(数组)中设​​置所有不同的相关制造商名称和电子邮件(对)。

代码:

// Custom function that handle settings for manufactures names and emails (pairs)
function manufacture_emails_and_names_pairs() {
    // Below in the array set the email as Key and the manufacture name as value (pairs) for each maufacture (one by line)
    return array(
        'james.web@cocacola.com'      => 'Cocacola','d.anderson@levis.com'        => 'Levis','robert.shows@liebing.com'    => 'Liebing Inc','jimmy.hannes@soupisgood.com' => 'Soup is Good',);
}


// Display an admin product custom Field (dropdown)
add_action( 'woocommerce_product_options_general_product_data','add_admin_custom_product_field' );
function add_admin_custom_product_field() {
    global $product_object;

    echo '<div class="options_group">';

    $field_id = 'manufacture_email';
    $default  = array( '' => __( 'Chose a Manufacture (email notification)','woocommerce' ) );
    $value    = $product_object->get_meta('_'.$field_id);

    woocommerce_wp_select( array(
        'id'                => $field_id,'label'             => __( 'Manufactures (email)','woocommerce' ),'description'       => __( 'Chose here a Manufacture to send an email notification to that manufacture when this product is purchased','desc_tip'          => true,'options'           => array_merge( $default,manufacture_emails_and_names_pairs() ),'value'             => $value,) );

    echo '</div>';
}

// Save admin product custom Field selected value
add_action( 'woocommerce_admin_process_product_object','save_admin_custom_product_field_value' );
function save_admin_custom_product_field_value( $product ){
    $field_id = 'manufacture_email';

    if( isset( $_POST[$field_id] ) ) {

        $product->update_meta_data( '_'.$field_id,sanitize_email( $_POST[$field_id] ) );
    }
}

// Save the manufacture name and email on related order items visible only on admin
add_action( 'woocommerce_checkout_create_order_line_item','giftcard_price_field_order_data',10,4 );
function giftcard_price_field_order_data( $item,$cart_item_key,$values,$order ) {
    $meta_key_1 = '_manufacture_name';
    $meta_key_2 = '_manufacture_email';

    $email = get_post_meta( $values['product_id'],$meta_key_2,true ); // Get manufacture email value

    if ( ! empty( $email ) ) {
        $setting_data = manufacture_emails_and_names_pairs(); // Load settings

        $item->update_meta_data( $meta_key_1,$setting_data[$email] );
        $item->update_meta_data( $meta_key_2,$email );
    }
}

// Add selected Manufacture email recipient to "New Order" email notification
add_filter('woocommerce_email_recipient_new_order','add_manufacture_email_recipient_to_new_order_notification',2);
function add_manufacture_email_recipient_to_new_order_notification( $recipient,$order ) {
    if ( ! is_a( $order,'WC_Order' ) )
        return $recipient;

    $meta_key = '_manufacture_email';

    foreach ( $order->get_items() as $item ) {
        $email = $item->get_meta( $meta_key );

        if ( ! empty( $email ) ) {
            $recipient .= ',' . $email;
        }
    }
    return $recipient;
}

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。

这只是一个显示正确方法的简单示例。现在,您可以向相关的制造商发送自定义电子邮件通知,但这是一个真正的发展。