首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >标记特定的WooCommerce产品以添加自定义相关电子邮件收件人

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

Stack Overflow用户
提问于 2020-08-24 05:16:53
回答 1查看 52关注 0票数 0

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

如何才能做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2020-08-24 08:48:10

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

您可以使用an admin product选择字段 (dropdown),在这里您将为相关产品选择一个制造商名称(每个制造商名称都有一个相应的电子邮件隐藏选项值)。

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

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

代码:

代码语言:javascript
运行
复制
// 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文件中。经过测试,效果良好。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63552086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档