0

我们的一些产品将是直销产品。一些制造商只接受电子邮件订单。因此,当订购特定产品时,我希望将它们标记为在订单电子邮件中发送给这些特定制造商。

如何实现?

4

1 回答 1

1

一个好方法是使用自定义字段,您将为这些特定产品填写制造电子邮件。

您可以使用管理产品选择字段(下拉菜单),您将在其中为相关产品选择制造商名称(每个制造商名称将具有相应的电子邮件隐藏选项值)。

然后当购买相关产品时,选定的制造商也会收到新订单电子邮件通知。

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

编码:

// 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', 'woocommerce' ),
        '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', 10, 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 文件中。测试和工作。

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

于 2020-08-24T00:48:09.617 回答